EnumRegistryTest::testGetInvalidException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
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 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);
0 ignored issues
show
Bug introduced by
It seems like $translator can also be of type object<Prophecy\Prophecy\ObjectProphecy>; however, Yokai\EnumBundle\Tests\F...tateEnum::__construct() does only seem to accept object<Symfony\Contracts...on\TranslatorInterface>, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
50
        $subscription = new SubscriptionEnum($translator);
0 ignored issues
show
Bug introduced by
It seems like $translator can also be of type object<Prophecy\Prophecy\ObjectProphecy>; however, Yokai\EnumBundle\Tests\F...tionEnum::__construct() does only seem to accept object<Symfony\Contracts...on\TranslatorInterface>, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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