-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathBoolEnumTest.php
323 lines (262 loc) · 10.1 KB
/
BoolEnumTest.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
namespace Spatie\Enum\OldTests;
use ArgumentCountError;
use BadMethodCallException;
use PHPUnit\Framework\TestCase;
use Spatie\Enum\Exceptions\InvalidIndexException;
use Spatie\Enum\Exceptions\InvalidNameException;
use Spatie\Enum\Exceptions\InvalidValueException;
use Spatie\Enum\Tests\Enums\BoolEnum;
use stdClass;
use TypeError;
class BoolEnumTest extends TestCase
{
/** @test */
public function it_can_get_all_instances()
{
$enums = BoolEnum::getAll();
$this->assertCount(2, $enums);
foreach ($enums as $enum) {
$this->assertInstanceOf(BoolEnum::class, $enum);
}
$this->assertEquals('false', $enums[0]->getValue());
$this->assertEquals('true', $enums[1]->getValue());
}
/** @test */
public function can_create_true_instance_from_doc_tag_name()
{
$true = BoolEnum::true();
$this->assertInstanceOf(BoolEnum::class, $true);
$this->assertSame(1, $true->getIndex());
$this->assertTrue(boolval($true->getIndex()));
$this->assertSame('true', $true->getValue());
$this->assertEquals('true', $true);
$this->assertSame('TRUE', $true->getName());
}
/** @test */
public function can_create_true_instance_from_strange_name()
{
$true = BoolEnum::make('TrUe');
$this->assertInstanceOf(BoolEnum::class, $true);
$this->assertSame(1, $true->getIndex());
$this->assertTrue(boolval($true->getIndex()));
$this->assertSame('true', $true->getValue());
$this->assertEquals('true', $true);
$this->assertSame('TRUE', $true->getName());
}
/** @test */
public function can_create_true_instance_from_index()
{
$true = BoolEnum::make(1);
$this->assertInstanceOf(BoolEnum::class, $true);
$this->assertSame(1, $true->getIndex());
$this->assertTrue(boolval($true->getIndex()));
$this->assertSame('true', $true->getValue());
$this->assertEquals('true', $true);
$this->assertSame('TRUE', $true->getName());
}
/** @test */
public function can_create_false_instance_from_doc_tag_name()
{
$false = BoolEnum::false();
$this->assertInstanceOf(BoolEnum::class, $false);
$this->assertSame(0, $false->getIndex());
$this->assertFalse(boolval($false->getIndex()));
$this->assertSame('false', $false->getValue());
$this->assertEquals('false', $false);
$this->assertSame('FALSE', $false->getName());
}
/** @test */
public function can_create_false_instance_from_index()
{
$false = BoolEnum::make(0);
$this->assertInstanceOf(BoolEnum::class, $false);
$this->assertSame(0, $false->getIndex());
$this->assertFalse(boolval($false->getIndex()));
$this->assertSame('false', $false->getValue());
$this->assertEquals('false', $false);
$this->assertSame('FALSE', $false->getName());
}
/** @test */
public function can_check_if_value_is_equal_to_defined_one()
{
$this->assertTrue(BoolEnum::isTrue('true'));
$this->assertTrue(BoolEnum::isTrue(1));
$this->assertFalse(BoolEnum::isTrue('false'));
$this->assertFalse(BoolEnum::isTrue(0));
$this->assertTrue(BoolEnum::isFalse('false'));
$this->assertTrue(BoolEnum::isFalse(0));
$this->assertFalse(BoolEnum::isFalse('true'));
$this->assertFalse(BoolEnum::isFalse(1));
$this->assertTrue(BoolEnum::make('true')->isTrue());
$this->assertTrue(BoolEnum::make(1)->isTrue());
$this->assertFalse(BoolEnum::make('false')->isTrue());
$this->assertFalse(BoolEnum::make(0)->isTrue());
$this->assertTrue(BoolEnum::make('false')->isFalse());
$this->assertTrue(BoolEnum::make(0)->isFalse());
$this->assertFalse(BoolEnum::make('true')->isFalse());
$this->assertFalse(BoolEnum::make(1)->isFalse());
}
/** @test */
public function can_encode_enum_as_json()
{
$true = BoolEnum::true();
$this->assertSame('"true"', json_encode($true));
}
/** @test */
public function can_represent_itself_as_array()
{
$this->assertEquals([
'false' => 0,
'true' => 1,
], BoolEnum::toArray());
}
/** @test */
public function can_represent_its_names_as_array()
{
$this->assertEquals([
'FALSE',
'TRUE',
], BoolEnum::getNames());
}
/** @test */
public function can_represent_its_values_as_array()
{
$this->assertEquals([
'false',
'true',
], BoolEnum::getValues());
}
/** @test */
public function can_represent_its_indices_as_array()
{
$this->assertEquals([
0,
1,
], BoolEnum::getIndices());
}
/** @test */
public function can_not_create_new_instance_without_arguments()
{
$this->expectException(InvalidValueException::class);
$this->expectExceptionMessage('The value for an enum must be a string but NULL given');
new BoolEnum();
}
/** @test */
public function can_not_create_new_instance_without_name()
{
$this->expectException(InvalidNameException::class);
$this->expectExceptionMessage('The name for an enum must be a string but NULL given');
new BoolEnum(null, 'true', 1);
}
/** @test */
public function can_not_create_new_instance_without_value()
{
$this->expectException(InvalidValueException::class);
$this->expectExceptionMessage('The value for an enum must be a string but NULL given');
new BoolEnum('TRUE', null, 1);
}
/** @test */
public function can_not_create_new_instance_without_index()
{
$this->expectException(InvalidIndexException::class);
$this->expectExceptionMessage('The index for an enum must be an int but NULL given');
new BoolEnum('TRUE', 'true', null);
}
/** @test */
public function can_not_create_new_instance_with_invalid_name()
{
$this->expectException(InvalidNameException::class);
$this->expectExceptionMessage('The given name [FOOBAR] is not available in this enum Spatie\Enum\Tests\Enums\BoolEnum');
new BoolEnum('FOOBAR', 'true', 0);
}
/** @test */
public function can_not_create_new_instance_with_invalid_value()
{
$this->expectException(InvalidValueException::class);
$this->expectExceptionMessage('The given value [foobar] is not available in this enum Spatie\Enum\Tests\Enums\BoolEnum');
new BoolEnum('TRUE', 'foobar', 0);
}
/** @test */
public function can_not_make_new_instance_with_invalid_value()
{
$this->expectException(InvalidValueException::class);
$this->expectExceptionMessage('The given value [foobar] is not available in this enum Spatie\Enum\Tests\Enums\BoolEnum');
BoolEnum::make('foobar');
}
/** @test */
public function can_not_create_new_instance_with_invalid_index()
{
$this->expectException(InvalidIndexException::class);
$this->expectExceptionMessage('The given index [2] is not available in this enum Spatie\Enum\Tests\Enums\BoolEnum');
new BoolEnum('FALSE', 'false', 2);
}
/** @test */
public function can_not_make_new_instance_with_invalid_index()
{
$this->expectException(InvalidIndexException::class);
$this->expectExceptionMessage('The given index [2] is not available in this enum Spatie\Enum\Tests\Enums\BoolEnum');
BoolEnum::make(2);
}
/** @test */
public function can_not_make_new_instance_with_float()
{
$this->expectException(TypeError::class);
$this->expectExceptionMessage('Spatie\Enum\Tests\Enums\BoolEnum::make() expects string|int as argument but double given');
BoolEnum::make(2.5);
}
/** @test */
public function can_not_make_new_instance_with_array()
{
$this->expectException(TypeError::class);
$this->expectExceptionMessage('Spatie\Enum\Tests\Enums\BoolEnum::make() expects string|int as argument but array given');
BoolEnum::make([]);
}
/** @test */
public function can_not_make_new_instance_with_object()
{
$this->expectException(TypeError::class);
$this->expectExceptionMessage('Spatie\Enum\Tests\Enums\BoolEnum::make() expects string|int as argument but object given');
BoolEnum::make(new stdClass());
}
/** @test */
public function can_not_call_undefined_method()
{
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('Call to undefined method Spatie\Enum\Tests\Enums\BoolEnum->foobar()');
BoolEnum::true()->foobar();
}
/** @test */
public function can_not_call_undefined_static_method()
{
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('Call to undefined method Spatie\Enum\Tests\Enums\BoolEnum::foobar()');
BoolEnum::foobar();
}
/** @test */
public function can_not_check_for_static_equality_without_argument()
{
$this->expectException(ArgumentCountError::class);
$this->expectExceptionMessage('Calling Spatie\Enum\Tests\Enums\BoolEnum::isFalse() in static context requires one argument');
BoolEnum::isFalse();
}
/** @test */
public function can_not_check_for_static_equality_with_invalid_value()
{
$this->expectException(InvalidValueException::class);
$this->expectExceptionMessage('The given value [foobar] is not available in this enum Spatie\Enum\Tests\Enums\BoolEnum');
BoolEnum::isFalse('foobar');
}
/** @test */
public function can_not_check_for_static_equality_with_invalid_index()
{
$this->expectException(InvalidIndexException::class);
$this->expectExceptionMessage('The given index [2] is not available in this enum Spatie\Enum\Tests\Enums\BoolEnum');
BoolEnum::isFalse(2);
}
/** @test */
public function can_call_public_nonstatic_method()
{
$this->assertTrue(BoolEnum::true()->testMethod());
}
}