Merge pull request #9957 from Pen-y-Fan/patch-4

[doc] Add additional information required to run the test
This commit is contained in:
Dylan Wagstaff 2021-06-16 22:20:06 +12:00 committed by GitHub
commit 5ba38520d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,7 +17,7 @@ class PageTest extends SapphireTest
{ {
/** /**
* Defines the fixture file to use for this test class * Defines the fixture file to use for this test class
* @var string * @var string $fixture_file
*/ */
protected static $fixture_file = 'SiteTreeTest.yml'; protected static $fixture_file = 'SiteTreeTest.yml';
@ -47,6 +47,38 @@ class PageTest extends SapphireTest
} }
``` ```
**app/tests/SiteTreeTest.yml**
```yaml
Page:
home:
Title: Home
staff:
Title: My Staff
about:
Title: About Us
staffduplicate:
Title: My Staff
```
**phpunit.xml**
```xml
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuite name="Default">
<directory>app/tests/</directory>
</testsuite>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">app/src</directory>
<exclude>
<directory suffix=".php">app/tests/</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
```
Firstly we define a static `$fixture_file`, this should point to a file that represents the data we want to test, 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 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. database and discarded at the end of the test.
@ -72,6 +104,16 @@ The final part of our test is an assertion command, `assertEquals`. An assertion
in our test methods (in this case we are testing if two values are equal). A test method can have more than one 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. assertion command, and if any one of these assertions fail, so will the test method.
The example **phpunit.xml** file should be placed in the root folder of your project. PHPUnit 5.7 should be included by default, as a dev dependancy, in the **composer.json** file.
## Caching
Just like on web requests, SilverStripe caches metadata about the execution context. This cache can get stale, e.g. when you change YAML configuration or add certain types of PHP code. In order to flush the cache, the **first time** this test is run use the `flush=1` CLI parameter:
```sh
vendor/bin/phpunit app/tests/PageTest.php '' flush=1
```
[info] [info]
For more information on PHPUnit's assertions see the [PHPUnit manual](http://www.phpunit.de/manual/current/en/api.html#api.assert). For more information on PHPUnit's assertions see the [PHPUnit manual](http://www.phpunit.de/manual/current/en/api.html#api.assert).
[/info] [/info]