8000 [PhpUnitBridge] Drop ErrorAssert in favor of @expectedDeprecation · symfony/symfony@48687eb · GitHub
[go: up one dir, main page]

Skip to content

Commit 48687eb

Browse files
[PhpUnitBridge] Drop ErrorAssert in favor of @expectedDeprecation
1 parent 4eeb1f0 commit 48687eb

File tree

16 files changed

+149
-281
lines changed

16 files changed

+149
-281
lines changed

src/Symfony/Bridge/PhpUnit/ErrorAssert.php

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/Symfony/Bridge/PhpUnit/SymfonyTestsListener.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class SymfonyTestsListener extends \PHPUnit_Framework_BaseTestListener
2525
private $skippedFile = false;
2626
private $wasSkipped = array();
2727
private $isSkipped = array();
28+
private $expectedDeprecations;
29+
private $gatheredDeprecations;
30+
private $previousErrorHandler;
2831

2932
/**
3033
* @param array $mockedNamespaces List of namespaces, indexed by mocked features (time-sensitive or dns-sensitive)
@@ -142,7 +145,7 @@ public function addSkippedTest(\PHPUnit_Framework_Test $test, \Exception $e, $ti
142145
public function startTest(\PHPUnit_Framework_Test $test)
143146
{
144147
if (-2 < $this->state && $test instanceof \PHPUnit_Framework_TestCase) {
145-
$groups = \PHPUnit_Util_Test::getGroups(get_class($test), $test->getName());
148+
$groups = \PHPUnit_Util_Test::getGroups(get_class($test), $test->getName(false));
146149

147150
if (in_array('time-sensitive', $groups, true)) {
148151
ClockMock::register(get_class($test));
@@ -151,13 +154,37 @@ public function startTest(\PHPUnit_Framework_Test $test)
151154
if (in_array('dns-sensitive', $groups, true)) {
152155
DnsMock::register(get_class($test));
153156
}
157+
158+
$annotations = \PHPUnit_Util_Test::parseTestMethodAnnotations(get_class($test), $test->getName(false));
159+
160+
if (isset($annotations['class']['expectedDeprecation'])) {
161+
$test->getTestResultObject()->addError($test, new \PHPUnit_Framework_AssertionFailedError('`@expectedDeprecation` annotations are not allowed at the class level.'), 0);
162+
}
163+
if (isset($annotations['method']['expectedDeprecation'])) {
164+
if (!in_array('legacy', $groups, true)) {
165+
$test->getTestResultObject()->addError($test, new \PHPUnit_Framework_AssertionFailedError('Only tests with the `@group legacy` annotation can have `@expectedDeprecation`.'), 0);
166+
}
167+
$this->expectedDeprecations = $annotations['method']['expectedDeprecation'];
168+
$this->previousErrorHandler = set_error_handler(array($this, 'handleError'));
169+
}
154170
}
155171
}
156172

157173
public function endTest(\PHPUnit_Framework_Test $test, $time)
158174
{
175+
if ($this->expectedDeprecations) {
176+
restore_error_handler();
177+
try {
178+
$prefix = "@expectedDeprecation:\n ";
179+
$test->assertStringMatchesFormat($prefix.implode("\n ", $this->expectedDeprecations), $prefix.implode("\n ", $this->gatheredDeprecations));
180+
} catch (\PHPUnit_Framework_AssertionFailedError $e) {
181+
$test->getTestResultObject()->addFailure($test, $e, $time);
182+
}
183+
184+
$this->expectedDeprecations = $this->gatheredDeprecations = $this->previousErrorHandler = null;
185+
}
159186
if (-2 < $this->state && $test instanceof \PHPUnit_Framework_TestCase) {
160-
$groups = \PHPUnit_Util_Test::getGroups(get_class($test), $test->getName());
187+
$groups = \PHPUnit_Util_Test::getGroups(get_class($test), $test->getName(false));
161188

162189
if (in_array('time-sensitive', $groups, true)) {
163190
ClockMock::withClockMock(false);
@@ -167,4 +194,17 @@ public function endTest(\PHPUnit_Framework_Test $test, $time)
167194
}
168195
}
169196
}
197+
198+
public function handleError($type, $msg, $file, $line, $context)
199+
{
200+
if (E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) {
201+
$h = $this->previousErrorHandler;
202+
203+
return $h ? $h($type, $msg, $file, $line, $context) : false;
204+
}
205+
if (error_reporting()) {
206+
$msg = 'Unsilenced deprecation: '.$msg;
207+
}
208+
$this->gatheredDeprecations[] = $msg;
209+
}
170210
}

src/Symfony/Bridge/PhpUnit/bin/simple-phpunit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

1313
// Please update when phpunit needs to be reinstalled with fresh deps:
14-
// Cache-Id-Version: 2016-09-12 09:00 UTC
14+
// Cache-Id-Version: 2016-10-20 14:00 UTC
1515

1616
error_reporting(-1);
1717

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection;
1313

14-
use Symfony\Bridge\PhpUnit\ErrorAssert;
1514
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1615
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
1716
use Symfony\Component\Cache\Adapter\ApcuAdapter;
@@ -566,19 +565,17 @@ public function testSerializerCacheDisabled()
566565

567566
/**
568567
* @group legacy
569-
* @requires function Symfony\Bridge\PhpUnit\ErrorAssert::assertDeprecationsAreTriggered
568+
* @expectedDeprecation The "framework.serializer.cache" option is deprecated %s.
570569
*/
571570
public function testDeprecatedSerializerCacheOption()
572571
{
573-
ErrorAssert::assertDeprecationsAreTriggered('The "framework.serializer.cache" option is deprecated', function () {
574-
$container = $this->createContainerFromFile('serializer_legacy_cache', array('kernel.debug' => true, 'kernel.container_class' => __CLASS__));
572+
$container = $this->createContainerFromFile('serializer_legacy_cache', array('kernel.debug' => true, 'kernel.container_class' => __CLASS__));
575573

576-
$this->assertFalse($container->hasDefinition('serializer.mapping.cache_class_metadata_factory'));
577-
$this->assertTrue($container->hasDefinition('serializer.mapping.class_metadata_factory'));
574+
$this->assertFalse($container->hasDefinition('serializer.mapping.cache_class_metadata_factory'));
575+
$this->assertTrue($container->hasDefinition('serializer.mapping.class_metadata_factory'));
578576

579-
$cache = $container->getDefinition('serializer.mapping.class_metadata_factory')->getArgument(1);
580-
$this->assertEquals(new Reference('foo'), $cache);
581-
});
577+
$cache = $container->getDefinition('serializer.mapping.class_metadata_factory')->getArgument(1);
578+
$this->assertEquals(new Reference('foo'), $cache);
582579
}
583580

