No Description

ArgumentMetadataFactoryTest.php 4.9KB

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