Test Failed
Pull Request — main (#20)
by Anatoly
31:11 queued 02:30
created

EnumTest::testCase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 17
rs 9.8666
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sunrise\Hydrator\Tests;
6
7
use PHPUnit\Framework\TestCase;
8
use Sunrise\Hydrator\Enum;
0 ignored issues
show
Bug introduced by
The type Sunrise\Hydrator\Enum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use JsonSerializable;
10
use RuntimeException;
11
12
class EnumTest extends TestCase
13
{
14
    public function testCases() : void
15
    {
16
        $this->assertSame([
17
            Fixtures\CustomEnum::Int1(),
18
            Fixtures\CustomEnum::Int2(),
19
            Fixtures\CustomEnum::String1(),
20
            Fixtures\CustomEnum::String2(),
21
        ], Fixtures\CustomEnum::cases());
22
    }
23
24
    public function testCase() : void
25
    {
26
        $case = Fixtures\CustomEnum::Int1();
27
        $this->assertSame('Int1', $case->name());
28
        $this->assertSame(Fixtures\CustomEnum::Int1, $case->value());
29
30
        $case = Fixtures\CustomEnum::Int2();
31
        $this->assertSame('Int2', $case->name());
32
        $this->assertSame(Fixtures\CustomEnum::Int2, $case->value());
33
34
        $case = Fixtures\CustomEnum::String1();
35
        $this->assertSame('String1', $case->name());
36
        $this->assertSame(Fixtures\CustomEnum::String1, $case->value());
37
38
        $case = Fixtures\CustomEnum::String2();
39
        $this->assertSame('String2', $case->name());
40
        $this->assertSame(Fixtures\CustomEnum::String2, $case->value());
41
    }
42
43
    public function testUnknownCase() : void
44
    {
45
        $this->expectException(RuntimeException::class);
46
        $this->expectExceptionMessage('Enum case ' . Fixtures\CustomEnum::class
47
                                    . '::Another not found');
48
49
        Fixtures\CustomEnum::Another();
50
    }
51
52
    public function testTryFrom() : void
53
    {
54
        $this->assertSame(Fixtures\CustomEnum::Int1(), Fixtures\CustomEnum::tryFrom(Fixtures\CustomEnum::Int1));
55
        $this->assertSame(Fixtures\CustomEnum::Int2(), Fixtures\CustomEnum::tryFrom(Fixtures\CustomEnum::Int2));
56
        $this->assertSame(Fixtures\CustomEnum::String1(), Fixtures\CustomEnum::tryFrom(Fixtures\CustomEnum::String1));
57
        $this->assertSame(Fixtures\CustomEnum::String2(), Fixtures\CustomEnum::tryFrom(Fixtures\CustomEnum::String2));
58
    }
59
60
    public function testTryFromWithUnknownValue() : void
61
    {
62
        $this->assertNull(Fixtures\CustomEnum::tryFrom(Fixtures\CustomEnum::Another));
63
        $this->assertNull(Fixtures\CustomEnum::tryFrom('unknown'));
64
    }
65
66
    public function testCache() : void
67
    {
68
        $this->assertSame(Fixtures\CustomEnum::cases(), Fixtures\CustomEnum::cases());
69
        $this->assertSame(Fixtures\CustomEnum::Int1(), Fixtures\CustomEnum::Int1());
70
        $this->assertSame(Fixtures\CustomEnum::Int2(), Fixtures\CustomEnum::Int2());
71
        $this->assertSame(Fixtures\CustomEnum::String1(), Fixtures\CustomEnum::String1());
72
        $this->assertSame(Fixtures\CustomEnum::String2(), Fixtures\CustomEnum::String2());
73
    }
74
75
    public function testJsonSerialize() : void
76
    {
77
        $case = Fixtures\CustomEnum::Int1();
78
79
        $this->assertInstanceOf(JsonSerializable::class, $case);
80
        $this->assertSame($case->value(), $case->jsonSerialize());
81
    }
82
}
83