From 4e8ef5a791923ce5f32f13cdfaf09109e4f77d63 Mon Sep 17 00:00:00 2001 From: Hrvoje Jukic Date: Thu, 17 Jun 2021 09:49:57 +0200 Subject: [PATCH] Implemented PhpUnitDecorator --- .gitignore | 3 ++ README.md | 21 +++++++++++++ composer.json | 31 +++++++++++++++++++ .../PhpUnitV1/PhpUnitDecorator.php | 28 +++++++++++++++++ .../PhpUnitV1/PhpUnitDecorator.service.yml | 6 ++++ .../PhpUnitV1/README.md | 18 +++++++++++ 6 files changed, 107 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/ThrowableDiagnosticV1Decorators/PhpUnitV1/PhpUnitDecorator.php create mode 100644 src/ThrowableDiagnosticV1Decorators/PhpUnitV1/PhpUnitDecorator.service.yml create mode 100644 src/ThrowableDiagnosticV1Decorators/PhpUnitV1/README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b3c4903 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.idea/ +/vendor/ +composer.lock diff --git a/README.md b/README.md new file mode 100644 index 0000000..e1687cf --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# Throwable Diagnostic PHPUnit Component + +Utilities for testing software using [ThrowableDiagnosticComponent](https://github.com/neighborhoods/ThrowableDiagnosticComponent). + +* Prevent diagnosis of failed assertions +* Mocks for commonly injected classes + +## Install + +Via Composer as development dependency +```bash +composer require --dev neighborhoods/throwable-diagnostic-phpunit-component +``` + +## PHPUnit Decorator + +Failed [PHPUnit](https://github.com/sebastianbergmann/phpunit) assertions raise Exceptions. Throwable Diagnostic Component disturbs the Exception based communication between a test case and the PHPUnit framework. The [PHPUnit decorator](src/ThrowableDiagnosticV1Decorators/PhpUnitV1/README.md) prevents Throwable Diagnostic Component from interfering with PHPUnit Exceptions. + +## Mocks + +TBD diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..595c428 --- /dev/null +++ b/composer.json @@ -0,0 +1,31 @@ +{ + "name": "neighborhoods/throwable-diagnostic-phpunit-component", + "description": "Utilities for testing software using ThrowableDiagnosticComponent.", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Hrvoje Jukic", + "email": "hrcajuka@gmail.com" + } + ], + "keywords": [ + "phpunit", + "throwable-diagnostic-component", + "testing" + ], + "minimum-stability": "stable", + "require": { + "neighborhoods/throwable-diagnostic-component": "^4.0.3" + }, + "autoload": { + "psr-4": { + "Neighborhoods\\ThrowableDiagnosticPHPUnitComponent\\": [ + "src" + ] + } + }, + "config": { + "sort-packages": true + } +} diff --git a/src/ThrowableDiagnosticV1Decorators/PhpUnitV1/PhpUnitDecorator.php b/src/ThrowableDiagnosticV1Decorators/PhpUnitV1/PhpUnitDecorator.php new file mode 100644 index 0000000..0bcf7c6 --- /dev/null +++ b/src/ThrowableDiagnosticV1Decorators/PhpUnitV1/PhpUnitDecorator.php @@ -0,0 +1,28 @@ +getThrowableDiagnosticV1ThrowableDiagnostic()->diagnose($throwable); + + return $this; + } +} diff --git a/src/ThrowableDiagnosticV1Decorators/PhpUnitV1/PhpUnitDecorator.service.yml b/src/ThrowableDiagnosticV1Decorators/PhpUnitV1/PhpUnitDecorator.service.yml new file mode 100644 index 0000000..4f91b88 --- /dev/null +++ b/src/ThrowableDiagnosticV1Decorators/PhpUnitV1/PhpUnitDecorator.service.yml @@ -0,0 +1,6 @@ +services: + Neighborhoods\ThrowableDiagnosticPHPUnitComponent\ThrowableDiagnosticV1Decorators\PhpUnitV1\PhpUnitDecorator: + class: Neighborhoods\ThrowableDiagnosticPHPUnitComponent\ThrowableDiagnosticV1Decorators\PhpUnitV1\PhpUnitDecorator + decorates: Neighborhoods\ThrowableDiagnosticComponent\ThrowableDiagnosticV1\ThrowableDiagnosticInterface + calls: + - [setThrowableDiagnosticV1ThrowableDiagnostic, ['@Neighborhoods\ThrowableDiagnosticComponentTest\ThrowableDiagnosticV1Decorators\PhpUnitV1\PhpUnitDecorator.inner']] diff --git a/src/ThrowableDiagnosticV1Decorators/PhpUnitV1/README.md b/src/ThrowableDiagnosticV1Decorators/PhpUnitV1/README.md new file mode 100644 index 0000000..65ce772 --- /dev/null +++ b/src/ThrowableDiagnosticV1Decorators/PhpUnitV1/README.md @@ -0,0 +1,18 @@ +# PHPUnit V1 +This Throwable Diagnostic Decorator helps integrate with [PHPUnit](https://github.com/sebastianbergmann/phpunit). + +An exception is thrown when a PHPUnit assertion fails. If this decorator is not part of the decorator stack, the PHPUnit exception will be diagnosed as non-transient causing the test case to error instead of failing. The error message doesn't include the assertion message, which makes it harder to understand and fix the problem. + +This implementation should work with any PHPUnit version. + +## Paths +Unlike other decorators this is applied to the core of ThrowableDiagnostic using Symfony DI. This decorator will be added to all existing decorator stacks by including the path of this decorator in the container builder. Therefore, this path should only be added in the test container. + +```php +$testContainerBuilder + // Your test container should already include the fab and src folders of ThrowableDiagnosticV1 + ->addSourcePath('vendor/neighborhoods/throwable-diagnostic-component/fab/ThrowableDiagnosticV1') + ->addSourcePath('vendor/neighborhoods/throwable-diagnostic-component/src/ThrowableDiagnosticV1') + // This will add PhpUnitV1 to all existing decorator stacks + ->addSourcePath('vendor/neighborhoods/throwable-diagnostic-phpunit-component/src/ThrowableDiagnosticV1Decorators/PhpUnitV1'); +```