ArgumentMetadataFactoryTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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\HttpKernel\Tests\ControllerMetadata;
  11. use Fake\ImportedAndFake;
  12. use PHPUnit\Framework\TestCase;
  13. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
  14. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory;
  15. use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\Foo;
  16. use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\AttributeController;
  17. use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\BasicTypesController;
  18. use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\NullableController;
  19. use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\VariadicController;
  20. class ArgumentMetadataFactoryTest extends TestCase
  21. {
  22. /**
  23. * @var ArgumentMetadataFactory
  24. */
  25. private $factory;
  26. protected function setUp(): void
  27. {
  28. $this->factory = new ArgumentMetadataFactory();
  29. }
  30. public function testSignature1()
  31. {
  32. $arguments = $this->factory->createArgumentMetadata([$this, 'signature1']);
  33. $this->assertEquals([
  34. new ArgumentMetadata('foo', self::class, false, false, null),
  35. new ArgumentMetadata('bar', 'array', false, false, null),
  36. new ArgumentMetadata('baz', 'callable', false, false, null),
  37. ], $arguments);
  38. }
  39. public function testSignature2()
  40. {
  41. $arguments = $this->factory->createArgumentMetadata([$this, 'signature2']);
  42. $this->assertEquals([
  43. new ArgumentMetadata('foo', self::class, false, true, null, true),
  44. new ArgumentMetadata('bar', FakeClassThatDoesNotExist::class, false, true, null, true),
  45. new ArgumentMetadata('baz', 'Fake\ImportedAndFake', false, true, null, true),
  46. ], $arguments);
  47. }
  48. public function testSignature3()
  49. {
  50. $arguments = $this->factory->createArgumentMetadata([$this, 'signature3']);
  51. $this->assertEquals([
  52. new ArgumentMetadata('bar', FakeClassThatDoesNotExist::class, false, false, null),
  53. new ArgumentMetadata('baz', 'Fake\ImportedAndFake', false, false, null),
  54. ], $arguments);
  55. }
  56. public function testSignature4()
  57. {
  58. $arguments = $this->factory->createArgumentMetadata([$this, 'signature4']);
  59. $this->assertEquals([
  60. new ArgumentMetadata('foo', null, false, true, 'default'),
  61. new ArgumentMetadata('bar', null, false, true, 500),
  62. new ArgumentMetadata('baz', null, false, true, []),
  63. ], $arguments);
  64. }
  65. public function testSignature5()
  66. {
  67. $arguments = $this->factory->createArgumentMetadata([$this, 'signature5']);
  68. $this->assertEquals([
  69. new ArgumentMetadata('foo', 'array', false, true, null, true),
  70. new ArgumentMetadata('bar', null, false, true, null, true),
  71. ], $arguments);
  72. }
  73. public function testVariadicSignature()
  74. {
  75. $arguments = $this->factory->createArgumentMetadata([new VariadicController(), 'action']);
  76. $this->assertEquals([
  77. new ArgumentMetadata('foo', null, false, false, null),
  78. new ArgumentMetadata('bar', null, true, false, null),
  79. ], $arguments);
  80. }
  81. public function testBasicTypesSignature()
  82. {
  83. $arguments = $this->factory->createArgumentMetadata([new BasicTypesController(), 'action']);
  84. $this->assertEquals([
  85. new ArgumentMetadata('foo', 'string', false, false, null),
  86. new ArgumentMetadata('bar', 'int', false, false, null),
  87. new ArgumentMetadata('baz', 'float', false, false, null),
  88. ], $arguments);
  89. }
  90. public function testNamedClosure()
  91. {
  92. $arguments = $this->factory->createArgumentMetadata(\Closure::fromCallable([$this, 'signature1']));
  93. $this->assertEquals([
  94. new ArgumentMetadata('foo', self::class, false, false, null),
  95. new ArgumentMetadata('bar', 'array', false, false, null),
  96. new ArgumentMetadata('baz', 'callable', false, false, null),
  97. ], $arguments);
  98. }
  99. public function testNullableTypesSignature()
  100. {
  101. $arguments = $this->factory->createArgumentMetadata([new NullableController(), 'action']);
  102. $this->assertEquals([
  103. new ArgumentMetadata('foo', 'string', false, false, null, true),
  104. new ArgumentMetadata('bar', \stdClass::class, false, false, null, true),
  105. new ArgumentMetadata('baz', 'string', false, true, 'value', true),
  106. new ArgumentMetadata('last', 'string', false, true, '', false),
  107. ], $arguments);
  108. }
  109. /**
  110. * @requires PHP 8
  111. */
  112. public function testAttributeSignature()
  113. {
  114. $arguments = $this->factory->createArgumentMetadata([new AttributeController(), 'action']);
  115. $this->assertEquals([
  116. new ArgumentMetadata('baz', 'string', false, false, null, false, [new Foo('bar')]),
  117. ], $arguments);
  118. }
  119. /**
  120. * @requires PHP 8
  121. */
  122. public function testMultipleAttributes()
  123. {
  124. $this->factory->createArgumentMetadata([new AttributeController(), 'multiAttributeArg']);
  125. $this->assertCount(1, $this->factory->createArgumentMetadata([new AttributeController(), 'multiAttributeArg'])[0]->getAttributes());
  126. }
  127. /**
  128. * @requires PHP 8
  129. */
  130. public function testIssue41478()
  131. {
  132. $arguments = $this->factory->createArgumentMetadata([new AttributeController(), 'issue41478']);
  133. $this->assertEquals([
  134. new ArgumentMetadata('baz', 'string', false, false, null, false, [new Foo('bar')]),
  135. new ArgumentMetadata('bat', 'string', false, false, null, false, []),
  136. ], $arguments);
  137. }
  138. private function signature1(self $foo, array $bar, callable $baz)
  139. {
  140. }
  141. private function signature2(self $foo = null, FakeClassThatDoesNotExist $bar = null, ImportedAndFake $baz = null)
  142. {
  143. }
  144. private function signature3(FakeClassThatDoesNotExist $bar, ImportedAndFake $baz)
  145. {
  146. }
  147. private function signature4($foo = 'default', $bar = 500, $baz = [])
  148. {
  149. }
  150. private function signature5(array $foo = null, $bar = null)
  151. {
  152. }
  153. }