/
AbstractEnumUnitTest.php
210 lines (176 loc) · 4.94 KB
/
AbstractEnumUnitTest.php
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
declare(strict_types=1);
/*
* This file is part of the Explicit Architecture POC,
* which is created on top of the Symfony Demo application.
*
* (c) Herberto Graça <herberto.graca@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Acme\PhpExtension\Test\Enum;
use Acme\PhpExtension\Test\AbstractUnitTest;
/**
* @small
*
* @internal
*/
final class AbstractEnumUnitTest extends AbstractUnitTest
{
/**
* @test
*/
public function enum(): void
{
$a = TestEnum::get(TestEnum::A);
$b = TestEnum::get(TestEnum::B);
self::assertSame(TestEnum::A, $a->getValue());
self::assertSame(TestEnum::B, $b->getValue());
self::assertSame(TestEnum::A, (string) $a);
self::assertSame(TestEnum::B, (string) $b);
}
/**
* @test
*/
public function null(): void
{
$nullEnum = TestEnum::get(null);
self::assertNull($nullEnum->getValue());
self::assertSame('', (string) $nullEnum);
}
/**
* @test
*/
public function integer(): void
{
$integerEnum = TestEnum::get(3);
self::assertSame(3, $integerEnum->getValue());
self::assertSame('3', (string) $integerEnum);
}
/**
* @test
*/
public function is_should_return_true_when_comparing_objects_with_the_same_type_and_value(): void
{
$a = TestEnum::get(TestEnum::A);
self::assertTrue($a->isA());
}
/**
* @test
*/
public function is_should_return_false_when_comparing_objects_with_the_same_type_and_different_value(): void
{
$a = TestEnum::get(TestEnum::A);
self::assertFalse($a->isB());
}
/**
* @test
*/
public function equals_should_return_true_when_comparing_objects_with_the_same_type_and_value(): void
{
$a = TestEnum::get(TestEnum::A);
self::assertTrue($a->equals(TestEnum::get(TestEnum::A)));
}
/**
* @test
*/
public function equals_should_return_false_when_comparing_objects_with_the_same_type_but_different_value(): void
{
$a = TestEnum::get(TestEnum::A);
self::assertFalse($a->equals(TestEnum::get(TestEnum::B)));
}
/**
* @test
*/
public function equals_should_return_false_when_comparing_objects_with_the_same_value_but_different_type(): void
{
$a = TestEnum::get(TestEnum::A);
self::assertFalse($a->equals(Test2Enum::get(Test2Enum::A)));
}
/**
* @test
*/
public function equals_should_return_true_when_comparing_objects_with_different_type_and_value(): void
{
$a = TestEnum::get(TestEnum::A);
self::assertFalse($a->equals(Test2Enum::get(Test2Enum::C)));
}
/**
* @test
*/
public function exception_message(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Value [anything] is not matching any valid value of class "TestEnum". Valid values are [\'A\', \'BEE\', 1, 3, NULL, true].');
TestEnum::get('anything');
}
/**
* @test
*
* @dataProvider getInvertedCaseOptions
*/
public function get_with_inverted_case_is_incorrect($option): void
{
$this->expectException(\InvalidArgumentException::class);
TestEnum::get($option);
}
public function getInvertedCaseOptions(): array
{
return [
[mb_strtolower(TestEnum::A)],
[mb_strtolower(TestEnum::B)],
];
}
/**
* @test
*/
public function get_with_strict_equal_match_throws_exception(): void
{
$this->expectException(\InvalidArgumentException::class);
TestEnum::get('1');
}
/**
* @test
*/
public function is_should_allow_to_call_methods_based_on_constant_names(): void
{
$enum = TestEnum::nameWithUnderscore();
self::assertInstanceOf(TestEnum::class, $enum);
self::assertSame(TestEnum::NAME_WITH_UNDERSCORE, $enum->getValue());
}
/**
* @test
*/
public function is_should_throw_an_exception_when_when_calling_an_invalid_method(): void
{
$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('Acme\\PhpExtension\\Test\\Enum\\TestEnum::isDoesNotExist() does not exist');
TestEnum::isDoesNotExist();
}
/**
* @test
*/
public function get_valid_options(): void
{
self::assertSame(
[
TestEnum::A,
TestEnum::B,
TestEnum::A1,
TestEnum::A3,
TestEnum::ANULL,
TestEnum::NAME_WITH_UNDERSCORE,
],
TestEnum::getValidOptions()
);
}
/**
* @test
*/
public function get_key(): void
{
$enum = TestEnum::b();
self::assertSame('B', $enum->getKey());
}
}