ArgumentMetadataFactoryTest.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. public function testVariadicSignature()
  72. {
  73. $arguments = $this->factory->createArgumentMetadata(array(new VariadicController(), 'action'));
  74. $this->assertEquals(array(
  75. new ArgumentMetadata('foo', null, false, false, null),
  76. new ArgumentMetadata('bar', null, true, false, null),
  77. ), $arguments);
  78. }
  79. public function testBasicTypesSignature()
  80. {
  81. $arguments = $this->factory->createArgumentMetadata(array(new BasicTypesController(), 'action'));
  82. $this->assertEquals(array(
  83. new ArgumentMetadata('foo', 'string', false, false, null),
  84. new ArgumentMetadata('bar', 'int', false, false, null),
  85. new ArgumentMetadata('baz', 'float', false, false, null),
  86. ), $arguments);
  87. }
  88. public function testNullableTypesSignature()
  89. {
  90. $arguments = $this->factory->createArgumentMetadata(array(new NullableController(), 'action'));
  91. $this->assertEquals(array(
  92. new ArgumentMetadata('foo', 'string', false, false, null, true),
  93. new ArgumentMetadata('bar', \stdClass::class, false, false, null, true),
  94. new ArgumentMetadata('baz', 'string', false, true, 'value', true),
  95. new ArgumentMetadata('mandatory', null, false, false, null, true),
  96. ), $arguments);
  97. }
  98. private function signature1(self $foo, array $bar, callable $baz)
  99. {
  100. }
  101. private function signature2(self $foo = null, FakeClassThatDoesNotExist $bar = null, ImportedAndFake $baz = null)
  102. {
  103. }
  104. private function signature3(FakeClassThatDoesNotExist $bar, ImportedAndFake $baz)
  105. {
  106. }
  107. private function signature4($foo = 'default', $bar = 500, $baz = array())
  108. {
  109. }
  110. private function signature5(array $foo = null, $bar)
  111. {
  112. }
  113. }