Skip to content

Commit b8fcced

Browse files
committed
add a triggered errors assertion helper
1 parent 93139f6 commit b8fcced

File tree

8 files changed

+84
-133
lines changed

8 files changed

+84
-133
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Symfony\Bridge\PhpUnit;
4+
5+
/**
6+
* Test that your code triggers expected error messages.
7+
*
8+
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
9+
*/
10+
final class ErrorAssert
11+
{
12+
public static function assertDeprecationsAreTriggered($expectedMessages, callable $testCode)
13+
{
14+
self::assertErrorsAreTriggered(E_USER_DEPRECATED, $expectedMessages, $testCode);
15+
}
16+
17+
public static function assertErrorsAreTriggered($expectedType, $expectedMessages, callable $testCode)
18+
{
19+
$triggeredMessages = array();
20+
21+
set_error_handler(function ($type, $message) use ($expectedType, &$triggeredMessages) {
22+
if ($expectedType !== $type) {
23+
restore_error_handler();
24+
25+
return call_user_func_array('PHPUnit_Util_ErrorHandler::handleError', func_get_args());
26+
}
27+
28+
$triggeredMessages[] = $message;
29+
});
30+
31+
$testCode();
32+
33+
restore_error_handler();
34+
35+
\PHPUnit_Framework_Assert::assertCount(count($expectedMessages), $triggeredMessages);
36+
37+
for ($i = 0; $i < count($triggeredMessages); ++$i) {
38+
\PHPUnit_Framework_Assert::assertContains($expectedMessages[$i], $triggeredMessages[$i]);
39+
}
40+
}
41+
}

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

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

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

14+
use Symfony\Bridge\PhpUnit\ErrorAssert;
1415
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1516
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
1617
use Symfony\Component\Cache\Adapter\ApcuAdapter;
@@ -541,25 +542,12 @@ public function testSerializerCacheDisabled()
541542
*/
542543
public function testDeprecatedSerializerCacheOption()
543544
{
544-
$deprecations = array();
545-
set_error_handler(function ($type, $msg) use (&$deprecations) {
546-
if (E_USER_DEPRECATED !== $type) {
547-
restore_error_handler();
545+
ErrorAssert::assertDeprecationsAreTriggered('The "framework.serializer.cache" option is deprecated', function () {
546+
$container = $this->createContainerFromFile('serializer_legacy_cache', array('kernel.debug' => true, 'kernel.container_class' => __CLASS__));
548547

549-
return call_user_func_array('PHPUnit_Util_ErrorHandler::handleError', func_get_args());
550-
}
551-
552-
$deprecations[] = $msg;
548+
$this->assertFalse($container->hasDefinition('serializer.mapping.cache_class_metadata_factory'));
549+
$this->assertEquals(new Reference('foo'), $container->getDefinition('serializer.mapping.class_metadata_factory')->getArgument(1));
553550
});
554-
555-
$container = $this->createContainerFromFile('serializer_legacy_cache', array('kernel.debug' => true, 'kernel.container_class' => __CLASS__));
556-
557-
restore_error_handler();
558-
559-
$this->assertCount(1, $deprecations);
560-
$this->assertContains('The "framework.serializer.cache" option is deprecated', $deprecations[0]);
561-
$this->assertFalse($container->hasDefinition('serializer.mapping.cache_class_metadata_factory'));
562-
$this->assertEquals(new Reference('foo'), $container->getDefinition('serializer.mapping.class_metadata_factory')->getArgument(1));
563551
}
564552

565553
public function testAssetHelperWhenAssetsAreEnabled()

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

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

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

14+
use Symfony\Bridge\PhpUnit\ErrorAssert;
1415
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1516
use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser;
1617
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
@@ -88,26 +89,13 @@ public function testParseValidNameWithNotFoundBundle()
8889
*/
8990
public function testAbsolutePathsAreDeprecated($name, $logicalName, $path, $ref)
9091
{
91-
$deprecations = array();
92-
set_error_handler(function ($type, $msg) use (&$deprecations) {
93-
if (E_USER_DEPRECATED !== $type) {
94-
restore_error_handler();
92+
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) {
93+
$template = $this->parser->parse($name);
9594

96-
return call_user_func_array('PHPUnit_Util_ErrorHandler::handleError', func_get_args());
97-
}
98-
99-
$deprecations[] = $msg;
95+
$this->assertSame($ref->getLogicalName(), $template->getLogicalName());
96+
$this->assertSame($logicalName, $template->getLogicalName());
97+
$this->assertSame($path, $template->getPath());
10098
});
101-
102-
$template = $this->parser->parse($name);
103-
104-
restore_error_handler();
105-
106-
$this->assertSame($ref->getLogicalName(), $template->getLogicalName());
107-
$this->assertSame($logicalName, $template->getLogicalName());
108-
$this->assertSame($path, $template->getPath());
109-
$this->assertCount(1, $deprecations);
110-
$this->assertContains('Absolute template path support is deprecated since Symfony 3.1 and will be removed in 4.0.', $deprecations[0]);
11199
}
112100

113101
public function provideAbsolutePaths()

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

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

17+
use Symfony\Bridge\PhpUnit\ErrorAssert;
1718
use Symfony\Component\Config\Resource\ResourceInterface;
1819
use Symfony\Component\DependencyInjection\Alias;
1920
use Symfony\Component\DependencyInjection\ContainerBuilder;
2021
use Symfony\Component\DependencyInjection\ContainerInterface;
2122
use Symfony\Component\DependencyInjection\Definition;
2223
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
23-
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
2424
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
2525
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
2626
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
@@ -60,28 +60,14 @@ public function testDefinitions()
6060

6161
public function testCreateDeprecatedService()
6262
{
63-
$deprecations = array();
64-
set_error_handler(function ($type, $msg) use (&$deprecations) {
65-
if (E_USER_DEPRECATED !== $type) {
66-
restore_error_handler();
63+
ErrorAssert::assertDeprecationsAreTriggered('The "deprecated_foo" service is deprecated. You should stop using it, as it will soon be removed.', function () {
64+
$definition = new Definition('stdClass');
65+
$definition->setDeprecated(true);
6766

68-
return call_user_func_array('PHPUnit_Util_ErrorHandler::handleError', func_get_args());
69-
}
70-
71-
$deprecations[] = $msg;
67+
$builder = new ContainerBuilder();
68+
$builder->setDefinition('deprecated_foo', $definition);
69+
$builder->get('deprecated_foo');
7270
});
73-
74-
$definition = new Definition('stdClass');
75-
$definition->setDeprecated(true);
76-
77-
$builder = new ContainerBuilder();
78-
$builder->setDefinition('deprecated_foo', $definition);
79-
$builder->get('deprecated_foo');
80-
81-
restore_error_handler();
82-
83-
$this->assertCount(1, $deprecations);
84-
$this->assertContains('The "deprecated_foo" service is deprecated. You should stop using it, as it will soon be removed.', $deprecations[0]);
8571
}
8672

8773
public function testRegister()

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

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

1212
namespace Symfony\Component\DependencyInjection\Tests\Loader;
1313

14+
use Symfony\Bridge\PhpUnit\ErrorAssert;
1415
use Symfony\Component\DependencyInjection\ContainerInterface;
1516
use Symfony\Component\DependencyInjection\ContainerBuilder;
1617
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
@@ -533,29 +534,19 @@ public function testAutowire()
533534
*/
534535
public function testAliasDefinitionContainsUnsupportedElements()
535536
{
536-
$container = new ContainerBuilder();
537-
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
537+
$deprecations = array(
538+
'Using the attribute "class" is deprecated for alias definition "bar"',
539+
'Using the element "tag" is deprecated for alias definition "bar"',
540+
'Using the element "factory" is deprecated for alias definition "bar"',
541+
);
538542

539-
$deprecations = array();
540-
set_error_handler(function ($type, $msg) use (&$deprecations) {
541-
if (E_USER_DEPRECATED !== $type) {
542-
restore_error_handler();
543+
ErrorAssert::assertDeprecationsAreTriggered($deprecations, function () {
544+
$container = new ContainerBuilder();
545+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
543546

544-
return call_user_func_array('PHPUnit_Util_ErrorHandler::handleError', func_get_args());
545-
}
547+
$loader->load('legacy_invalid_alias_definition.xml');
546548

547-
$deprecations[] = $msg;
549+
$this->assertTrue($container->has('bar'));
548550
});
549-
550-
$loader->load('legacy_invalid_alias_definition.xml');
551-
552-
restore_error_handler();
553-
554-
$this->assertTrue($container->has('bar'));
555-
556-
$this->assertCount(3, $deprecations);
557-
$this->assertContains('Using the attribute "class" is deprecated for alias definition "bar"', $deprecations[0]);
558-
$this->assertContains('Using the element "tag" is deprecated for alias definition "bar"', $deprecations[1]);
559-
$this->assertContains('Using the element "factory" is deprecated for alias definition "bar"', $deprecations[2]);
560551
}
561552
}

src/Symfony/Component/HttpKernel/Tests/Fragment/EsiFragmentRendererTest.php

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

1212
namespace Symfony\Component\HttpKernel\Tests\Fragment;
1313

14+
use Symfony\Bridge\PhpUnit\ErrorAssert;
1415
use Symfony\Component\HttpKernel\Controller\ControllerReference;
1516
use Symfony\Component\HttpKernel\Fragment\EsiFragmentRenderer;
1617
use Symfony\Component\HttpKernel\HttpCache\Esi;
@@ -30,29 +31,12 @@ public function testRenderFallbackToInlineStrategyIfEsiNotSupported()
3031
*/
3132
public function testRenderFallbackWithObjectAttributesIsDeprecated()
3233
{
33-
$deprecations = array();
34-
set_error_handler(function ($type, $message) use (&$deprecations) {
35-
if (E_USER_DEPRECATED !== $type) {
36-
restore_error_handler();
37-
38-
return call_user_func_array('PHPUnit_Util_ErrorHandler::handleError', func_get_args());
39-
}
40-
41-
$deprecations[] = $message;
34+
ErrorAssert::assertDeprecationsAreTriggered('Passing objects as part of URI attributes to the ESI and SSI rendering strategies is deprecated', function () {
35+
$strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(true), new UriSigner('foo'));
36+
$request = Request::create('/');
37+
$reference = new ControllerReference('main_controller', array('foo' => array('a' => array(), 'b' => new \stdClass())), array());
38+
$strategy->render($reference, $request);
4239
});
43-
44-
$strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(true), new UriSigner('foo'));
45-
46-
$request = Request::create('/');
47-
48-
$reference = new ControllerReference('main_controller', array('foo' => array('a' => array(), 'b' => new \stdClass())), array());
49-
50-
$strategy->render($reference, $request);
51-
52-
restore_error_handler();
53-
54-
$this->assertCount(1, $deprecations);
55-
$this->assertContains('Passing objects as part of URI attributes to the ESI and SSI rendering strategies is deprecated', $deprecations[0]);
5640
}
5741

5842
public function testRender()

src/Symfony/Component/Yaml/Tests/InlineTest.php

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

1212
namespace Symfony\Component\Yaml\Tests;
1313

14+
use Symfony\Bridge\PhpUnit\ErrorAssert;
1415
use Symfony\Component\Yaml\Inline;
1516
use Symfony\Component\Yaml\Yaml;
1617

@@ -258,23 +259,9 @@ public function getScalarIndicators()
258259
*/
259260
public function testParseUnquotedScalarStartingWithPercentCharacter()
260261
{
261-
$deprecations = array();
262-
set_error_handler(function ($type, $msg) use (&$deprecations) {
263-
if (E_USER_DEPRECATED !== $type) {
264-
restore_error_handler();
265-
266-
return call_user_func_array('PHPUnit_Util_ErrorHandler::handleError', func_get_args());
267-
}
268-
269-
$deprecations[] = $msg;
262+
ErrorAssert::assertDeprecationsAreTriggered('Not quoting a scalar starting with the "%" indicator character is deprecated since Symfony 3.1 and will throw a ParseException in 4.0.', function () {
263+
Inline::parse('{ foo: %foo }');
270264
});
271-
272-
Inline::parse('{ foo: %foo }');
273-
274-
restore_error_handler();
275-
276-
$this->assertCount(1, $deprecations);
277-
$this->assertContains('Not quoting a scalar starting with the "%" indicator character is deprecated since Symfony 3.1 and will throw a ParseException in 4.0.', $deprecations[0]);
278265
}
279266

280267
public function getTestsForParse()

src/Symfony/Component/Yaml/Tests/ParserTest.php

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

1212
namespace Symfony\Component\Yaml\Tests;
1313

14+
use Symfony\Bridge\PhpUnit\ErrorAssert;
1415
use Symfony\Component\Yaml\Yaml;
1516
use Symfony\Component\Yaml\Parser;
1617

@@ -33,27 +34,12 @@ protected function tearDown()
3334
*/
3435
public function testSpecifications($file, $expected, $yaml, $comment, $deprecated)
3536
{
36-
$deprecations = array();
37-
3837
if ($deprecated) {
39-
set_error_handler(function ($type, $msg) use (&$deprecations) {
40-
if (E_USER_DEPRECATED !== $type) {
41-
restore_error_handler();
42-
43-
return call_user_func_array('PHPUnit_Util_ErrorHandler::handleError', func_get_args());
44-
}
45-
46-
$deprecations[] = $msg;
38+
ErrorAssert::assertDeprecationsAreTriggered('Using the comma as a group separator for floats is deprecated since version 3.2 and will be removed in 4.0.', function () use ($expected, $yaml, $comment) {
39+
$this->assertEquals($expected, var_export($this->parser->parse($yaml), true), $comment);
4740
});
48-
}
49-
50-
$this->assertEquals($expected, var_export($this->parser->parse($yaml), true), $comment);
51-
52-
if ($deprecated) {
53-
restore_error_handler();
54-
55-
$this->assertCount(1, $deprecations);
56-
$this->assertContains('Using the comma as a group separator for floats is deprecated since version 3.2 and will be removed in 4.0.', $deprecations[0]);
41+
} else {
42+
$this->assertEquals($expected, var_export($this->parser->parse($yaml), true), $comment);
5743
}
5844
}
5945

0 commit comments

Comments
 (0)