1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | namespace Thunder\Platenum\Tests; |
||
4 | |||
5 | use Thunder\Platenum\Enum\CallbackEnumTrait; |
||
6 | use Thunder\Platenum\Exception\PlatenumException; |
||
7 | |||
8 | /** |
||
9 | * @author Tomasz Kowalczyk <[email protected]> |
||
10 | */ |
||
11 | final class CallbackEnumTest extends AbstractTestCase |
||
12 | { |
||
13 | public function testMembers(): void |
||
14 | { |
||
15 | $members = ['FIRST' => 1, 'SECOND' => 2]; |
||
16 | $trait = $this->makeCallbackTraitEnum($members); |
||
17 | $trait::initialize(function() use($members) { return $members; }); |
||
18 | $extends = $this->makeCallbackExtendsEnum($members); |
||
19 | $extends::initialize(function() use($members) { return $members; }); |
||
20 | |||
21 | $expected = ['FIRST' => 1, 'SECOND' => 2]; |
||
22 | $this->assertSame($expected, $trait::getMembersAndValues()); |
||
23 | $this->assertSame($expected, $extends::getMembersAndValues()); |
||
24 | } |
||
25 | |||
26 | public function testExceptionNoInitialize(): void |
||
27 | { |
||
28 | $members = ['FIRST' => 1, 'SECOND' => 2]; |
||
29 | $class = $this->makeCallbackTraitEnum($members); |
||
30 | |||
31 | $this->expectException(PlatenumException::class); |
||
32 | $this->expectExceptionMessage('Enum '.$class.' requires static property $callback to be a valid callable returning members and values mapping.'); |
||
33 | $class::FIRST(); |
||
34 | } |
||
35 | |||
36 | public function testExceptionAlreadyInitialized(): void |
||
37 | { |
||
38 | $members = ['FIRST' => 1, 'SECOND' => 2]; |
||
39 | $class = $this->makeCallbackTraitEnum($members); |
||
40 | $class::initialize(function() use($members) { return $members; }); |
||
41 | |||
42 | $this->expectException(PlatenumException::class); |
||
43 | $this->expectExceptionMessage('Enum '.$class.' callback was already initialized.'); |
||
44 | $class::initialize(function() use($members) { return $members; }); |
||
45 | } |
||
46 | |||
47 | public function testImpossibleExceptionCallbackNotCallable(): void |
||
48 | { |
||
49 | $class = $this->computeUniqueClassName('CallbackTrait'); |
||
50 | eval('final class '.$class.' implements \JsonSerializable { use '.CallbackEnumTrait::class.'; }'); |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
51 | |||
52 | $ref = new \ReflectionClass($class); |
||
53 | $callbacks = $ref->getProperty('callbacks'); |
||
54 | $callbacks->setAccessible(true); |
||
55 | $callbacks->setValue($class, [$class => 'invalid']); |
||
56 | |||
57 | $this->expectException(PlatenumException::class); |
||
58 | $this->expectExceptionMessage('Enum '.$class.' requires static property $callback to be a valid callable returning members and values mapping.'); |
||
59 | $class::FIRST(); |
||
60 | } |
||
61 | } |
||
62 |