Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Codeception functional tests to POST data

I want to create a test which will directly post data to a Laravel page to test it handling bad data.

I can run an acceptance test starting at the page with the form on, call $I->click('Search'); and the page processes the data as expected.

Reading the introduction to Functional Tests on the Codeception website, it states

In simple terms we set $_REQUEST, $_GET and $_POST variables, then we execute your script inside a test, we receive output, and then we test it.

This sounds ideal, set an array of POST data and fire it directly at the processing page. But I can't find any documentation for it. I've played with sendAjaxPostRequest but it's not passing anything to $_POST.

Is there a way I can test the pages in isolation like this?

like image 216
Chris Armitage Avatar asked Apr 24 '26 16:04

Chris Armitage


1 Answers

Turns out the solution lies with the Codeception REST module.

Adding this to the functional.suite.yml file allows you to write:

$I = new TestGuy($scenario);
$I->wantTo('check a page is resistant to POST injection');
$I->sendPOST(
    'search',
    array(
        'startDate' => '2013-07-03',
        'endDate' => '2013-07-10',
        'exec' => 'some dodgy commands',
    ));
$I->see('Search results');
$I->dontSee('Dodgy command executed');

A little clunky, but it allows testing of a single page.

like image 99
Chris Armitage Avatar answered Apr 27 '26 07:04

Chris Armitage



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!