1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\Annotations\Assembler\Constant; |
6
|
|
|
|
7
|
|
|
use DateTimeImmutable; |
8
|
|
|
use DateTimeInterface; |
9
|
|
|
use Doctrine\Annotations\Assembler\Constant\Exception\ClassConstantNotFound; |
10
|
|
|
use Doctrine\Annotations\Assembler\Constant\Exception\ConstantNotAccessible; |
11
|
|
|
use Doctrine\Annotations\Assembler\Constant\Exception\InvalidClass; |
12
|
|
|
use Doctrine\Annotations\Assembler\Constant\ReflectionConstantResolver; |
13
|
|
|
use Doctrine\Annotations\Metadata\Reflection\DefaultReflectionProvider; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use function sprintf; |
16
|
|
|
use const PHP_EOL; |
17
|
|
|
use const PHP_VERSION_ID; |
18
|
|
|
|
19
|
|
|
final class ReflectionConstantResolverTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
/** @var ReflectionConstantResolver */ |
22
|
|
|
private $resolver; |
23
|
|
|
|
24
|
|
|
protected function setUp() : void |
25
|
|
|
{ |
26
|
|
|
$this->resolver = new ReflectionConstantResolver(new DefaultReflectionProvider()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param mixed $value |
31
|
|
|
* |
32
|
|
|
* @dataProvider validStandaloneConstantsProvider() |
33
|
|
|
*/ |
34
|
|
|
public function testValidStandaloneConstant(string $constantName, $value) : void |
35
|
|
|
{ |
36
|
|
|
self::assertSame($value, $this->resolver->resolveStandaloneConstant($constantName)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return mixed[][] |
41
|
|
|
*/ |
42
|
|
|
public function validStandaloneConstantsProvider() : iterable |
43
|
|
|
{ |
44
|
|
|
yield ['PHP_EOL', PHP_EOL]; |
|
|
|
|
45
|
|
|
yield ['PHP_VERSION_ID', PHP_VERSION_ID]; |
46
|
|
|
yield [__NAMESPACE__ . '\SOME_CONSTANT', SOME_CONSTANT]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param mixed $value |
51
|
|
|
* |
52
|
|
|
* @dataProvider validClassConstantsProvider() |
53
|
|
|
*/ |
54
|
|
|
public function testValidClassConstant(string $className, string $constantName, $value) : void |
55
|
|
|
{ |
56
|
|
|
self::assertSame($value, $this->resolver->resolveClassOrInterfaceConstant($className, $constantName)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return mixed[][] |
61
|
|
|
*/ |
62
|
|
|
public function validClassConstantsProvider() : iterable |
63
|
|
|
{ |
64
|
|
|
yield 'class' => [DateTimeImmutable::class, 'RFC3339', DateTimeImmutable::RFC3339]; |
|
|
|
|
65
|
|
|
yield 'interface' => [DateTimeInterface::class, 'RFC3339', DateTimeInterface::RFC3339]; |
66
|
|
|
yield 'user-defined' => [SomeClass::class, 'PUBLIC_CONSTANT', SomeClass::PUBLIC_CONSTANT]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testInvalidClass() : void |
70
|
|
|
{ |
71
|
|
|
$this->expectException(InvalidClass::class); |
72
|
|
|
|
73
|
|
|
$this->resolver->resolveClassOrInterfaceConstant(__NAMESPACE__ . '\InvalidClass', 'INVALID_CONSTANT'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testNonexistentClassConstant() : void |
77
|
|
|
{ |
78
|
|
|
$this->expectException(ClassConstantNotFound::class); |
79
|
|
|
$this->expectExceptionMessage( |
80
|
|
|
sprintf( |
81
|
|
|
'Class or interface constant %s::INVALID_CONSTANT does not exist.', |
82
|
|
|
SomeClass::class |
83
|
|
|
) |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$this->resolver->resolveClassOrInterfaceConstant(SomeClass::class, 'INVALID_CONSTANT'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testPrivateClassConstant() : void |
90
|
|
|
{ |
91
|
|
|
$this->expectException(ConstantNotAccessible::class); |
92
|
|
|
$this->expectExceptionMessage( |
93
|
|
|
sprintf( |
94
|
|
|
"Could not access constant %s::PRIVATE_CONSTANT because it's not public.", |
95
|
|
|
SomeClass::class |
96
|
|
|
) |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
$this->resolver->resolveClassOrInterfaceConstant(SomeClass::class, 'PRIVATE_CONSTANT'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
const SOME_CONSTANT = 123; |
104
|
|
|
|
105
|
|
|
final class SomeClass |
106
|
|
|
{ |
107
|
|
|
public const PUBLIC_CONSTANT = 123; |
108
|
|
|
private const PRIVATE_CONSTANT = 456; |
109
|
|
|
} |
110
|
|
|
|