Failed Conditions
Push — new-parser-ast-metadata ( e8e091...17d881 )
by Michael
21s
created

ReflectionConstantResolverTest::testInvalidClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 const PHP_EOL;
16
use const PHP_VERSION_ID;
17
use function sprintf;
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];
0 ignored issues
show
Bug Best Practice introduced by
The expression yield array('PHP_EOL', PHP_EOL) returns the type Generator which is incompatible with the documented return type array<mixed,array<mixed,mixed>>.
Loading history...
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];
0 ignored issues
show
Bug Best Practice introduced by
The expression yield 'class' => array(D...TimeImmutable::RFC3339) returns the type Generator which is incompatible with the documented return type array<mixed,array<mixed,mixed>>.
Loading history...
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