Search code examples
phplaravelfunctional-testingcodeception

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?


Solution

  • 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.