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

Skip to content

[PhpUnitBridge] Add the ability to expect a deprecation inside a test #35192

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

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
1 change: 1 addition & 0 deletions src/Symfony/Bridge/PhpUnit/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* ignore verbosity settings when the build fails because of deprecations
* added per-group verbosity
* added `ExpectDeprecationTrait` to be able to define an expected deprecation from inside a test

5.0.0
-----
Expand Down
31 changes: 31 additions & 0 deletions src/Symfony/Bridge/PhpUnit/ExpectDeprecationTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\PhpUnit;

use Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait;

trait ExpectDeprecationTrait
{
/**
* @param string $message
*
* @return void
*/
protected function expectDeprecation($message)
{
if (!SymfonyTestsListenerTrait::$previousErrorHandler) {
SymfonyTestsListenerTrait::$previousErrorHandler = set_error_handler([SymfonyTestsListenerTrait::class, 'handleError']);
}

SymfonyTestsListenerTrait::$expectedDeprecations[] = $message;
}
}
53 changes: 31 additions & 22 deletions src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@

use Doctrine\Common\Annotations\AnnotationRegistry;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\RiskyTestError;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestSuite;
use PHPUnit\Runner\BaseTestRunner;
use PHPUnit\Util\Blacklist;
use PHPUnit\Util\Test;
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Bridge\PhpUnit\DnsMock;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Debug\DebugClassLoader as LegacyDebugClassLoader;
use Symfony\Component\ErrorHandler\DebugClassLoader;

