From c26b2a7b1d4704cad6d2508ed7d3625dda1a65ba Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Wed, 18 Sep 2024 11:31:55 +1200 Subject: [PATCH] DOC Update PHPUnit code sample --- .../04_Configuration/00_Configuration.md | 5 +++-- .../06_Testing/00_Unit_Testing.md | 14 +++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/en/02_Developer_Guides/04_Configuration/00_Configuration.md b/en/02_Developer_Guides/04_Configuration/00_Configuration.md index 505ba5185..beba44424 100644 --- a/en/02_Developer_Guides/04_Configuration/00_Configuration.md +++ b/en/02_Developer_Guides/04_Configuration/00_Configuration.md @@ -400,6 +400,7 @@ 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; @@ -407,10 +408,10 @@ 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) { @@ -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'], diff --git a/en/02_Developer_Guides/06_Testing/00_Unit_Testing.md b/en/02_Developer_Guides/06_Testing/00_Unit_Testing.md index 405c5194c..a7c9cdd5a 100644 --- a/en/02_Developer_Guides/06_Testing/00_Unit_Testing.md +++ b/en/02_Developer_Guides/06_Testing/00_Unit_Testing.md @@ -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 @@ -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();`. 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)