|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Yokai\EnumBundle\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
6
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
7
|
|
|
use Yokai\EnumBundle\EnumRegistry; |
|
8
|
|
|
use Yokai\EnumBundle\Exception\DuplicatedEnumException; |
|
9
|
|
|
use Yokai\EnumBundle\Exception\InvalidEnumException; |
|
10
|
|
|
use Yokai\EnumBundle\Tests\Fixtures\GenderEnum; |
|
11
|
|
|
use Yokai\EnumBundle\Tests\Fixtures\StateEnum; |
|
12
|
|
|
use Yokai\EnumBundle\Tests\Fixtures\SubscriptionEnum; |
|
13
|
|
|
use Yokai\EnumBundle\Tests\Fixtures\TypeEnum; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author Yann Eugoné <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class EnumRegistryTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var EnumRegistry |
|
22
|
|
|
*/ |
|
23
|
|
|
private $registry; |
|
24
|
|
|
|
|
25
|
|
|
protected function setUp(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$this->registry = new EnumRegistry; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testAddDuplicatedException(): void |
|
31
|
|
|
{ |
|
32
|
|
|
$this->expectException(DuplicatedEnumException::class); |
|
33
|
|
|
$this->registry->add(new GenderEnum); |
|
34
|
|
|
$this->registry->add(new GenderEnum); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testGetInvalidException(): void |
|
38
|
|
|
{ |
|
39
|
|
|
$this->expectException(InvalidEnumException::class); |
|
40
|
|
|
$this->registry->add(new GenderEnum); |
|
41
|
|
|
$this->registry->get('type'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testAddNominal(): void |
|
45
|
|
|
{ |
|
46
|
|
|
/** @var TranslatorInterface|ObjectProphecy $translator */ |
|
47
|
|
|
$translator = $this->prophesize(TranslatorInterface::class)->reveal(); |
|
48
|
|
|
$gender = new GenderEnum; |
|
49
|
|
|
$state = new StateEnum($translator); |
|
|
|
|
|
|
50
|
|
|
$subscription = new SubscriptionEnum($translator); |
|
|
|
|
|
|
51
|
|
|
$type = new TypeEnum; |
|
52
|
|
|
|
|
53
|
|
|
$this->registry->add($gender); |
|
54
|
|
|
$this->registry->add($state); |
|
55
|
|
|
$this->registry->add($subscription); |
|
56
|
|
|
$this->registry->add($type); |
|
57
|
|
|
|
|
58
|
|
|
$this->assertTrue($this->registry->has(GenderEnum::class)); |
|
59
|
|
|
$this->assertTrue($this->registry->has('state')); |
|
60
|
|
|
$this->assertTrue($this->registry->has('subscription')); |
|
61
|
|
|
$this->assertTrue($this->registry->has('type')); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertSame($gender, $this->registry->get(GenderEnum::class)); |
|
64
|
|
|
$this->assertSame($state, $this->registry->get('state')); |
|
65
|
|
|
$this->assertSame($subscription, $this->registry->get('subscription')); |
|
66
|
|
|
$this->assertSame($type, $this->registry->get('type')); |
|
67
|
|
|
$this->assertSame( |
|
68
|
|
|
[GenderEnum::class => $gender, 'state' => $state, 'subscription' => $subscription, 'type' => $type], |
|
69
|
|
|
$this->registry->all() |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.