2013-09-12 19:22:46 +02:00
|
|
|
# Creating a functional tests
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
Functional tests test your controllers. The core of these are the same as unit tests:
|
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
* Create a subclass of `[api:SapphireTest]` in the `mysite/tests` or `(module)/tests` folder.
|
2011-02-07 07:48:44 +01:00
|
|
|
* Define static $fixture_file to point to a database YAML file.
|
|
|
|
* Create methods that start with "test" to create your tests.
|
|
|
|
* Assertions are used to work out if a test passed or failed.
|
|
|
|
|
|
|
|
The code of the tests is a little different. Instead of examining the behaviour of objects, we example the results of
|
|
|
|
URLs. Here is an example from the subsites module:
|
|
|
|
|
|
|
|
:::php
|
|
|
|
class SubsiteAdminTest extends SapphireTest {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $fixture_file = 'subsites/tests/SubsiteTest.yml';
|
2013-09-12 19:22:46 +02:00
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
/**
|
|
|
|
* Return a session that has a user logged in as an administrator
|
|
|
|
*/
|
2012-01-30 23:13:42 +01:00
|
|
|
public function adminLoggedInSession() {
|
2014-06-19 00:38:26 +02:00
|
|
|
return Injector::inst()->create('Session', array(
|
2011-02-07 07:48:44 +01:00
|
|
|
'loggedInAs' => $this->idFromFixture('Member', 'admin')
|
|
|
|
));
|
|
|
|
}
|
2013-09-12 19:22:46 +02:00
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
/**
|
|
|
|
* Test generation of the view
|
|
|
|
*/
|
2012-01-30 23:13:42 +01:00
|
|
|
public function testBasicView() {
|
2011-02-07 07:48:44 +01:00
|
|
|
// Open the admin area logged in as admin
|
|
|
|
$response1 = Director::test('admin/subsites/', null, $this->adminLoggedInSession());
|
2013-09-12 19:22:46 +02:00
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
// Confirm that this URL gets you the entire page, with the edit form loaded
|
|
|
|
$response2 = Director::test('admin/subsites/show/1', null, $this->adminLoggedInSession());
|
|
|
|
$this->assertTrue(strpos($response2->getBody(), 'id="Root_Configuration"') !== false);
|
|
|
|
$this->assertTrue(strpos($response2->getBody(), '<head') !== false);
|
2013-09-12 19:22:46 +02:00
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
// Confirm that this URL gets you just the form content, with the edit form loaded
|
|
|
|
$response3 = Director::test('admin/subsites/show/1', array('ajax' => 1), $this->adminLoggedInSession());
|
2013-09-12 19:22:46 +02:00
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
$this->assertTrue(strpos($response3->getBody(), 'id="Root_Configuration"') !== false);
|
|
|
|
$this->assertTrue(strpos($response3->getBody(), '<form') === false);
|
|
|
|
$this->assertTrue(strpos($response3->getBody(), '<head') === false);
|
|
|
|
}
|
2013-09-24 22:09:30 +02:00
|
|
|
}
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
We are using a new static method here: **Director::test($url, $postVars, $sessionObj)**
|
|
|
|
|
|
|
|
Director::test() lets us execute a URL and see what happens. It bypasses HTTP, instead relying on the cleanly
|
2011-03-08 22:05:51 +01:00
|
|
|
encapsulated execution model of `[api:Controller]`.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
It takes 3 arguments:
|
|
|
|
|
|
|
|
* $url: The URL to execute
|
|
|
|
* $postVars: Post variables to pass to the URL
|
|
|
|
* $sessionObj: A Session object representing the current session.
|
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
And it returns an `[api:HTTPResponse]` object, which will give you the response headers (including redirection), status code,
|
2011-02-07 07:48:44 +01:00
|
|
|
and body.
|
|
|
|
|
|
|
|
We can use string processing on the body of the response to then see if it fits with our expectations.
|
|
|
|
|
|
|
|
If you're testing for natural language responses like error messages, make sure to use [i18n](/topics/i18n) translations through
|
|
|
|
the *_t()* method to avoid tests failing when i18n is enabled.
|
2012-11-09 19:16:16 +01:00
|
|
|
|
2013-09-24 22:09:30 +02:00
|
|
|
Note that for a more highlevel testing approach, SilverStripe also supports
|
2013-09-12 19:22:46 +02:00
|
|
|
[behaviour-driven testing through Behat](https://github.com/silverstripe-labs/silverstripe-behat-extension). It interacts
|
2014-06-19 00:38:26 +02:00
|
|
|
directly with your website or CMS interface by remote controlling an actual browser, driven by natural language assertions.
|