Version: current

# Class FunctionalTest

Sapphire-specific testing object designed to support functional testing of your web app. It simulates get/post requests, form submission, and can validate resulting HTML, looking up content by CSS selector.

The example below shows how it works.

     function testMyForm() {
       // Visit a URL
       $this->get("your/url");

       // Submit a form on the page that you get in response
       $this->submitForm("MyForm_ID",  array("Email" => "invalid email ^&*&^"));

       // Validate the content that is returned
       $this->assertExactMatchBySelector("#MyForm_ID p.error", array("That email address is invalid."));
    }

  PHPUnit_Framework_TestCase
  SapphireTest
  FunctionalTest

Package: sapphire\testing
Located at sapphire/dev/FunctionalTest.php

## Methods summary

`public` | # `session( )` Returns the `Session` object for this test Returns the `Session` object for this test
`public` | # `setUp( )` #### Overrides `SapphireTest::setUp`
`public` | # `tearDown( )` #### Overrides `SapphireTest::tearDown`
`public` | # `get( mixed $url, mixed $session = null, mixed $headers = null, mixed $cookies = null )` Submit a get request Submit a get request #### Uses ``Director::test()``
`public` | # `post( mixed $url, mixed $data, mixed $headers = null, mixed $session = null, mixed $body = null, mixed $cookies = null )` Submit a post request Submit a post request #### Uses ``Director::test()``
`public` | # `submitForm( mixed $formID, mixed $button = null, mixed $data = array() )` Submit the form with the given HTML ID, filling it out with the given data. Acts on the most recent response Submit the form with the given HTML ID, filling it out with the given data. Acts on the most recent response
`public` | # `content( )` Return the most recent content Return the most recent content
`public `SimpleXMLElement`` | # `findAttribute( `SimpleXMLElement` $object, string $attribute )` Find an attribute in a SimpleXMLElement object by name. Find an attribute in a SimpleXMLElement object by name. #### Parameters $object SimpleXMLElement object $attribute string $attribute Name of attribute to find #### Returns ``SimpleXMLElement`` object of the attribute
`public` | # `cssParser( )` Return a CSSContentParser for the most recent content. Return a CSSContentParser for the most recent content.
`public boolean` | # `assertPartialMatchBySelector( string $selector, array|string $expectedMatches )` Assert that the most recently queried page contains a number of content tags specified by a CSS selector. The given CSS selector will be applied to the HTML of the most recent page. The content of every matching tag will be examined. The assertion fails if one of the expectedMatches fails to appear. Assert that the most recently queried page contains a number of content tags specified by a CSS selector. The given CSS selector will be applied to the HTML of the most recent page. The content of every matching tag will be examined. The assertion fails if one of the expectedMatches fails to appear. Note: characters are stripped from the content; make sure that your assertions take this into account. #### Parameters $selector string $selector A basic CSS selector, e.g. 'li.jobs h3' $expectedMatches array|string $expectedMatches The content of at least one of the matched tags #### Returns `boolean` boolean #### Throws `PHPUnit_Framework_AssertionFailedError` PHPUnit_Framework_AssertionFailedError
`public boolean` | # `assertExactMatchBySelector( string $selector, array|string $expectedMatches )` Assert that the most recently queried page contains a number of content tags specified by a CSS selector. The given CSS selector will be applied to the HTML of the most recent page. The full HTML of every matching tag will be examined. The assertion fails if one of the expectedMatches fails to appear. Assert that the most recently queried page contains a number of content tags specified by a CSS selector. The given CSS selector will be applied to the HTML of the most recent page. The full HTML of every matching tag will be examined. The assertion fails if one of the expectedMatches fails to appear. Note: characters are stripped from the content; make sure that your assertions take this into account. #### Parameters $selector string $selector A basic CSS selector, e.g. 'li.jobs h3' $expectedMatches array|string $expectedMatches The content of all matching tags as an array #### Returns `boolean` boolean #### Throws `PHPUnit_Framework_AssertionFailedError` PHPUnit_Framework_AssertionFailedError
`public boolean` | # `assertPartialHTMLMatchBySelector( string $selector, array|string $expectedMatches )` Assert that the most recently queried page contains a number of content tags specified by a CSS selector. The given CSS selector will be applied to the HTML of the most recent page. The content of every matching tag will be examined. The assertion fails if one of the expectedMatches fails to appear. Assert that the most recently queried page contains a number of content tags specified by a CSS selector. The given CSS selector will be applied to the HTML of the most recent page. The content of every matching tag will be examined. The assertion fails if one of the expectedMatches fails to appear. Note: characters are stripped from the content; make sure that your assertions take this into account. #### Parameters $selector string $selector A basic CSS selector, e.g. 'li.jobs h3' $expectedMatches array|string $expectedMatches The content of at least one of the matched tags #### Returns `boolean` boolean #### Throws `PHPUnit_Framework_AssertionFailedError` PHPUnit_Framework_AssertionFailedError
`public boolean` | # `assertExactHTMLMatchBySelector( string $selector, array|string $expectedMatches )` Assert that the most recently queried page contains a number of content tags specified by a CSS selector. The given CSS selector will be applied to the HTML of the most recent page. The full HTML of every matching tag will be examined. The assertion fails if one of the expectedMatches fails to appear. Assert that the most recently queried page contains a number of content tags specified by a CSS selector. The given CSS selector will be applied to the HTML of the most recent page. The full HTML of every matching tag will be examined. The assertion fails if one of the expectedMatches fails to appear. Note: characters are stripped from the content; make sure that your assertions take this into account. #### Parameters $selector string $selector A basic CSS selector, e.g. 'li.jobs h3' $expectedMatches array|string $expectedMatches The content of all matched tags as an array #### Returns `boolean` boolean #### Throws `PHPUnit_Framework_AssertionFailedError` PHPUnit_Framework_AssertionFailedError
`public` | # `logInAs( mixed $member )` Log in as the given member Log in as the given member #### Parameters $member $member The ID, fixture codename, or Member object of the member that you want to log in
`public` | # `useDraftSite( )` Use the draft (stage) site for testing. This is helpful if you're not testing publication functionality and don't want "stage management" cluttering your test. Use the draft (stage) site for testing. This is helpful if you're not testing publication functionality and don't want "stage management" cluttering your test.
`public` | # `stat( mixed $varName )` Return a static variable from this class. Gets around PHP's lack of late static binding. Return a static variable from this class. Gets around PHP's lack of late static binding.

