Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC Update PHPUnit code sample #583

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading