1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Werkspot\Enum\Tests; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
final class AbstractEnumTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @test |
13
|
|
|
*/ |
14
|
|
|
public function enum(): void |
15
|
|
|
{ |
16
|
|
|
$a = TestEnum::get(TestEnum::A); |
17
|
|
|
$b = TestEnum::get(TestEnum::B); |
18
|
|
|
|
19
|
|
|
self::assertSame(TestEnum::A, $a->getValue()); |
20
|
|
|
self::assertSame(TestEnum::B, $b->getValue()); |
21
|
|
|
self::assertSame(TestEnum::A, (string) $a); |
22
|
|
|
self::assertSame(TestEnum::B, (string) $b); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @test |
27
|
|
|
*/ |
28
|
|
|
public function null(): void |
29
|
|
|
{ |
30
|
|
|
$nullEnum = TestEnum::get(null); |
31
|
|
|
self::assertNull($nullEnum->getValue()); |
32
|
|
|
self::assertSame('', (string) $nullEnum); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @test |
37
|
|
|
*/ |
38
|
|
|
public function integer(): void |
39
|
|
|
{ |
40
|
|
|
$integerEnum = TestEnum::get(3); |
41
|
|
|
self::assertSame(3, $integerEnum->getValue()); |
42
|
|
|
self::assertSame('3', (string) $integerEnum); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @test |
47
|
|
|
*/ |
48
|
|
|
public function equalsShouldReturnTrueWhenComparingObjectsWithTheSameTypeAndValue(): void |
49
|
|
|
{ |
50
|
|
|
$a = TestEnum::get(TestEnum::A); |
51
|
|
|
|
52
|
|
|
self::assertTrue($a->equals(TestEnum::get(TestEnum::A))); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @test |
57
|
|
|
*/ |
58
|
|
|
public function equalsShouldReturnFalseWhenComparingObjectsWithTheSameTypeButDifferentValue(): void |
59
|
|
|
{ |
60
|
|
|
$a = TestEnum::get(TestEnum::A); |
61
|
|
|
|
62
|
|
|
self::assertFalse($a->equals(TestEnum::get(TestEnum::B))); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @test |
67
|
|
|
*/ |
68
|
|
|
public function equalsShouldReturnFalseWhenComparingObjectsWithTheSameValueButDifferentType(): void |
69
|
|
|
{ |
70
|
|
|
$a = TestEnum::get(TestEnum::A); |
71
|
|
|
|
72
|
|
|
self::assertFalse($a->equals(Test2Enum::get(Test2Enum::A))); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @test |
77
|
|
|
*/ |
78
|
|
|
public function equalsShouldReturnTrueWhenComparingObjectsWithDifferentTypeAndValue(): void |
79
|
|
|
{ |
80
|
|
|
$a = TestEnum::get(TestEnum::A); |
81
|
|
|
|
82
|
|
|
self::assertFalse($a->equals(Test2Enum::get(Test2Enum::C))); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @test |
87
|
|
|
* |
88
|
|
|
* @expectedException \InvalidArgumentException |
89
|
|
|
* @expectedExceptionMessage Value [anything] is not matching any valid value of class "TestEnum". Valid values are ['A', 'BEE', 1, 3, NULL, true]. |
90
|
|
|
*/ |
91
|
|
|
public function exceptionMessage(): void |
92
|
|
|
{ |
93
|
|
|
TestEnum::get('anything'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @test |
98
|
|
|
* |
99
|
|
|
* @dataProvider getInvertedCaseOptions |
100
|
|
|
* @expectedException \InvalidArgumentException |
101
|
|
|
* |
102
|
|
|
* @param mixed $option |
103
|
|
|
*/ |
104
|
|
|
public function getWithInvertedCaseIsIncorrect($option): void |
105
|
|
|
{ |
106
|
|
|
TestEnum::get($option); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getInvertedCaseOptions() |
110
|
|
|
{ |
111
|
|
|
return [ |
112
|
|
|
[strtolower(TestEnum::A)], |
113
|
|
|
[strtolower(TestEnum::B)], |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @test |
119
|
|
|
* |
120
|
|
|
* @expectedException \InvalidArgumentException |
121
|
|
|
*/ |
122
|
|
|
public function getWithStrictEqualMatchThrowsException(): void |
123
|
|
|
{ |
124
|
|
|
TestEnum::get('1'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @test |
130
|
|
|
*/ |
131
|
|
|
public function isShouldAllowToCallMethodsBasedOnConstantNames(): void |
132
|
|
|
{ |
133
|
|
|
$enum = TestEnum::nameWithUnderscore(); |
134
|
|
|
|
135
|
|
|
self::assertInstanceOf(TestEnum::class, $enum); |
136
|
|
|
self::assertSame(TestEnum::NAME_WITH_UNDERSCORE, $enum->getValue()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @test |
141
|
|
|
* |
142
|
|
|
* @expectedException \BadMethodCallException |
143
|
|
|
* @expectedExceptionMessage Werkspot\Enum\Tests\TestEnum::isDoesNotExist() does not exist |
144
|
|
|
*/ |
145
|
|
|
public function isShouldThrowAnExceptionWhenWhenCallingAnInvalidMethod(): void |
146
|
|
|
{ |
147
|
|
|
TestEnum::isDoesNotExist(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @test |
152
|
|
|
*/ |
153
|
|
|
public function getValidOptions(): void |
154
|
|
|
{ |
155
|
|
|
self::assertSame( |
156
|
|
|
[ |
157
|
|
|
TestEnum::A, |
158
|
|
|
TestEnum::B, |
159
|
|
|
TestEnum::A1, |
160
|
|
|
TestEnum::A3, |
161
|
|
|
TestEnum::ANULL, |
162
|
|
|
TestEnum::NAME_WITH_UNDERSCORE, |
163
|
|
|
], |
164
|
|
|
TestEnum::getValidOptions() |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|