Skip to content

Commit ede5f52

Browse files
committed
update tests to use the new error assertion helper
1 parent 4b9645f commit ede5f52

File tree

2 files changed

+18
-41
lines changed

2 files changed

+18
-41
lines changed

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()

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;
@@ -546,29 +547,19 @@ public function testAutowire()
546547
*/
547548
public function testAliasDefinitionContainsUnsupportedElements()
548549
{
549-
$container = new ContainerBuilder();
550-
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
550+
$deprecations = array(
551+
'Using the attribute "class" is deprecated for alias definition "bar"',
552+
'Using the element "tag" is deprecated for alias definition "bar"',
553+
'Using the element "factory" is deprecated for alias definition "bar"',
554+
);
551555

552-
$deprecations = array();
553-
set_error_handler(function ($type, $msg) use (&$deprecations) {
554-
if (E_USER_DEPRECATED !== $type) {
555-
restore_error_handler();
556+
ErrorAssert::assertDeprecationsAreTriggered($deprecations, function () {
557+
$container = new ContainerBuilder();
558+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
556559

557-
return call_user_func_array('PHPUnit_Util_ErrorHandler::handleError', func_get_args());
558-
}
560+
$loader->load('legacy_invalid_alias_definition.xml');
559561

560-
$deprecations[] = $msg;
562+
$this->assertTrue($container->has('bar'));
561563
});
562-
563-
$loader->load('legacy_invalid_alias_definition.xml');
564-
565-
restore_error_handler();
566-
567-
$this->assertTrue($container->has('bar'));
568-
569-
$this->assertCount(3, $deprecations);
570-
$this->assertContains('Using the attribute "class" is deprecated for alias definition "bar"', $deprecations[0]);
571-
$this->assertContains('Using the element "tag" is deprecated for alias definition "bar"', $deprecations[1]);
572-
$this->assertContains('Using the element "factory" is deprecated for alias definition "bar"', $deprecations[2]);
573564
}
574565
}

0 commit comments

Comments
 (0)