### Methods inherited from SapphireTest

`allFixtureIDs()`, `assertDOSAllMatch()`, `assertDOSContains()`, `assertDOSEquals()`, `assertEmailSent()`, `clearEmails()`, `clearFixtures()`, `create_temp_db()`, `delete_all_temp_dbs()`, `empty_temp_db()`, `findEmail()`, `idFromFixture()`, `is_running_test()`, `kill_temp_db()`, `loadFixture()`, `logInWithPermission()`, `objFromFixture()`, `resetDBSchema()`, `setUpOnce()`, `tearDownOnce()`, `using_temp_db()`

### Magic methods summary

## Properties summary

`public static boolean` | $disable_themes | # Set this to true on your sub-class to disable the use of themes in this test. This can be handy for functional testing of modules without having to worry about whether a user has changed behaviour by replacing the theme. Set this to true on your sub-class to disable the use of themes in this test. This can be handy for functional testing of modules without having to worry about whether a user has changed behaviour by replacing the theme.
`public static boolean` | $use_draft_site | # Set this to true on your sub-class to use the draft site by default for every test in this class. Set this to true on your sub-class to use the draft site by default for every test in this class.
`protected mixed` | $mainSession | #
`protected mixed` | $cssParser | # CSSContentParser for the most recently requested page. CSSContentParser for the most recently requested page.
`protected boolean` | $autoFollowRedirection | # If this is true, then 30x Location headers will be automatically followed. If not, then you will have to manaully call $this->mainSession->followRedirection() to follow them. However, this will let you inspect the intermediary headers If this is true, then 30x Location headers will be automatically followed. If not, then you will have to manaully call $this->mainSession->followRedirection() to follow them. However, this will let you inspect the intermediary headers

### Properties inherited from SapphireTest

`$backupGlobals`, `$cache_generatedMembers`, `$extraDataObjects`, `$fixtureDictionary`, `$fixture_file`, `$fixtures`, `$illegalExtensions`, `$is_running_test`, `$mailer`, `$originalIsRunningTest`, `$originalMailer`, `$originalMemberPasswordValidator`, `$originalNestedURLsState`, `$originalRequirements`, `$originalTheme`, `$requireDefaultRecordsFrom`, `$requiredExtensions`

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -