Skip to content

Commit 31610db

Browse files
authored
Merge pull request #66 from greg0ire/phpunit-integration
Document how to integrate with PHPUnit
2 parents 02c4605 + a8a2989 commit 31610db

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,67 @@ class MyTest extends TestCase
150150
}
151151
```
152152

153+
## Displaying deprecations after running a PHPUnit test suite
154+
155+
It is possible to integrate this library with PHPUnit to display all
156+
deprecations triggered during the test suite execution.
157+
158+
```xml
159+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
160+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
161+
colors="true"
162+
bootstrap="vendor/autoload.php"
163+
displayDetailsOnTestsThatTriggerDeprecations="true"
164+
failOnDeprecation="true"
165+
>
166+
<!-- one attribute to display the deprecations, the other to fail the test suite -->
167+
168+
<php>
169+
<!-- ensures native PHP deprecations are used -->
170+
<server name="DOCTRINE_DEPRECATIONS" value="trigger"/>
171+
</php>
172+
173+
<!-- ensures the @ operator in @trigger_error is ignored -->
174+
<source ignoreSuppressionOfDeprecations="true">
175+
<include>
176+
<directory>src</directory>
177+
</include>
178+
</source>
179+
</phpunit>
180+
```
181+
182+
Note that you can still trigger Deprecations in your code, provided you use the
183+
`#[WithoutErrorHandler]` attribute to disable PHPUnit's error handler for tests
184+
that call it. Be wary that this will disable all error handling, meaning it
185+
will mask any warnings or errors that would otherwise be caught by PHPUnit.
186+
187+
At the moment, it is not possible to disable deduplication with an environment
188+
variable, but you can use a bootstrap file to achieve that:
189+
190+
```php
191+
// tests/bootstrap.php
192+
<?php
193+
194+
declare(strict_types=1);
195+
196+
require dirname(__DIR__) . '/vendor/autoload.php';
197+
198+
use Doctrine\Deprecations\Deprecation;
199+
200+
Deprecation::withoutDeduplication();
201+
```
202+
203+
Then, reference that file in your PHPUnit configuration:
204+
205+
```xml
206+
<phpunit
207+
bootstrap="tests/bootstrap.php"
208+
209+
>
210+
211+
</phpunit>
212+
```
213+
153214
## What is a deprecation identifier?
154215

155216
An identifier for deprecations is just a link to any resource, most often a

src/Deprecation.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,19 +224,19 @@ private static function basename(string $filename): string
224224

225225
public static function enableTrackingDeprecations(): void
226226
{
227-
self::$type = self::$type ?? 0;
227+
self::$type = self::$type ?? self::getTypeFromEnv();
228228
self::$type |= self::TYPE_TRACK_DEPRECATIONS;
229229
}
230230

231231
public static function enableWithTriggerError(): void
232232
{
233-
self::$type = self::$type ?? 0;
233+
self::$type = self::$type ?? self::getTypeFromEnv();
234234
self::$type |= self::TYPE_TRIGGER_ERROR;
235235
}
236236

237237
public static function enableWithPsrLogger(LoggerInterface $logger): void
238238
{
239-
self::$type = self::$type ?? 0;
239+
self::$type = self::$type ?? self::getTypeFromEnv();
240240
self::$type |= self::TYPE_PSR_LOGGER;
241241
self::$logger = $logger;
242242
}

tests/EnvTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Deprecations;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use ReflectionProperty;
9+
10+
class EnvTest extends TestCase
11+
{
12+
public function testEnvIsTakenIntoAccountWhenCallingEnableTrackingDeprecations(): void
13+
{
14+
$_ENV['DOCTRINE_DEPRECATIONS'] = 'trigger';
15+
Deprecation::enableTrackingDeprecations();
16+
$reflectionProperty = new ReflectionProperty(Deprecation::class, 'type');
17+
$reflectionProperty->setAccessible(true);
18+
self::assertSame(1 | 2, $reflectionProperty->getValue());
19+
unset($_ENV['DOCTRINE_DEPRECATIONS']);
20+
$reflectionProperty->setValue(null, null);
21+
}
22+
}

0 commit comments

Comments
 (0)