2013-09-12 19:22:46 +02:00
|
|
|
# Creating a SilverStripe Test
|
|
|
|
|
2013-09-21 21:24:32 +02:00
|
|
|
A test is created by extending one of two classes, SapphireTest and FunctionalTest.
|
|
|
|
You would subclass `[api:SapphireTest]` to test your application logic,
|
|
|
|
for example testing the behaviour of one of your `[DataObjects](api:DataObject)`,
|
|
|
|
whereas `[api:FunctionalTest]` is extended when you want to test your application's functionality,
|
|
|
|
such as testing the results of GET and POST requests,
|
|
|
|
and validating the content of a page. FunctionalTest is a subclass of SapphireTest.
|
2013-09-12 19:22:46 +02:00
|
|
|
|
|
|
|
## Creating a test from SapphireTest
|
|
|
|
|
|
|
|
Here is an example of a test which extends SapphireTest:
|
|
|
|
|
|
|
|
:::php
|
|
|
|
<?php
|
|
|
|
class SiteTreeTest extends SapphireTest {
|
|
|
|
|
|
|
|
// Define the fixture file to use for this test class
|
2014-01-02 12:38:43 +01:00
|
|
|
protected static $fixture_file = 'SiteTreeTest.yml';
|
2013-09-12 19:22:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test generation of the URLSegment values.
|
|
|
|
* - Turns things into lowercase-hyphen-format
|
|
|
|
* - Generates from Title by default, unless URLSegment is explicitly set
|
|
|
|
* - Resolves duplicates by appending a number
|
|
|
|
*/
|
|
|
|
public function testURLGeneration() {
|
|
|
|
$expectedURLs = array(
|
|
|
|
'home' => 'home',
|
|
|
|
'staff' => 'my-staff',
|
|
|
|
'about' => 'about-us',
|
2013-09-21 21:24:32 +02:00
|
|
|
'staffduplicate' => 'my-staff-2'
|
2013-09-12 19:22:46 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach($expectedURLs as $fixture => $urlSegment) {
|
|
|
|
$obj = $this->objFromFixture('Page', $fixture);
|
|
|
|
$this->assertEquals($urlSegment, $obj->URLSegment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Firstly we define a static member `$fixture_file`, this should point to a file that represents the data we want to test,
|
|
|
|
represented in YAML. When our test is run, the data from this file will be loaded into a test database for our test to use.
|
|
|
|
This property can be an array of strings pointing to many .yml files, but for our test we are just using a string on its
|
2013-09-21 21:24:32 +02:00
|
|
|
own. For more detail on fixtures, see [this page](fixtures).
|
2013-09-12 19:22:46 +02:00
|
|
|
|
|
|
|
The second part of our class is the `testURLGeneration` method. This method is our test. You can asign many tests, but
|
2013-09-21 21:24:32 +02:00
|
|
|
again for our purposes there is just the one. When the test is executed, methods prefixed with the word `test` will be
|
|
|
|
run. The test database is rebuilt every time one of these methods is run.
|
2013-09-12 19:22:46 +02:00
|
|
|
|
|
|
|
Inside our test method is the `objFromFixture` method that will generate an object for us based on data from our fixture
|
|
|
|
file. To identify to the object, we provide a class name and an identifier. The identifier is specified in the YAML file
|
|
|
|
but not saved in the database anywhere, `objFromFixture` looks the `[api:DataObject]` up in memory rather than using the
|
|
|
|
database. This means that you can use it to test the functions responsible for looking up content in the database.
|
|
|
|
|
|
|
|
The final part of our test is an assertion command, `assertEquals`. An assertion command allows us to test for something
|
|
|
|
in our test methods (in this case we are testing if two values are equal). A test method can have more than one assertion
|
2013-09-21 21:24:32 +02:00
|
|
|
command, and if any one of these assertions fail, so will the test method.
|
2013-09-12 19:22:46 +02:00
|
|
|
|
|
|
|
For more information on PHPUnit's assertions see the [PHPUnit manual](http://www.phpunit.de/manual/current/en/api.html#api.assert).
|
|
|
|
|
2013-09-21 21:24:32 +02:00
|
|
|
The `[api:SapphireTest]` class comes with additional assertions which are more specific to Sapphire, for example the
|
|
|
|
`assertEmailSent` method, which simulates sending emails through the `Email->send()`
|
|
|
|
API without actually using a mail server. For more details on this see the [testing emails](testing-email) guide.
|