Completed
Pull Request — master (#29)
by Yann
12:40
created

ConstantExtractorTest::getExtractor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace Yokai\EnumBundle\Tests;
4
5
use Generator;
6
use Yokai\EnumBundle\ConstantExtractor;
7
use Yokai\EnumBundle\Exception\CannotExtractConstantsException;
8
9
/**
10
 * @author Yann Eugoné <[email protected]>
11
 */
12
class ConstantExtractorTest extends TestCase
13
{
14
    public function getExtractor(): ConstantExtractor
15
    {
16
        return new ConstantExtractor();
17
    }
18
19
    /**
20
     * @dataProvider malformed
21
     */
22
    public function testExtractMalformedPattern(string $pattern, string $exceptionMessage): void
23
    {
24
        $this->expectException(CannotExtractConstantsException::class);
25
        $this->expectExceptionMessageMatches($exceptionMessage);
26
27
        $this->getExtractor()->extract($pattern);
28
    }
29
30
    /**
31
     * @dataProvider empty
32
     */
33
    public function testExtractEmpty(string $pattern, string $exceptionMessage): void
34
    {
35
        $this->expectException(CannotExtractConstantsException::class);
36
        $this->expectExceptionMessageMatches($exceptionMessage);
37
38
        $this->getExtractor()->extract($pattern);
39
    }
40
41
    /**
42
     * @dataProvider successful
43
     */
44
    public function testExtractSuccessful(string $pattern, array $expectedList): void
45
    {
46
        self::assertEquals($expectedList, $this->getExtractor()->extract($pattern));
47
    }
48
49
    public function empty(): Generator
50
    {
51
        yield 'class without constant' => [
52
            ClassWithoutConstant::class.'::*',
53
            '/Class .+ has no public constant/',
54
        ];
55
        yield 'class without public constant' => [
56
            ClassWithNoPublicConstant::class.'::*',
57
            '/Class .+ has no public constant/',
58
        ];
59
        yield 'class with constant but no match' => [
60
            ClassWithConstant::class.'::NO_MATCH*',
61
            '/Pattern .+ matches no constant/',
62
        ];
63
    }
64
65
    public function malformed(): Generator
66
    {
67
        $invalidPatternRegexp = '/Constant extraction pattern must look like Fully\\\\Qualified\\\\ClassName::CONSTANT_\*\..+/';
68
        yield 'no class no constant pattern' => [
69
            'not a pattern',
70
            $invalidPatternRegexp,
71
        ];
72
        yield 'class that do not exists' => [
73
            'SomeClassThatDoNotExists::STATUS_*',
74
            '/Class SomeClassThatDoNotExists do not exists\./',
75
        ];
76
        yield 'missing constant pattern and separator' => [
77
            ClassWithConstant::class,
78
            $invalidPatternRegexp,
79
        ];
80
        yield 'missing constant pattern' => [
81
            ClassWithConstant::class.'::',
82
            $invalidPatternRegexp,
83
        ];
84
        yield 'two separator' => [
85
            ClassWithConstant::class.'::STATUS_*::',
86
            $invalidPatternRegexp,
87
        ];
88
        yield 'no * in pattern' => [
89
            ClassWithConstant::class.'::STATUS_ONLINE',
90
            $invalidPatternRegexp,
91
        ];
92
    }
93
94
    public function successful(): Generator
95
    {
96
        yield 'starting with status' => [
97
            ClassWithConstant::class.'::STATUS_*',
98
            [ClassWithConstant::STATUS_ONLINE, ClassWithConstant::STATUS_DRAFT],
99
        ];
100
        yield 'ending with online' => [
101
            ClassWithConstant::class.'::*_ONLINE',
102
            [ClassWithConstant::STATUS_ONLINE],
103
        ];
104
    }
105
}
106
107
class ClassWithoutConstant
108
{
109
}
110
111
class ClassWithNoPublicConstant
112
{
113
    private const PROTECTED_CONST = 'protected';
114
    private const PRIVATE_CONST = 'private';
115
}
116
117
class ClassWithConstant
118
{
119
    public const STATUS_ONLINE = 'online';
120
    public const STATUS_DRAFT = 'draft';
121
    protected const STATUS_PROTECTED = 'protected';
122
    private const STATUS_PRIVATE = 'private';
123
}
124