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

Skip to content

Add ability to set expected deprecation message inside a test #25757

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

Closed
wants to merge 7 commits into from
Closed
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: 5 additions & 0 deletions src/Symfony/Bridge/PhpUnit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.1.0
-----

* support for adding expected deprecations via `SymfonyTestsListenerTrait::expectDeprecation()`
Copy link
Member

Choose a reason for hiding this comment

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

What about adding a new class to make this a bit more friendly,
eg SymfonyTestHelper::expectDeprecation() at the root of the bridge's namespace?

Copy link
Contributor

Choose a reason for hiding this comment

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

Calling anything on a trait definitely smells.
Keeping it in sync with $this->expectDeprecation() called inside tests would be the best imho.


4.0.0
-----

Expand Down
54 changes: 35 additions & 19 deletions src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class SymfonyTestsListenerTrait
private $skippedFile = false;
private $wasSkipped = array();
private $isSkipped = array();
private $expectedDeprecations = array();
private $gatheredDeprecations = array();
private $previousErrorHandler;
private static $expectedDeprecations = array();
private static $gatheredDeprecations = array();
private static $previousErrorHandler;
private $testsWithWarnings;
private $reportUselessTests;
private $error;
Expand Down Expand Up @@ -214,14 +214,8 @@ public function startTest($test)
$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`.');
}

$test->getTestResultObject()->beStrictAboutTestsThatDoNotTestAnything(false);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've moved this to end tests but because we reset the value using $this->reportUselessTests this code does not seem to have the intended affect and we still to add $this->addToAssertionCount(1); to the test to make it not risky.


$this->expectedDeprecations = $annotations['method']['expectedDeprecation'];
$this->previousErrorHandler = set_error_handler(array($this, 'handleError'));
static::$expectedDeprecations = $annotations['method']['expectedDeprecation'];
static::$previousErrorHandler = set_error_handler(array(__CLASS__, 'handleError'));
}
}
}
Expand All @@ -239,15 +233,24 @@ public function endTest($test, $time)
$Test = 'PHPUnit_Util_Test';
$BaseTestRunner = 'PHPUnit_Runner_BaseTestRunner';
$Warning = 'PHPUnit_Framework_Warning';
$AssertionFailedError = 'PHPUnit_Framework_AssertionFailedError';
} else {
$Test = 'PHPUnit\Util\Test';
$BaseTestRunner = 'PHPUnit\Runner\BaseTestRunner';
$Warning = 'PHPUnit\Framework\Warning';
$AssertionFailedError = 'PHPUnit\Framework\AssertionFailedError';
}
$className = get_class($test);
$classGroups = $Test::getGroups($className);
$groups = $Test::getGroups($className, $test->getName(false));

if (static::$expectedDeprecations) {
$test->getTestResultObject()->beStrictAboutTestsThatDoNotTestAnything(false);
if (!in_array('legacy', $groups, true)) {
$this->error = new $AssertionFailedError('Only tests with the `@group legacy` annotation can have `@expectedDeprecation`.');
}
}

if (null !== $this->reportUselessTests) {
$test->getTestResultObject()->beStrictAboutTestsThatDoNotTestAnything($this->reportUselessTests);
$this->reportUselessTests = null;
Expand All @@ -273,26 +276,26 @@ public function endTest($test, $time)
$this->runsInSeparateProcess = false;
}

if ($this->expectedDeprecations) {
if (static::$expectedDeprecations) {
if (!in_array($test->getStatus(), array($BaseTestRunner::STATUS_SKIPPED, $BaseTestRunner::STATUS_INCOMPLETE), true)) {
$test->addToAssertionCount(count($this->expectedDeprecations));
$test->addToAssertionCount(count(static::$expectedDeprecations));
}

restore_error_handler();

if (!$errored && !in_array($test->getStatus(), array($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 ", static::$expectedDeprecations)."\n%A", $prefix.' '.implode("\n ", static::$gatheredDeprecations)."\n");
} catch (AssertionFailedError $e) {
$test->getTestResultObject()->addFailure($test, $e, $time);
} catch (\PHPUnit_Framework_AssertionFailedError $e) {
$test->getTestResultObject()->addFailure($test, $e, $time);
}
}

$this->expectedDeprecations = $this->gatheredDeprecations = array();
$this->previousErrorHandler = null;
static::$expectedDeprecations = static::$gatheredDeprecations = array();
static::$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 @@ -304,10 +307,10 @@ public function endTest($test, $time)
}
}

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

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

/**
Expand All @@ -339,4 +342,17 @@ private function willBeIsolated($test)

return $r->getValue($test);
}

/**
* Sets an expected deprecation message.
*
* @param string $message Deprecation message to expect
*/
public static function expectDeprecation($message)
{
if (!static::$previousErrorHandler) {
static::$previousErrorHandler = set_error_handler(array(__CLASS__, 'handleError'));
}
static::$expectedDeprecations[] = $message;
}
}
21 changes: 21 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Tests/ExpectDeprecationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Symfony\Bridge\PhpUnit\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait;

/**
* Don't remove this test case, it tests the SymfonyTestsListenerTrait.
*
* @group legacy
*/
class ExpectDeprecationTest extends TestCase
{
public function testExpectDeprecation()
{
SymfonyTestsListenerTrait::expectDeprecation('Test abc');
@trigger_error('Test abc', E_USER_DEPRECATED);
$this->addToAssertionCount(1);
}
}