ResolveNamedArgumentsPassTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Tests\Compiler;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\Compiler\ResolveNamedArgumentsPass;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
  16. /**
  17. * @autor ThurData <info@thurdata.ch>
  18. */
  19. class ResolveNamedArgumentsPassTest extends TestCase
  20. {
  21. public function testProcess()
  22. {
  23. $container = new ContainerBuilder();
  24. $definition = $container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class);
  25. $definition->setArguments(array(
  26. 2 => 'http://api.example.com',
  27. '$apiKey' => '123',
  28. 0 => new Reference('foo'),
  29. ));
  30. $definition->addMethodCall('setApiKey', array('$apiKey' => '123'));
  31. $pass = new ResolveNamedArgumentsPass();
  32. $pass->process($container);
  33. $this->assertEquals(array(0 => new Reference('foo'), 1 => '123', 2 => 'http://api.example.com'), $definition->getArguments());
  34. $this->assertEquals(array(array('setApiKey', array('123'))), $definition->getMethodCalls());
  35. }
  36. public function testWithFactory()
  37. {
  38. $container = new ContainerBuilder();
  39. $container->register('factory', NoConstructor::class);
  40. $definition = $container->register('foo', NoConstructor::class)
  41. ->setFactory(array(new Reference('factory'), 'create'))
  42. ->setArguments(array('$apiKey' => '123'));
  43. $pass = new ResolveNamedArgumentsPass();
  44. $pass->process($container);
  45. $this->assertSame(array(0 => '123'), $definition->getArguments());
  46. }
  47. /**
  48. * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
  49. */
  50. public function testClassNull()
  51. {
  52. $container = new ContainerBuilder();
  53. $definition = $container->register(NamedArgumentsDummy::class);
  54. $definition->setArguments(array('$apiKey' => '123'));
  55. $pass = new ResolveNamedArgumentsPass();
  56. $pass->process($container);
  57. }
  58. /**
  59. * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
  60. */
  61. public function testClassNotExist()
  62. {
  63. $container = new ContainerBuilder();
  64. $definition = $container->register(NotExist::class, NotExist::class);
  65. $definition->setArguments(array('$apiKey' => '123'));
  66. $pass = new ResolveNamedArgumentsPass();
  67. $pass->process($container);
  68. }
  69. /**
  70. * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
  71. */
  72. public function testClassNoConstructor()
  73. {
  74. $container = new ContainerBuilder();
  75. $definition = $container->register(NoConstructor::class, NoConstructor::class);
  76. $definition->setArguments(array('$apiKey' => '123'));
  77. $pass = new ResolveNamedArgumentsPass();
  78. $pass->process($container);
  79. }
  80. /**
  81. * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  82. */
  83. public function testArgumentNotFound()
  84. {
  85. $container = new ContainerBuilder();
  86. $definition = $container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class);
  87. $definition->setArguments(array('$notFound' => '123'));
  88. $pass = new ResolveNamedArgumentsPass();
  89. $pass->process($container);
  90. }
  91. }
  92. class NoConstructor
  93. {
  94. public static function create($apiKey)
  95. {
  96. }
  97. }