584581
public function testAssetHelperWhenAssetsAreEnabled()

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplateNameParserTest.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating;
1313

14-
use Symfony\Bridge\PhpUnit\ErrorAssert;
1514
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1615
use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser;
1716
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
@@ -86,17 +85,15 @@ public function testParseValidNameWithNotFoundBundle()
8685
/**
8786
* @group legacy
8887
* @dataProvider provideAbsolutePaths
89-
* @requires function Symfony\Bridge\PhpUnit\ErrorAssert::assertDeprecationsAreTriggered
88+
* @expectedDeprecation Absolute template path support is deprecated since Symfony 3.1 and will be removed in 4.0.
9089
*/
9190
public function testAbsolutePathsAreDeprecated($name, $logicalName, $path, $ref)
9291
{
93-
ErrorAssert::assertDeprecationsAreTriggered('Absolute template path support is deprecated since Symfony 3.1 and will be removed in 4.0.', function () use ($name, $logicalName, $path, $ref) {
94-
$template = $this->parser->parse($name);
92+
$template = $this->parser->parse($name);
9593

96-
$this->assertSame($ref->getLogicalName(), $template->getLogicalName());
97-
$this->assertSame($logicalName, $template->getLogicalName());
98-
$this->assertSame($path, $template->getPath());
99-
});
94+
$this->assertSame($ref->getLogicalName(), $template->getLogicalName());
95+
$this->assertSame($logicalName, $template->getLogicalName());
96+
$this->assertSame($path, $template->getPath());
10097
}
10198

