Passed
Push — main ( 80f234...435c54 )
by Anatoly
02:13 queued 12s
created

EnumTest::testJsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
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;
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(),
0 ignored issues
show
Bug introduced by
The method Int1() does not exist on Sunrise\Hydrator\Tests\Fixtures\CustomEnum. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

17
            Fixtures\CustomEnum::/** @scrutinizer ignore-call */ 
18
                                 Int1(),
Loading history...
18
            Fixtures\CustomEnum::Int2(),
0 ignored issues
show
Bug introduced by
The method Int2() does not exist on Sunrise\Hydrator\Tests\Fixtures\CustomEnum. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

18
            Fixtures\CustomEnum::/** @scrutinizer ignore-call */ 
19
                                 Int2(),
Loading history...
19
            Fixtures\CustomEnum::String1(),
0 ignored issues
show
Bug introduced by
The method String1() does not exist on Sunrise\Hydrator\Tests\Fixtures\CustomEnum. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

19
            Fixtures\CustomEnum::/** @scrutinizer ignore-call */ 
20
                                 String1(),
Loading history...
20
            Fixtures\CustomEnum::String2(),
0 ignored issues
show
Bug introduced by
The method String2() does not exist on Sunrise\Hydrator\Tests\Fixtures\CustomEnum. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
            Fixtures\CustomEnum::/** @scrutinizer ignore-call */ 
21
                                 String2(),
Loading history...
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();
0 ignored issues
show
Bug introduced by
The method Another() does not exist on Sunrise\Hydrator\Tests\Fixtures\CustomEnum. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        Fixtures\CustomEnum::/** @scrutinizer ignore-call */ 
50
                             Another();
Loading history...
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));
0 ignored issues
show
Bug introduced by
Sunrise\Hydrator\Tests\F...res\CustomEnum::Another of type array is incompatible with the type integer|string expected by parameter $value of Sunrise\Hydrator\Enum::tryFrom(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        $this->assertNull(Fixtures\CustomEnum::tryFrom(/** @scrutinizer ignore-type */ Fixtures\CustomEnum::Another));
Loading history...
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