File: AbstractEnumTest.php

package info (click to toggle)
dasprid-enum 1.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 200 kB
  • sloc: php: 777; xml: 24; makefile: 11; sh: 7
file content (121 lines) | stat: -rw-r--r-- 3,795 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
declare(strict_types = 1);

namespace DASPRiD\EnumTest;

use DASPRiD\Enum\AbstractEnum;
use DASPRiD\Enum\Exception\CloneNotSupportedException;
use DASPRiD\Enum\Exception\IllegalArgumentException;
use DASPRiD\Enum\Exception\MismatchException;
use DASPRiD\Enum\Exception\SerializeNotSupportedException;
use DASPRiD\Enum\Exception\UnserializeNotSupportedException;
use PHPUnit\Framework\TestCase;
use ReflectionClass;

final class AbstractEnumTest extends TestCase
{
    public function setUp(): void
    {
        $reflectionClass = new ReflectionClass(AbstractEnum::class);

        $constantsProperty = $reflectionClass->getProperty('constants');
        $constantsProperty->setAccessible(true);
        $constantsProperty->setValue([]);

        $valuesProperty = $reflectionClass->getProperty('values');
        $valuesProperty->setAccessible(true);
        $valuesProperty->setValue([]);

        $allValuesLoadedProperty = $reflectionClass->getProperty('allValuesLoaded');
        $allValuesLoadedProperty->setAccessible(true);
        $allValuesLoadedProperty->setValue([]);
    }

    public function testToString() : void
    {
        $weekday = WeekDay::FRIDAY();
        self::assertSame('FRIDAY', (string) $weekday);
    }

    public function testName() : void
    {
        $this->assertSame('WEDNESDAY', WeekDay::WEDNESDAY()->name());
    }

    public function testOrdinal() : void
    {
        $this->assertSame(2, WeekDay::WEDNESDAY()->ordinal());
    }

    public function testSameInstanceIsReturned() : void
    {
        self::assertSame(WeekDay::FRIDAY(), WeekDay::FRIDAY());
    }

    public static function testValueOf() : void
    {
        self::assertSame(WeekDay::FRIDAY(), WeekDay::valueOf('FRIDAY'));
    }

    public function testValueOfInvalidConstant() : void
    {
        $this->expectException(IllegalArgumentException::class);
        WeekDay::valueOf('CATURDAY');
    }

    public function testExceptionOnCloneAttempt() : void
    {
        $this->expectException(CloneNotSupportedException::class);
        clone WeekDay::FRIDAY();
    }

    public function testExceptionOnSerializeAttempt() : void
    {
        $this->expectException(SerializeNotSupportedException::class);
        serialize(WeekDay::FRIDAY());
    }

    public function testExceptionOnUnserializeAttempt() : void
    {
        $this->expectException(UnserializeNotSupportedException::class);
        unserialize('O:24:"DASPRiD\\EnumTest\\WeekDay":0:{}');
    }

    public function testReturnValueOfValuesIsSortedByOrdinal() : void
    {
        // Initialize some week days out of order
        WeekDay::SATURDAY();
        WeekDay::TUESDAY();

        $ordinals = array_values(array_map(function (WeekDay $weekDay) : int {
            return $weekDay->ordinal();
        }, WeekDay::values()));

        self::assertSame([0, 1, 2, 3, 4, 5, 6], $ordinals);

        $cachedOrdinals = array_values(array_map(function (WeekDay $weekDay) : int {
            return $weekDay->ordinal();
        }, WeekDay::values()));
        $this->assertSame($ordinals, $cachedOrdinals);
    }

    public function testCompareTo() : void
    {
        $this->assertSame(-4, WeekDay::WEDNESDAY()->compareTo(WeekDay::SUNDAY()));
        $this->assertSame(4, WeekDay::SUNDAY()->compareTo(WeekDay::WEDNESDAY()));
        $this->assertSame(0, WeekDay::WEDNESDAY()->compareTo(WeekDay::WEDNESDAY()));
    }

    public function testCompareToWrongEnum() : void
    {
        $this->expectException(MismatchException::class);
        WeekDay::MONDAY()->compareTo(Planet::EARTH());
    }

    public function testParameterizedEnum() : void
    {
        $planet = Planet::EARTH();
        $this->assertSame(5.976e+24, $planet->mass());
        $this->assertSame(6.37814e6, $planet->radius());
    }
}