|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
namespace Thunder\Platenum\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Thunder\Platenum\Enum\DocblockEnumTrait; |
|
6
|
|
|
use Thunder\Platenum\Exception\PlatenumException; |
|
7
|
|
|
use Thunder\Platenum\Tests\Fake\FakeEnum; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
final class DocblockEnumTest extends AbstractTestCase |
|
13
|
|
|
{ |
|
14
|
|
|
public function testMembers(): void |
|
15
|
|
|
{ |
|
16
|
|
|
$members = ['FIRST' => 1, 'SECOND' => 2]; |
|
17
|
|
|
$trait = $this->makeDocblockTraitEnum($members); |
|
18
|
|
|
$extends = $this->makeDocblockExtendsEnum($members); |
|
19
|
|
|
|
|
20
|
|
|
$expected = ['FIRST' => 'FIRST', 'SECOND' => 'SECOND']; |
|
21
|
|
|
$this->assertSame($expected, $trait::getMembersAndValues()); |
|
22
|
|
|
$this->assertSame($expected, $extends::getMembersAndValues()); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testAcceptBothSelfAndStaticDocblockTypeQualifiers(): void |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var FakeEnum $enum */ |
|
28
|
|
|
$enum = $this->computeUniqueClassName('X'); |
|
29
|
|
|
eval('/** |
|
|
|
|
|
|
30
|
|
|
* @method static self FIRST() |
|
31
|
|
|
* @method static static SECOND() |
|
32
|
|
|
*/ |
|
33
|
|
|
class '.$enum.' { use '.DocblockEnumTrait::class.'; }'); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertSame('FIRST', $enum::FIRST()->getValue()); |
|
36
|
|
|
$this->assertSame('SECOND', $enum::SECOND()->getValue()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testExceptionDocblockNotPresent(): void |
|
40
|
|
|
{ |
|
41
|
|
|
/** @var FakeEnum $enum */ |
|
42
|
|
|
$enum = $this->computeUniqueClassName('X'); |
|
43
|
|
|
eval('class '.$enum.' { use '.DocblockEnumTrait::class.'; }'); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
$this->expectException(PlatenumException::class); |
|
46
|
|
|
$this->expectExceptionMessage('Enum '.$enum.' does not have a docblock with member definitions.'); |
|
47
|
|
|
$enum::FIRST(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testExceptionDocblockWithoutMembers(): void |
|
51
|
|
|
{ |
|
52
|
|
|
/** @var FakeEnum $enum */ |
|
53
|
|
|
$enum = $this->computeUniqueClassName('X'); |
|
54
|
|
|
eval('/** |
|
|
|
|
|
|
55
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
|
56
|
|
|
*/ |
|
57
|
|
|
class '.$enum.' { use '.DocblockEnumTrait::class.'; }'); |
|
58
|
|
|
|
|
59
|
|
|
$this->expectException(PlatenumException::class); |
|
60
|
|
|
$this->expectExceptionMessage('Enum '.$enum.' contains docblock without any member definitions.'); |
|
61
|
|
|
$enum::FIRST(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testExceptionMalformedDocblockMembers(): void |
|
65
|
|
|
{ |
|
66
|
|
|
/** @var FakeEnum $enum */ |
|
67
|
|
|
$enum = $this->computeUniqueClassName('X'); |
|
68
|
|
|
eval('/** |
|
|
|
|
|
|
69
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
|
70
|
|
|
* |
|
71
|
|
|
* @method static self FIRST() |
|
72
|
|
|
* @method static self INV*ALID() |
|
73
|
|
|
*/ |
|
74
|
|
|
class '.$enum.' { use '.DocblockEnumTrait::class.'; }'); |
|
75
|
|
|
|
|
76
|
|
|
$this->expectException(PlatenumException::class); |
|
77
|
|
|
$this->expectExceptionMessage('Enum '.$enum.' contains malformed docblock member definitions.'); |
|
78
|
|
|
$enum::FIRST(); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|