1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Cubiche\Core\Enum\Tests\Units; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\Enum\Enum; |
14
|
|
|
use Cubiche\Core\Enum\Tests\Fixtures\BadDefaultEnumFixture; |
15
|
|
|
use Cubiche\Core\Enum\Tests\Fixtures\DefaultEnumFixture; |
16
|
|
|
use Cubiche\Core\Enum\Tests\Fixtures\EnumFixture; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Enum Tests Class. |
20
|
|
|
* |
21
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
22
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class EnumTests extends EnumTestCase |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function newDefaultTestedInstance() |
30
|
|
|
{ |
31
|
|
|
return new EnumFixture(EnumFixture::FOO); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Test value method. |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public function testValue() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$this |
40
|
|
|
->given($enum = new EnumFixture(EnumFixture::FOO)) |
41
|
|
|
->when($value = $enum->value()) |
42
|
|
|
->then() |
43
|
|
|
->variable($value) |
44
|
|
|
->isEqualTo(EnumFixture::FOO) |
45
|
|
|
; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Test name method. |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function testName() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$this |
54
|
|
|
->given($enum = new EnumFixture(EnumFixture::FOO)) |
55
|
|
|
->when($name = $enum->name()) |
56
|
|
|
->then() |
57
|
|
|
->variable($name) |
58
|
|
|
->isEqualTo('FOO') |
59
|
|
|
; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Test hashCode method. |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function testHashCode() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$this |
68
|
|
|
->given($enum = new EnumFixture(EnumFixture::FOO)) |
69
|
|
|
->when($hashCode = $enum->hashCode()) |
70
|
|
|
->then() |
71
|
|
|
->variable($hashCode) |
72
|
|
|
->isEqualTo((string) $enum->value()) |
73
|
|
|
; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Test __toString method. |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function testToString() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$this |
82
|
|
|
->given($enum = new EnumFixture(EnumFixture::FOO)) |
83
|
|
|
->when($string = $enum->__toString()) |
84
|
|
|
->then() |
85
|
|
|
->string($string) |
86
|
|
|
->isEqualTo((string) $enum->value()) |
87
|
|
|
; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Test is method. |
92
|
|
|
*/ |
93
|
|
|
public function testIs() |
94
|
|
|
{ |
95
|
|
|
$this |
96
|
|
|
->given($enum = new EnumFixture(EnumFixture::FOO)) |
97
|
|
|
->then() |
98
|
|
|
->boolean($enum->is(EnumFixture::FOO)) |
99
|
|
|
->isTrue() |
100
|
|
|
->boolean($enum->is(EnumFixture::BAR)) |
101
|
|
|
->isFalse() |
102
|
|
|
; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Test isValidName method. |
107
|
|
|
*/ |
108
|
|
|
public function testIsValidName() |
109
|
|
|
{ |
110
|
|
|
$this |
111
|
|
|
->when($isValid = EnumFixture::isValidName('FOO')) |
112
|
|
|
->then() |
113
|
|
|
->boolean($isValid) |
114
|
|
|
->isTrue() |
115
|
|
|
; |
116
|
|
|
|
117
|
|
|
$this |
118
|
|
|
->when($isValid = EnumFixture::isValidName('ZOO')) |
119
|
|
|
->then() |
120
|
|
|
->boolean($isValid) |
121
|
|
|
->isFalse() |
122
|
|
|
; |
123
|
|
|
|
124
|
|
|
$this |
125
|
|
|
->when($isValid = EnumFixture::isValidName('__DEFAULT')) |
126
|
|
|
->then() |
127
|
|
|
->boolean($isValid) |
128
|
|
|
->isFalse() |
129
|
|
|
; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Test names method. |
134
|
|
|
*/ |
135
|
|
|
public function testNames() |
136
|
|
|
{ |
137
|
|
|
$this |
138
|
|
|
->when($names = EnumFixture::names()) |
139
|
|
|
->then() |
140
|
|
|
->array($names) |
141
|
|
|
->isEqualTo(array('FOO', 'BAR')) |
142
|
|
|
; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Test values method. |
147
|
|
|
*/ |
148
|
|
|
public function testValues() |
149
|
|
|
{ |
150
|
|
|
$this |
151
|
|
|
->when($values = EnumFixture::values()) |
152
|
|
|
->then() |
153
|
|
|
->array($values) |
154
|
|
|
->isEqualTo(array('FOO' => EnumFixture::FOO(), 'BAR' => EnumFixture::BAR())) |
155
|
|
|
; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Test __DEFAULT method. |
160
|
|
|
*/ |
161
|
|
|
public function testDefault() |
162
|
|
|
{ |
163
|
|
|
$this->equalityTest(EnumFixture::__DEFAULT(), EnumFixture::FOO()); |
164
|
|
|
$this->equalityTest(DefaultEnumFixture::__DEFAULT(), DefaultEnumFixture::BAR()); |
165
|
|
|
|
166
|
|
|
$this |
167
|
|
|
->exception(function () { |
168
|
|
|
BadDefaultEnumFixture::__DEFAULT(); |
169
|
|
|
}) |
170
|
|
|
->isInstanceof(\UnexpectedValueException::class) |
171
|
|
|
->exception(function () { |
172
|
|
|
BadDefaultEnumFixture::BAZ(); |
173
|
|
|
}) |
174
|
|
|
->isInstanceof(\BadMethodCallException::class) |
175
|
|
|
; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Test ensure method. |
180
|
|
|
*/ |
181
|
|
|
public function testEnsure() |
182
|
|
|
{ |
183
|
|
|
$this->equalityTest(EnumFixture::ensure(EnumFixture::FOO()), EnumFixture::FOO()); |
184
|
|
|
$this->equalityTest(EnumFixture::ensure(EnumFixture::FOO(), EnumFixture::BAR()), EnumFixture::FOO()); |
185
|
|
|
$this->equalityTest(EnumFixture::ensure(), EnumFixture::__DEFAULT()); |
186
|
|
|
$this->equalityTest(EnumFixture::ensure(null, EnumFixture::BAR()), EnumFixture::BAR()); |
187
|
|
|
|
188
|
|
|
$this |
189
|
|
|
->exception(function () { |
190
|
|
|
EnumFixture::ensure(DefaultEnumFixture::FOO()); |
191
|
|
|
}) |
192
|
|
|
->isInstanceof(\InvalidArgumentException::class) |
193
|
|
|
; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param Enum $enum1 |
198
|
|
|
* @param Enum $enum2 |
199
|
|
|
*/ |
200
|
|
|
protected function equalityTest(Enum $enum1, Enum $enum2) |
201
|
|
|
{ |
202
|
|
|
$this |
203
|
|
|
->given($enum1, $enum2) |
204
|
|
|
->then() |
205
|
|
|
->boolean($enum1->equals($enum2)) |
206
|
|
|
->isTrue() |
207
|
|
|
; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.