FunctionalTest
abstract class FunctionalTest extends SapphireTest implements TestOnly (View source)
SilverStripe-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.
public function testMyForm() {
// Visit a URL
$this->get("your/url");
// Submit a form on the page that you get in response $this->submitForm("MyForm_ID", "action_dologin", array("Email" => "invalid email ^&*&^"));
// Validate the content that is returned $this->assertExactMatchBySelector("#MyForm_ID p.error", array("That email address is invalid.")); }
Methods
Called once per test case ({@link SapphireTest} subclass).
tearDown method that's called once per test class rather once per test method.
Clear all fixtures which were previously loaded through {@link loadFixture()}
Asserts that an array has a specified subset.
No description
No description
Search for an email that was sent.
Assert that the matching email was sent since the last call to clearEmails() All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression.
Assert that the given {@link SS_List} includes DataObjects matching the given key-value pairs. Each match must correspond to 1 distinct record.
Asserts that no items in a given list appear in the given dataobject list
Assert that the given {@link SS_List} includes only DataObjects matching the given key-value pairs. Each match must correspond to 1 distinct record.
Assert that the every record in the given {@link SS_List} matches the given key-value pairs.
Asserts that two SQL queries are equivalent
Asserts that a SQL query contains a SQL fragment
Asserts that a SQL query contains a SQL fragment
Reset the testing database's schema, but only if it is active
A wrapper for automatically performing callbacks as a user with a specific permission
Create a member and group with the given permission code, and log in with it.
Return all extra objects to scaffold for this test
Get additional controller classes to register routes for
Submit the form with the given HTML ID, filling it out with the given data.
Return the most recent content
Find an attribute in a SimpleXMLElement object by name.
Return a CSSContentParser for the most recent content.
Assert that the most recently queried page contains a number of content tags specified by a CSS selector.
Assert that the most recently queried page contains a number of content tags specified by a CSS selector.
Assert that the most recently queried page contains a number of content tags specified by a CSS selector.
Assert that the most recently queried page contains a number of content tags specified by a CSS selector.
No description
Details
static TempDatabase
tempDB()
static array
getIllegalExtensions()
Gets illegal extensions for this class
static array
getRequiredExtensions()
Gets required extensions for this class
static string|string[]
get_fixture_file()
bool
getUsesDatabase()
bool
getUsesTransactions()
array
getRequireDefaultRecordsFrom()
static void
setUpBeforeClass()
Called once per test case ({@link SapphireTest} subclass).
This is different to {@link setUp()}, which gets called once per method. Useful to initialize expensive operations which don't change state for any called method inside the test, e.g. dynamically adding an extension. See {@link teardownAfterClass()} for tearing down the state again.
Always sets up in order:
- Reset php state
- Nest
- Custom state helpers
User code should call parent::setUpBeforeClass() before custom setup code
static void
tearDownAfterClass()
tearDown method that's called once per test class rather once per test method.
Always sets up in order:
- Custom state helpers
- Unnest
- Reset php state
User code should call parent::tearDownAfterClass() after custom tear down code
FixtureFactory
getFixtureFactory()
deprecated
deprecated
SapphireTest
setFixtureFactory(FixtureFactory $factory)
deprecated
deprecated
Sets a new fixture factory
void
loadFixture(string $fixtureFile)
deprecated
deprecated
Load a YAML fixture file into the database.
Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture. Doesn't clear existing fixtures.
void
clearFixtures()
Clear all fixtures which were previously loaded through {@link loadFixture()}
static void
assertArraySubset(array|ArrayAccess $subset, array|ArrayAccess $array, bool $checkForObjectIdentity = false, string $message = '')
Asserts that an array has a specified subset.
static void
assertContains($needle, $haystack, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false)
static void
assertNotContains($needle, $haystack, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false)
bool
clearEmails()
Clear the log of emails sent
static array
findEmail(string $to, string $from = null, string $subject = null, string $content = null)
Search for an email that was sent.
All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression.
static void
assertEmailSent(string $to, string $from = null, string $subject = null, string $content = null)
Assert that the matching email was sent since the last call to clearEmails() All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression.
static void
assertListContains(SS_List|array $matches, SS_List $list, string $message = '')
Assert that the given {@link SS_List} includes DataObjects matching the given key-value pairs. Each match must correspond to 1 distinct record.
assertDOSContains(array $matches, $dataObjectSet)
deprecated
deprecated
static void
assertListNotContains(SS_List|array $matches, SS_List $list, string $message = '')
Asserts that no items in a given list appear in the given dataobject list
static
assertNotDOSContains($matches, $dataObjectSet)
deprecated
deprecated
static void
assertListEquals(mixed $matches, SS_List $list, string $message = '')
Assert that the given {@link SS_List} includes only DataObjects matching the given key-value pairs. Each match must correspond to 1 distinct record.
Example
Check that only the entries Sam Minnee and Ingo Schommer exist in $members. Order doesn't matter: $this->assertListEquals([ ['FirstName' =>'Sam', 'Surname' => 'Minnee'], ['FirstName' => 'Ingo', 'Surname' => 'Schommer'], ], $members);
assertDOSEquals($matches, SS_List $dataObjectSet)
deprecated
deprecated
static void
assertListAllMatch(mixed $match, SS_List $list, string $message = '')
Assert that the every record in the given {@link SS_List} matches the given key-value pairs.
Example
Check that every entry in $members has a Status of 'Active': $this->assertListAllMatch(['Status' => 'Active'], $members);
assertDOSAllMatch($match, SS_List $dataObjectSet)
deprecated
deprecated
static void
assertSQLEquals(string $expectedSQL, string $actualSQL, string $message = '', int $delta = 0, int $maxDepth = 10, bool $canonicalize = false, bool $ignoreCase = false)
Asserts that two SQL queries are equivalent
static void
assertSQLContains(string $needleSQL, string $haystackSQL, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true)
Asserts that a SQL query contains a SQL fragment
static void
assertSQLNotContains(string $needleSQL, string $haystackSQL, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true)
Asserts that a SQL query contains a SQL fragment
static void
start()
Start test environment
static void
resetDBSchema(bool $includeExtraDataObjects = false, bool $forceCreate = false)
Reset the testing database's schema, but only if it is active
mixed
actWithPermission(string|array $permCode, callable $callback)
A wrapper for automatically performing callbacks as a user with a specific permission
int
logInWithPermission(string|array $permCode = 'ADMIN')
Create a member and group with the given permission code, and log in with it.
Returns the member ID.
void
logInAs(Member|int|string $member)
Log in as the given member
void
logOut()
Log out the current user
static array
getExtraDataObjects()
Return all extra objects to scaffold for this test
static array
getExtraControllers()
Get additional controller classes to register routes for
Session
session()
Returns the {@link Session} object for this test
HTTPResponse
get(string $url, Session $session = null, array $headers = null, array $cookies = null)
Submit a get request
HTTPResponse
post(string $url, array $data, array $headers = null, Session $session = null, string $body = null, array $cookies = null)
Submit a post request
HTTPResponse
submitForm(string $formID, string $button = null, array $data = array())
Submit the form with the given HTML ID, filling it out with the given data.
Acts on the most recent response.
Any data parameters have to be present in the form, with exact form field name and values, otherwise they are removed from the submission.
Caution: Parameter names have to be formatted as they are in the form submission, not as they are interpreted by PHP. Wrong: array('mycheckboxvalues' => array(1 => 'one', 2 => 'two')) Right: array('mycheckboxvalues[1]' => 'one', 'mycheckboxvalues[2]' => 'two')
string
content()
Return the most recent content
SimpleXMLElement
findAttribute(SimpleXMLElement $object, string $attribute)
Find an attribute in a SimpleXMLElement object by name.
CSSContentParser
cssParser()
Return a CSSContentParser for the most recent content.
assertPartialMatchBySelector(string $selector, array|string $expectedMatches, string $message = null)
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.
assertExactMatchBySelector(string $selector, array|string $expectedMatches, string $message = null)
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.
assertPartialHTMLMatchBySelector(string $selector, array|string $expectedMatches, string $message = null)
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.
assertExactHTMLMatchBySelector(string $selector, array|string $expectedMatches, string $message = null)
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.
useDraftSite(bool $enabled = true)
deprecated
deprecated
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.
static bool
get_disable_themes()
static bool
get_use_draft_site()
deprecated
deprecated