Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a8a2989

Browse files
committed
Document how to integrate with PHPUnit
Right now, it is possible to display Doctrine deprecations with a bit of PHPUnit configuration. Note that this does not seem to clash with triggering self-deprecations, provided you use the VerifyDeprecations trait in tests that call the deprecated pieces of code.
1 parent 0d0c25d commit a8a2989

3 files changed

Lines changed: 86 additions & 3 deletions

File tree

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
@@ -226,19 +226,19 @@ private static function basename(string $filename): string
226226

227227
public static function enableTrackingDeprecations(): void
228228
{
229-
self::$type = self::$type ?? 0;
229+
self::$type = self::$type ?? self::getTypeFromEnv();
230230
self::$type |= self::TYPE_TRACK_DEPRECATIONS;
231231
}
232232

233233
public static function enableWithTriggerError(): void
234234
{
235-
self::$type = self::$type ?? 0;
235+
self::$type = self::$type ?? self::getTypeFromEnv();
236236
self::$type |= self::TYPE_TRIGGER_ERROR;
237237
}
238238

239239
public static function enableWithPsrLogger(LoggerInterface $logger): void
240240
{
241-
self::$type = self::$type ?? 0;
241+
self::$type = self::$type ?? self::getTypeFromEnv();
242242
self::$type |= self::TYPE_PSR_LOGGER;
243243
self::$logger = $logger;
244244
}

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)