ArgumentMetadataTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
  13. use Symfony\Component\HttpKernel\Attribute\ArgumentInterface;
  14. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
  15. use Symfony\Component\HttpKernel\Tests\Fixtures\Attribute\Foo;
  16. class ArgumentMetadataTest extends TestCase
  17. {
  18. use ExpectDeprecationTrait;
  19. public function testWithBcLayerWithDefault()
  20. {
  21. $argument = new ArgumentMetadata('foo', 'string', false, true, 'default value');
  22. $this->assertFalse($argument->isNullable());
  23. }
  24. public function testDefaultValueAvailable()
  25. {
  26. $argument = new ArgumentMetadata('foo', 'string', false, true, 'default value', true);
  27. $this->assertTrue($argument->isNullable());
  28. $this->assertTrue($argument->hasDefaultValue());
  29. $this->assertSame('default value', $argument->getDefaultValue());
  30. }
  31. public function testDefaultValueUnavailable()
  32. {
  33. $this->expectException(\LogicException::class);
  34. $argument = new ArgumentMetadata('foo', 'string', false, false, null, false);
  35. $this->assertFalse($argument->isNullable());
  36. $this->assertFalse($argument->hasDefaultValue());
  37. $argument->getDefaultValue();
  38. }
  39. /**
  40. * @group legacy
  41. */
  42. public function testLegacyAttribute()
  43. {
  44. $attribute = $this->createMock(ArgumentInterface::class);
  45. $this->expectDeprecation('Since symfony/http-kernel 5.3: The "Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata" constructor expects an array of PHP attributes as last argument, %s given.');
  46. $this->expectDeprecation('Since symfony/http-kernel 5.3: Method "Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata::getAttribute()" is deprecated, use "getAttributes()" instead.');
  47. $argument = new ArgumentMetadata('foo', 'string', false, true, 'default value', true, $attribute);
  48. $this->assertSame($attribute, $argument->getAttribute());
  49. }
  50. /**
  51. * @requires PHP 8
  52. */
  53. public function testGetAttributes()
  54. {
  55. $argument = new ArgumentMetadata('foo', 'string', false, true, 'default value', true, [new Foo('bar')]);
  56. $this->assertEquals([new Foo('bar')], $argument->getAttributes());
  57. }
  58. }