This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveNamedArgumentsPassTest.php

184 lines
6.7 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
2017-02-20 13:56:45 +00:00
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Compiler\ResolveNamedArgumentsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
bug #29697 [DI] Fixed wrong factory method in exception (Wojciech Gorczyca) This PR was submitted for the 4.2 branch but it was merged into the 4.1 branch instead (closes #29697). Discussion ---------- [DI] Fixed wrong factory method in exception | Q | A | ------------- | --- | Branch? | 4.2 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #29678 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | n/a <!-- required for new features --> When a service definition with a factory defines invalid arguments, the [resulting exception message ](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php#L70)incorrectly specifies the factory constructor instead of the factory method as not having the specified named arguments. <!-- Write a short README entry for your feature/bugfix here (replace this comment block.) This will help people understand your PR and can be used as a start of the Doc PR. Additionally: - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the master branch. --> Commits ------- 922885c892 [DI] Fixed wrong factory method in exception
2019-01-05 07:48:11 +00:00
use Symfony\Component\DependencyInjection\Tests\Fixtures\FactoryDummy;
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
use Symfony\Component\DependencyInjection\Tests\Fixtures\SimilarArgumentsDummy;
bug #29697 [DI] Fixed wrong factory method in exception (Wojciech Gorczyca) This PR was submitted for the 4.2 branch but it was merged into the 4.1 branch instead (closes #29697). Discussion ---------- [DI] Fixed wrong factory method in exception | Q | A | ------------- | --- | Branch? | 4.2 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #29678 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | n/a <!-- required for new features --> When a service definition with a factory defines invalid arguments, the [resulting exception message ](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php#L70)incorrectly specifies the factory constructor instead of the factory method as not having the specified named arguments. <!-- Write a short README entry for your feature/bugfix here (replace this comment block.) This will help people understand your PR and can be used as a start of the Doc PR. Additionally: - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the master branch. --> Commits ------- 922885c892 [DI] Fixed wrong factory method in exception
2019-01-05 07:48:11 +00:00
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1;
/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
2017-02-20 13:56:45 +00:00
class ResolveNamedArgumentsPassTest extends TestCase
{
public function testProcess()
{
$container = new ContainerBuilder();
$definition = $container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class);
$definition->setArguments(array(
2 => 'http://api.example.com',
'$apiKey' => '123',
0 => new Reference('foo'),
));
$definition->addMethodCall('setApiKey', array('$apiKey' => '123'));
$pass = new ResolveNamedArgumentsPass();
$pass->process($container);
$this->assertEquals(array(0 => new Reference('foo'), 1 => '123', 2 => 'http://api.example.com'), $definition->getArguments());
$this->assertEquals(array(array('setApiKey', array('123'))), $definition->getMethodCalls());
}
public function testWithFactory()
{
$container = new ContainerBuilder();
$container->register('factory', NoConstructor::class);
$definition = $container->register('foo', NoConstructor::class)
->setFactory(array(new Reference('factory'), 'create'))
->setArguments(array('$apiKey' => '123'));
$pass = new ResolveNamedArgumentsPass();
$pass->process($container);
$this->assertSame(array(0 => '123'), $definition->getArguments());
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
*/
public function testClassNull()
{
$container = new ContainerBuilder();
$definition = $container->register(NamedArgumentsDummy::class);
$definition->setArguments(array('$apiKey' => '123'));
$pass = new ResolveNamedArgumentsPass();
$pass->process($container);
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
*/
public function testClassNotExist()
{
$container = new ContainerBuilder();
$definition = $container->register(NotExist::class, NotExist::class);
$definition->setArguments(array('$apiKey' => '123'));
$pass = new ResolveNamedArgumentsPass();
$pass->process($container);
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
*/
public function testClassNoConstructor()
{
$container = new ContainerBuilder();
$definition = $container->register(NoConstructor::class, NoConstructor::class);
$definition->setArguments(array('$apiKey' => '123'));
$pass = new ResolveNamedArgumentsPass();
$pass->process($container);
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
bug #29697 [DI] Fixed wrong factory method in exception (Wojciech Gorczyca) This PR was submitted for the 4.2 branch but it was merged into the 4.1 branch instead (closes #29697). Discussion ---------- [DI] Fixed wrong factory method in exception | Q | A | ------------- | --- | Branch? | 4.2 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #29678 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | n/a <!-- required for new features --> When a service definition with a factory defines invalid arguments, the [resulting exception message ](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php#L70)incorrectly specifies the factory constructor instead of the factory method as not having the specified named arguments. <!-- Write a short README entry for your feature/bugfix here (replace this comment block.) This will help people understand your PR and can be used as a start of the Doc PR. Additionally: - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the master branch. --> Commits ------- 922885c892 [DI] Fixed wrong factory method in exception
2019-01-05 07:48:11 +00:00
* @expectedExceptionMessage Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy": method "__construct()" has no argument named "$notFound". Check your service definition.
*/
public function testArgumentNotFound()
{
$container = new ContainerBuilder();
$definition = $container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class);
$definition->setArguments(array('$notFound' => '123'));
$pass = new ResolveNamedArgumentsPass();
$pass->process($container);
}
bug #29697 [DI] Fixed wrong factory method in exception (Wojciech Gorczyca) This PR was submitted for the 4.2 branch but it was merged into the 4.1 branch instead (closes #29697). Discussion ---------- [DI] Fixed wrong factory method in exception | Q | A | ------------- | --- | Branch? | 4.2 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #29678 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | n/a <!-- required for new features --> When a service definition with a factory defines invalid arguments, the [resulting exception message ](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php#L70)incorrectly specifies the factory constructor instead of the factory method as not having the specified named arguments. <!-- Write a short README entry for your feature/bugfix here (replace this comment block.) This will help people understand your PR and can be used as a start of the Doc PR. Additionally: - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the master branch. --> Commits ------- 922885c892 [DI] Fixed wrong factory method in exception
2019-01-05 07:48:11 +00:00
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
2019-01-05 12:04:54 +00:00
* @expectedExceptionMessage Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1": method "Symfony\Component\DependencyInjection\Tests\Fixtures\FactoryDummy::createTestDefinition1()" has no argument named "$notFound". Check your service definition.
bug #29697 [DI] Fixed wrong factory method in exception (Wojciech Gorczyca) This PR was submitted for the 4.2 branch but it was merged into the 4.1 branch instead (closes #29697). Discussion ---------- [DI] Fixed wrong factory method in exception | Q | A | ------------- | --- | Branch? | 4.2 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #29678 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | n/a <!-- required for new features --> When a service definition with a factory defines invalid arguments, the [resulting exception message ](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php#L70)incorrectly specifies the factory constructor instead of the factory method as not having the specified named arguments. <!-- Write a short README entry for your feature/bugfix here (replace this comment block.) This will help people understand your PR and can be used as a start of the Doc PR. Additionally: - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the master branch. --> Commits ------- 922885c892 [DI] Fixed wrong factory method in exception
2019-01-05 07:48:11 +00:00
*/
public function testCorrectMethodReportedInException()
{
$container = new ContainerBuilder();
$container->register(FactoryDummy::class, FactoryDummy::class);
$definition = $container->register(TestDefinition1::class, TestDefinition1::class);
2019-01-05 12:04:54 +00:00
$definition->setFactory(array(FactoryDummy::class, 'createTestDefinition1'));
bug #29697 [DI] Fixed wrong factory method in exception (Wojciech Gorczyca) This PR was submitted for the 4.2 branch but it was merged into the 4.1 branch instead (closes #29697). Discussion ---------- [DI] Fixed wrong factory method in exception | Q | A | ------------- | --- | Branch? | 4.2 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #29678 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | n/a <!-- required for new features --> When a service definition with a factory defines invalid arguments, the [resulting exception message ](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php#L70)incorrectly specifies the factory constructor instead of the factory method as not having the specified named arguments. <!-- Write a short README entry for your feature/bugfix here (replace this comment block.) This will help people understand your PR and can be used as a start of the Doc PR. Additionally: - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the master branch. --> Commits ------- 922885c892 [DI] Fixed wrong factory method in exception
2019-01-05 07:48:11 +00:00
$definition->setArguments(array('$notFound' => '123'));
$pass = new ResolveNamedArgumentsPass();
$pass->process($container);
}
public function testTypedArgument()
{
$container = new ContainerBuilder();
$definition = $container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class);
$definition->setArguments(array('$apiKey' => '123', CaseSensitiveClass::class => new Reference('foo')));
$pass = new ResolveNamedArgumentsPass();
$pass->process($container);
$this->assertEquals(array(new Reference('foo'), '123'), $definition->getArguments());
}
public function testResolvesMultipleArgumentsOfTheSameType()
{
$container = new ContainerBuilder();
$definition = $container->register(SimilarArgumentsDummy::class, SimilarArgumentsDummy::class);
$definition->setArguments(array(CaseSensitiveClass::class => new Reference('foo'), '$token' => 'qwerty'));
$pass = new ResolveNamedArgumentsPass();
$pass->process($container);
$this->assertEquals(array(new Reference('foo'), 'qwerty', new Reference('foo')), $definition->getArguments());
}
public function testResolvePrioritizeNamedOverType()
{
$container = new ContainerBuilder();
$definition = $container->register(SimilarArgumentsDummy::class, SimilarArgumentsDummy::class);
$definition->setArguments(array(CaseSensitiveClass::class => new Reference('foo'), '$token' => 'qwerty', '$class1' => new Reference('bar')));
$pass = new ResolveNamedArgumentsPass();
$pass->process($container);
$this->assertEquals(array(new Reference('bar'), 'qwerty', new Reference('foo')), $definition->getArguments());
}
}
class NoConstructor
{
public static function create($apiKey)
{
}
}