-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
87e2c94
Add ability to set expected deprecation message inside a test
alexpott 884b57c
Coding standards
alexpott 50608cc
Coding standards
alexpott 444909a
Change to expectDeprecation() to match PHPUnit 6
alexpott d663d82
There are 2 too many coding standards in my world :)
alexpott 915aa9e
Prefer variable names to not be shortened
alexpott da0d168
Add CHANGELOG
alexpott File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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->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')); | ||
} | ||
} | ||
} | ||
|
@@ -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; | ||
|
@@ -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)) { | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
@@ -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
21
src/Symfony/Bridge/PhpUnit/Tests/ExpectDeprecationTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.