Expand All @@ -32,16 +34,16 @@
*/
class SymfonyTestsListenerTrait
{
public static $expectedDeprecations = [];
public static $previousErrorHandler;
private static $gatheredDeprecations = [];
private static $globallyEnabled = false;
private $state = -1;
private $skippedFile = false;
private $wasSkipped = [];
private $isSkipped = [];
private $expectedDeprecations = [];
private $gatheredDeprecations = [];
private $previousErrorHandler;
private $error;
private $runsInSeparateProcess = false;
private $checkNumAssertions = false;

/**
* @param array $mockedNamespaces List of namespaces, indexed by mocked features (time-sensitive or dns-sensitive)
Expand Down Expand Up @@ -220,15 +222,17 @@ public function startTest($test)
if (isset($annotations['class']['expectedDeprecation'])) {
$test->getTestResultObject()->addError($test, new AssertionFailedError('`@expectedDeprecation` annotations are not allowed at the class level.'), 0);
}
if (isset($annotations['method']['expectedDeprecation'])) {
if (!\in_array('legacy', $groups, true)) {
$this->error = new AssertionFailedError('Only tests with the `@group legacy` annotation can have `@expectedDeprecation`.');
if (isset($annotations['method']['expectedDeprecation']) || $this->checkNumAssertions = \in_array(ExpectDeprecationTrait::class, class_uses($test), true)) {
if (isset($annotations['method']['expectedDeprecation'])) {
self::$expectedDeprecations = $annotations['method']['expectedDeprecation'];
self::$previousErrorHandler = set_error_handler([self::class, 'handleError']);
}

$test->getTestResultObject()->beStrictAboutTestsThatDoNotTestAnything(false);
if ($this->checkNumAssertions) {
$this->checkNumAssertions = $test->getTestResultObject()->isStrictAboutTestsThatDoNotTestAnything() && !$test->doesNotPerformAssertions();
}

$this->expectedDeprecations = $annotations['method']['expectedDeprecation'];
$this->previousErrorHandler = set_error_handler([$this, 'handleError']);
$test->getTestResultObject()->beStrictAboutTestsThatDoNotTestAnything(false);
}
}
}
Expand All @@ -242,9 +246,12 @@ public function endTest($test, $time)
$className = \get_class($test);
$groups = Test::getGroups($className, $test->getName(false));

if ($errored = null !== $this->error) {
$test->getTestResultObject()->addError($test, $this->error, 0);
$this->error = null;
if ($this->checkNumAssertions) {
if (!self::$expectedDeprecations && !$test->getNumAssertions()) {
$test->getTestResultObject()->addFailure($test, new RiskyTestError('This test did not perform any assertions'), $time);
}

$this->checkNumAssertions = false;
}

if ($this->runsInSeparateProcess) {
Expand All @@ -263,24 +270,26 @@ public function endTest($test, $time)
$this->runsInSeparateProcess = false;
}

if ($this->expectedDeprecations) {
if (self::$expectedDeprecations) {
if (!\in_array($test->getStatus(), [BaseTestRunner::STATUS_SKIPPED, BaseTestRunner::STATUS_INCOMPLETE], true)) {
$test->addToAssertionCount(\count($this->expectedDeprecations));
$test->addToAssertionCount(\count(self::$expectedDeprecations));
}

restore_error_handler();

if (!$errored && !\in_array($test->getStatus(), [BaseTestRunner::STATUS_SKIPPED, BaseTestRunner::STATUS_INCOMPLETE, BaseTestRunner::STATUS_FAILURE, BaseTestRunner::STATUS_ERROR], true)) {
if (!\in_array('legacy', $groups, true)) {
$test->getTestResultObject()->addError($test, new AssertionFailedError('Only tests with the `@group legacy` annotation can expect a deprecation.'), 0);
} elseif (!\in_array($test->getStatus(), [BaseTestRunner::STATUS_SKIPPED, BaseTestRunner::STATUS_INCOMPLETE, BaseTestRunner::STATUS_FAILURE, BaseTestRunner::STATUS_ERROR], true)) {
try {
$prefix = "@expectedDeprecation:\n";
$test->assertStringMatchesFormat($prefix.'%A '.implode("\n%A ", $this->expectedDeprecations)."\n%A", $prefix.' '.implode("\n ", $this->gatheredDeprecations)."\n");
$test->assertStringMatchesFormat($prefix.'%A '.implode("\n%A ", self::$expectedDeprecations)."\n%A", $prefix.' '.implode("\n ", self::$gatheredDeprecations)."\n");
} catch (AssertionFailedError $e) {
$test->getTestResultObject()->addFailure($test, $e, $time);
}
}

$this->expectedDeprecations = $this->gatheredDeprecations = [];
$this->previousErrorHandler = null;
self::$expectedDeprecations = self::$gatheredDeprecations = [];
self::$previousErrorHandler = null;
}
if (!$this->runsInSeparateProcess && -2 < $this->state && ($test instanceof \PHPUnit\Framework\TestCase || $test instanceof TestCase)) {
if (\in_array('time-sensitive', $groups, true)) {
Expand All @@ -292,10 +301,10 @@ public function endTest($test, $time)
}
}

public function handleError($type, $msg, $file, $line, $context = [])
public static function handleError($type, $msg, $file, $line, $context = [])
{
if (E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) {
$h = $this->previousErrorHandler;
$h = self::$previousErrorHandler;

return $h ? $h($type, $msg, $file, $line, $context) : false;
}
Expand All @@ -308,7 +317,7 @@ public function handleError($type, $msg, $file, $line, $context = [])
if (error_reporting()) {
$msg = 'Unsilenced deprecation: '.$msg;
}
$this->gatheredDeprecations[] = $msg;
self::$gatheredDeprecations[] = $msg;

return null;
}
Expand Down
76 changes: 76 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Tests/ExpectDeprecationTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\PhpUnit\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;

final class ExpectDeprecationTraitTest extends TestCase
{
use ExpectDeprecationTrait;

/**
* Do not remove this test in the next major version.
*
* @group legacy
*/
public function testOne()
{
$this->expectDeprecation('foo');
@trigger_error('foo', E_USER_DEPRECATED);
}

/**
* Do not remove this test in the next major version.
*
* @group legacy
*/
public function testMany()
{
$this->expectDeprecation('foo');
$this->expectDeprecation('bar');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To actually test that the feature is working, we would also need to write tests where expectDeprecation is called but not deprecation is triggered, to show that the test fail in such case.

Of course, this must not be part of the main Symfony testsuite run (here, you are writing tests as if they were testing trigger_error, not as if they were testing the bridge), but as phpt tests which can assert that running these tests report a failure.

@trigger_error('foo', E_USER_DEPRECATED);
@trigger_error('bar', E_USER_DEPRECATED);
}

/**
* Do not remove this test in the next major version.
*
* @group legacy
*
* @expectedDeprecation foo
*/
public function testOneWithAnnotation()
{
$this->expectDeprecation('bar');
@trigger_error('foo', E_USER_DEPRECATED);
@trigger_error('bar', E_USER_DEPRECATED);
}

/**
* Do not remove this test in the next major version.
*
* @group legacy
*
* @expectedDeprecation foo
* @expectedDeprecation bar
*/
public function testManyWithAnnotation()
{
$this->expectDeprecation('ccc');
$this->expectDeprecation('fcy');
@trigger_error('foo', E_USER_DEPRECATED);
@trigger_error('bar', E_USER_DEPRECATED);
@trigger_error('ccc', E_USER_DEPRECATED);
@trigger_error('fcy', E_USER_DEPRECATED);
}
}