10299
public function provideAbsolutePaths()

src/Symfony/Component/Console/Formatter/OutputFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ private function createStyleFromString($string)
221221
try {
222222
$style->setOption($option);
223223
} catch (\InvalidArgumentException $e) {
224-
trigger_error(sprintf('Unknown style options are deprecated since version 3.2 and will be removed in 4.0. Exception "%s".', $e->getMessage()), E_USER_DEPRECATED);
224+
@trigger_error(sprintf('Unknown style options are deprecated since version 3.2 and will be removed in 4.0. Exception "%s".', $e->getMessage()), E_USER_DEPRECATED);
225225

226226
return false;
227227
}

src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Console\Tests\Formatter;
1313

14-
use Symfony\Bridge\PhpUnit\ErrorAssert;
1514
use Symfony\Component\Console\Formatter\OutputFormatter;
1615
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
1716

@ 8000 @ -196,17 +195,12 @@ public function provideInlineStyleOptionsCases()
196195
/**
197196
* @group legacy
198197
* @dataProvider provideInlineStyleTagsWithUnknownOptions
199-
* @requires function Symfony\Bridge\PhpUnit\ErrorAssert::assertDeprecationsAreTriggered
198+
* @expectedDeprecation Unknown style options are deprecated since version 3.2 and will be removed in 4.0. Exception "Invalid option specified: "%s". Expected one of (bold, underscore, blink, reverse, conceal)".
200199
*/
201200
public function testInlineStyleOptionsUnknownAreDeprecated($tag, $option)
202201
{
203-
ErrorAssert::assertDeprecationsAreTriggered(
204-
array(sprintf('Unknown style options are deprecated since version 3.2 and will be removed in 4.0. Exception "Invalid option specified: "%s". Expected one of (bold, underscore, blink, reverse, conceal)".', $option)),
205-
function () use ($tag) {
206-
$formatter = new OutputFormatter(true);
207-
$formatter->format($tag);
208-
}
209-
);
202+
$formatter = new OutputFormatter(true);
203+
$formatter->format($tag);
210204
}
211205

212206
public function provideInlineStyleTagsWithUnknownOptions()

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
require_once __DIR__.'/Fixtures/includes/classes.php';
1515
require_once __DIR__.'/Fixtures/includes/ProjectExtension.php';
1616

17-
use Symfony\Bridge\PhpUnit\ErrorAssert;
1817
use Symfony\Component\Config\Resource\ResourceInterface;
1918
use Symfony\Component\DependencyInjection\Alias;
2019
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
@@ -60,19 +59,18 @@ public function testDefinitions()
6059
}
6160

6261
/**
63-
* @requires function Symfony\Bridge\PhpUnit\ErrorAssert::assertDeprecationsAreTriggered
62+
* @group legacy
63+
* @expectedDeprecation The "deprecated_foo" service is deprecated. You should stop using it, as it will soon be removed.
6464
*/
6565
public function testCreateDeprecatedService()
6666
{
67-
ErrorAssert::assertDeprecationsAreTriggered('The "deprecated_foo" service is deprecated. You should stop using it, as it will soon be removed.', function () {
68-
$definition = new Definition('stdClass');
69-
$definition->setDeprecated(true);
67+
$definition = new Definition('stdClass');
68+
$definition->setDeprecated(true);
7069

71-
$builder = new ContainerBuilder();
72-
$builder->setDefinition('deprecated_foo', $definition);
73-
$builder->compile();
74-
$builder->get('deprecated_foo');
75-
});
70+
$builder = new ContainerBuilder();
71+
$builder->setDefinition('deprecated_foo', $definition);
72+
$builder->compile();
73+
$builder->get('deprecated_foo');
7674
}
7775

7876
public function testRegister()

0 commit comments

Comments
 (0)
0