2019-11-18 05:58:33 +01:00
|
|
|
---
|
2014-10-17 06:53:52 +02:00
|
|
|
title: How to write a SapphireTest
|
2019-11-18 05:58:33 +01:00
|
|
|
summary: Learn the basics of unit testing in Silverstripe
|
|
|
|
---
|
2014-10-17 06:53:52 +02:00
|
|
|
|
|
|
|
# How to write a SapphireTest
|
|
|
|
|
2017-07-03 03:22:12 +02:00
|
|
|
Here is an example of a test which extends [SapphireTest](api:SilverStripe\Dev\SapphireTest) to test the URL generation of the page. It also showcases
|
2014-10-17 06:53:52 +02:00
|
|
|
how you can load default records into the test database.
|
|
|
|
|
2018-06-25 00:39:53 +02:00
|
|
|
**app/tests/PageTest.php**
|
2014-10-17 06:53:52 +02:00
|
|
|
|
2017-08-03 02:51:32 +02:00
|
|
|
```php
|
2017-10-27 04:38:27 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2014-10-17 06:53:52 +02:00
|
|
|
|
2017-10-27 04:38:27 +02:00
|
|
|
class PageTest extends SapphireTest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Defines the fixture file to use for this test class
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected static $fixture_file = 'SiteTreeTest.yml';
|
2014-10-17 06:53:52 +02:00
|
|
|
|
2017-10-27 04:38:27 +02:00
|
|
|
/**
|
|
|
|
* Test generation of the URLSegment values.
|
|
|
|
*
|
|
|
|
* Makes sure to:
|
|
|
|
* - Turn 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 = [
|
|
|
|
'home' => 'home',
|
|
|
|
'staff' => 'my-staff',
|
|
|
|
'about' => 'about-us',
|
|
|
|
'staffduplicate' => 'my-staff-2'
|
|
|
|
];
|
2014-10-17 06:53:52 +02:00
|
|
|
|
2017-10-27 04:38:27 +02:00
|
|
|
foreach ($expectedURLs as $fixture => $urlSegment) {
|
|
|
|
$obj = $this->objFromFixture('Page', $fixture);
|
2014-10-17 06:53:52 +02:00
|
|
|
|
2017-10-27 04:38:27 +02:00
|
|
|
$this->assertEquals($urlSegment, $obj->URLSegment);
|
2017-08-07 05:11:17 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-27 04:38:27 +02:00
|
|
|
}
|
2017-08-03 02:51:32 +02:00
|
|
|
```
|
2014-10-17 06:53:52 +02:00
|
|
|
|
|
|
|
Firstly we define a static `$fixture_file`, this should point to a file that represents the data we want to test,
|
|
|
|
represented as a YAML [Fixture](../fixtures). When our test is run, the data from this file will be loaded into a test
|
|
|
|
database and discarded at the end of the test.
|
|
|
|
|
2019-11-18 05:58:33 +01:00
|
|
|
[notice]
|
2014-10-17 06:53:52 +02:00
|
|
|
The `fixture_file` property can be path to a file, or an array of strings pointing to many files. The path must be
|
|
|
|
absolute from your website's root folder.
|
2019-11-18 05:58:33 +01:00
|
|
|
[/notice]
|
2014-10-17 06:53:52 +02:00
|
|
|
|
|
|
|
The second part of our class is the `testURLGeneration` method. This method is our test. When the test is executed,
|
|
|
|
methods prefixed with the word `test` will be run.
|
|
|
|
|
2019-11-18 05:58:33 +01:00
|
|
|
[notice]
|
2014-10-17 06:53:52 +02:00
|
|
|
The test database is rebuilt every time one of these methods is run.
|
2019-11-18 05:58:33 +01:00
|
|
|
[/notice]
|
2014-10-17 06:53:52 +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
|
2017-07-03 03:22:12 +02:00
|
|
|
but not saved in the database anywhere, `objFromFixture` looks the [DataObject](api:SilverStripe\ORM\DataObject) up in memory rather than using the
|
2014-10-17 06:53:52 +02:00
|
|
|
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 command, and if any one of these assertions fail, so will the test method.
|
|
|
|
|
2019-11-18 05:58:33 +01:00
|
|
|
[info]
|
2014-10-17 06:53:52 +02:00
|
|
|
For more information on PHPUnit's assertions see the [PHPUnit manual](http://www.phpunit.de/manual/current/en/api.html#api.assert).
|
2019-11-18 05:58:33 +01:00
|
|
|
[/info]
|
2014-10-17 06:53:52 +02:00
|
|
|
|
|
|
|
## Related Documentation
|
|
|
|
|
|
|
|
* [Unit Testing](../unit_testing)
|
|
|
|
* [Fixtures](../fixtures)
|
|
|
|
|
|
|
|
## API Documentation
|
|
|
|
|
2017-07-03 03:22:12 +02:00
|
|
|
* [SapphireTest](api:SilverStripe\Dev\SapphireTest)
|
|
|
|
* [FunctionalTest](api:SilverStripe\Dev\FunctionalTest)
|