|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Polyfill\Tests\Php85; |
| 13 | + |
| 14 | +use Attribute; |
| 15 | +use DelayedTargetValidation; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use ReflectionClass; |
| 18 | +use ReflectionClassConstant; |
| 19 | +use ReflectionFunction; |
| 20 | +use ReflectionMethod; |
| 21 | +use ReflectionParameter; |
| 22 | +use ReflectionProperty; |
| 23 | + |
| 24 | +#[DelayedTargetValidation] |
| 25 | +class HasAttribute { |
| 26 | + #[DelayedTargetValidation] |
| 27 | + private $prop; |
| 28 | + |
| 29 | + #[DelayedTargetValidation] |
| 30 | + public const FOO = 'BAR'; |
| 31 | + |
| 32 | + #[DelayedTargetValidation] |
| 33 | + public function __construct( |
| 34 | + #[DelayedTargetValidation] $param |
| 35 | + ) {} |
| 36 | +} |
| 37 | + |
| 38 | +#[DelayedTargetValidation] |
| 39 | +function globalFunc() {} |
| 40 | + |
| 41 | +/** |
| 42 | + * @author Daniel Scherzer <[email protected]> |
| 43 | + * @requires PHP >= 8.0 |
| 44 | + */ |
| 45 | +class DelayedTargetValidationTest extends TestCase |
| 46 | +{ |
| 47 | + public function testAttributeAttribute() |
| 48 | + { |
| 49 | + $ref = new ReflectionClass(\DelayedTargetValidation::class); |
| 50 | + $attributes = $ref->getAttributes(); |
| 51 | + $this->assertCount(1, $attributes); |
| 52 | + $attribute = $attributes[0]; |
| 53 | + $this->assertSame('Attribute', $attribute->getName()); |
| 54 | + $args = $attribute->getArguments(); |
| 55 | + $this->assertCount(1, $args); |
| 56 | + $this->assertSame(Attribute::TARGET_ALL, $args[0]); |
| 57 | + $this->expectException(\ReflectionException::class); |
| 58 | + $this->expectExceptionMessage('Constant "missing" does not exist'); |
| 59 | + new \ReflectionConstant('missing'); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @dataProvider provideReflectionInstances |
| 64 | + */ |
| 65 | + public function testTargetValidation($reflectionSource) |
| 66 | + { |
| 67 | + $attributes = $reflectionSource->getAttributes(); |
| 68 | + $this->assertCount(1, $attributes); |
| 69 | + $attrib = $attributes[0]; |
| 70 | + $this->assertSame('DelayedTargetValidation', $attrib->getName()); |
| 71 | + $this->assertSame([], $attrib->getArguments()); |
| 72 | + $this->assertInstanceOf( |
| 73 | + DelayedTargetValidation::class, |
| 74 | + $attrib->newInstance() |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + public static function provideReflectionInstances() { |
| 79 | + yield 'Class' => [ new ReflectionClass(HasAttribute::class) ]; |
| 80 | + yield 'Property' => [ new ReflectionProperty(HasAttribute::class, 'prop')]; |
| 81 | + yield 'Class constant' => [ new ReflectionClassConstant(HasAttribute::class, 'FOO')]; |
| 82 | + yield 'Method' => [ new ReflectionMethod(HasAttribute::class, '__construct')]; |
| 83 | + yield 'Parameter' => [ |
| 84 | + new ReflectionParameter([HasAttribute::class, '__construct'], 'param') |
| 85 | + ]; |
| 86 | + yield 'Function' => [ new ReflectionFunction(__NAMESPACE__ . '\\globalFunc') ]; |
| 87 | + } |
| 88 | +} |
0 commit comments