Skip to content

Commit

Permalink
Merge pull request #583 from creative-commoners/pulls/6/phpunit11
Browse files Browse the repository at this point in the history
DOC Update PHPUnit code sample
  • Loading branch information
GuySartorelli authored Sep 18, 2024
2 parents a29b31b + c26b2a7 commit e5f48ed
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 3 additions & 2 deletions en/02_Developer_Guides/04_Configuration/00_Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,17 +400,18 @@ Note that the configuration change is active only within the callback function.
namespace App\Test\Service;
use App\Service\MyService;
use PHPUnit\Framework\Attributes\DataProvider;
use SilverStripe\Config\Collections\MutableConfigCollectionInterface;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
class MyServiceTest extends SapphireTest
{
/**
* @dataProvider testValuesProvider
* @param string $value
* @param string $expected
*/
#[DataProvider('provideConfigValues')]
public function testConfigValues($value, $expected)
{
$result = Config::withConfig(function (MutableConfigCollectionInterface $config) use ($value) {
Expand All @@ -427,7 +428,7 @@ class MyServiceTest extends SapphireTest
$this->assertEquals($expected, $result);
}
public function testValuesProvider(): array
public function provideConfigValues(): array
{
return [
['test value 1', 'expected value 1'],
Expand Down
14 changes: 13 additions & 1 deletion en/02_Developer_Guides/06_Testing/00_Unit_Testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ summary: Test models, database logic and your object methods.

# Unit and integration testing

A Unit Test is an automated piece of code that invokes a unit of work in the application and then checks the behavior
A unit test is an automated piece of code that invokes a unit of work in the application and then checks the behavior
to ensure that it works as it should. A simple example would be to test the result of a PHP method.

```php
Expand Down Expand Up @@ -208,6 +208,18 @@ class MyTest extends SapphireTest
}
```

### Asserting errors, warnings and notices

The `phpunit` no longer supports the ability to assert expected errors, warning and notices. Calling [`SapphireTest::enableErrorHandler()`](api:SilverStripe\Dev\SapphireTest::enableErrorHandler()) at beginning on your unit test, or in the `setUp()` method, will use a custom error handler to catch any errors, warning and notices and re-throw them as exceptions which can then be asserted by calling `$this->expectException(<exception-class>);`. The following error types are converted to the following exceptions:

| Error types | Exception |
| ----------- | --------- |
| E_USER_ERROR, E_RECOVERABLE_ERROR | [`ExpectedErrorException`](api:SilverStripe\Dev\Exceptions\ExpectedErrorException) |
| E_NOTICE, E_USER_NOTICE | [`ExpectedNoticeException`](api:SilverStripe\Dev\Exceptions\ExpectedNoticeException) |
| E_WARNING, E_USER_WARNING | [`ExpectedWarningException`](api:SilverStripe\Dev\Exceptions\ExpectedWarningException) |

All other error types are not converted to exceptions and are handled by the default PHP error handler.

## Related documentation

- [How to Write a SapphireTest](how_tos/write_a_sapphiretest)
Expand Down

0 comments on commit e5f48ed

Please sign in to comment.