silverstripe-framework/docs/en/02_Developer_Guides/06_Testing/How_Tos/01_Write_a_FunctionalTest.md
Aaron Carlino 6888901468
NEW: Update docs to be compliant with Gatsby site (#9314)
* First cut

* Temporarily disable composer.json for netlify build

* POC

* New recursive directory query, various refinements

* Fix flexbox

* new styled components plugin

* Apply frontmatter delimiters

* Mobile styles, animation

* Search

* Redesign, clean up

* Nuke the cache, try again

* fix file casing

* Remove production env file

* ID headers

* Move app to new repo

* Add frontmatter universally

* Hide children changelogs

* Add how to title

* New callout tags

* Revert inline code block change

* Replace note callouts

* Fix icons

* Repalce images

* Fix icon

* Fix image links

* Use proper SQL icon
2019-11-18 17:58:33 +13:00

1.8 KiB

title summary
How to write a FunctionalTest Expand your testing capabilities with integrations tests

How to Write a FunctionalTest

FunctionalTest test your applications Controller instances and anything else which requires a web request. The core of these tests are the same as SapphireTest unit tests but add several methods for creating HTTPRequest and receiving HTTPResponse objects. In this How To, we'll see how to write a test to query a page, check the response and modify the session within a test.

app/tests/HomePageTest.php

use SilverStripe\Security\Member;

class HomePageTest extends FunctionalTest 
{

    /**
     * Test generation of the view
     */
    public function testViewHomePage() 
    {
        $page = $this->get('home/');

        // Home page should load..
        $this->assertEquals(200, $page->getStatusCode());

        // We should see a login form
        $login = $this->submitForm("LoginFormID", null, [
            'Email' => 'test@example.com',
            'Password' => 'wrongpassword'
        ]);

        // wrong details, should now see an error message
        $this->assertExactHTMLMatchBySelector("#LoginForm p.error", [
            "That email address is invalid."
        ]);

        // If we login as a user we should see a welcome message
        $me = Member::get()->first();

        $this->logInAs($me);
        $page = $this->get('home/');

        $this->assertExactHTMLMatchBySelector("#Welcome", [
            'Welcome Back'
        ]);
    }
}

API Documentation