ArgumentMetadataFactoryTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\Controller\BasicTypesController;
  16. use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\NullableController;
  17. use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\VariadicController;
  18. class ArgumentMetadataFactoryTest extends TestCase
  19. {
  20. /**
  21. * @var ArgumentMetadataFactory
  22. */
  23. private $factory;
  24. protected function setUp()
  25. {
  26. $this->factory = new ArgumentMetadataFactory();
  27. }
  28. public function testSignature1()
  29. {
  30. $arguments = $this->factory->createArgumentMetadata(array($this, 'signature1'));
  31. $this->assertEquals(array(
  32. new ArgumentMetadata('foo', self::class, false, false, null),
  33. new ArgumentMetadata('bar', 'array', false, false, null),
  34. new ArgumentMetadata('baz', 'callable', false, false, null),
  35. ), $arguments);
  36. }
  37. public function testSignature2()
  38. {
  39. $arguments = $this->factory->createArgumentMetadata(array($this, 'signature2'));
  40. $this->assertEquals(array(
  41. new ArgumentMetadata('foo', self::class, false, true, null, true),
  42. new ArgumentMetadata('bar', __NAMESPACE__.'\FakeClassThatDoesNotExist', false, true, null, true),
  43. new ArgumentMetadata('baz', 'Fake\ImportedAndFake', false, true, null, true),
  44. ), $arguments);
  45. }
  46. public function testSignature3()
  47. {
  48. $arguments = $this->factory->createArgumentMetadata(array($this, 'signature3'));
  49. $this->assertEquals(array(
  50. new ArgumentMetadata('bar', __NAMESPACE__.'\FakeClassThatDoesNotExist', false, false, null),
  51. new ArgumentMetadata('baz', 'Fake\ImportedAndFake', false, false, null),
  52. ), $arguments);
  53. }
  54. public function testSignature4()
  55. {
  56. $arguments = $this->factory->createArgumentMetadata(array($this, 'signature4'));
  57. $this->assertEquals(array(
  58. new ArgumentMetadata('foo', null, false, true, 'default'),
  59. new ArgumentMetadata('bar', null, false, true, 500),
  60. new ArgumentMetadata('baz', null, false, true, array()),
  61. ), $arguments);
  62. }
  63. public function testSignature5()
  64. {
  65. $arguments = $this->factory->createArgumentMetadata(array($this, 'signature5'));
  66. $this->assertEquals(array(
  67. new ArgumentMetadata('foo', 'array', false, true, null, true),
  68. new ArgumentMetadata('bar', null, false, false, null),
  69. ), $arguments);
  70. }
  71. /**
  72. * @requires PHP 5.6
  73. */
  74. public function testVariadicSignature()
  75. {
  76. $arguments = $this->factory->createArgumentMetadata(array(new VariadicController(), 'action'));
  77. $this->assertEquals(array(
  78. new ArgumentMetadata('foo', null, false, false, null),
  79. new ArgumentMetadata('bar', null, true, false, null),
  80. ), $arguments);
  81. }
  82. /**
  83. * @requires PHP 7.0
  84. */
  85. public function testBasicTypesSignature()
  86. {
  87. $arguments = $this->factory->createArgumentMetadata(array(new BasicTypesController(), 'action'));
  88. $this->assertEquals(array(
  89. new ArgumentMetadata('foo', 'string', false, false, null),
  90. new ArgumentMetadata('bar', 'int', false, false, null),
  91. new ArgumentMetadata('baz', 'float', false, false, null),
  92. ), $arguments);
  93. }
  94. /**
  95. * @requires PHP 7.1
  96. */
  97. public function testNullableTypesSignature()
  98. {
  99. $arguments = $this->factory->createArgumentMetadata(array(new NullableController(), 'action'));
  100. $this->assertEquals(array(
  101. new ArgumentMetadata('foo', 'string', false, false, null, true),
  102. new ArgumentMetadata('bar', \stdClass::class, false, false, null, true),
  103. new ArgumentMetadata('baz', 'string', false, true, 'value', true),
  104. new ArgumentMetadata('mandatory', null, false, false, null, true),
  105. ), $arguments);
  106. }
  107. private function signature1(ArgumentMetadataFactoryTest $foo, array $bar, callable $baz)
  108. {
  109. }
  110. private function signature2(ArgumentMetadataFactoryTest $foo = null, FakeClassThatDoesNotExist $bar = null, ImportedAndFake $baz = null)
  111. {
  112. }
  113. private function signature3(FakeClassThatDoesNotExist $bar, ImportedAndFake $baz)
  114. {
  115. }
  116. private function signature4($foo = 'default', $bar = 500, $baz = array())
  117. {
  118. }
  119. private function signature5(array $foo = null, $bar)
  120. {
  121. }
  122. }