1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* User: benedikt |
6
|
|
|
* Date: 10/1/17 |
7
|
|
|
* Time: 11:03 AM |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Tfboe\FmLib\Tests\Unit\Helpers; |
11
|
|
|
|
12
|
|
|
use Tfboe\FmLib\Exceptions\ValueNotValid; |
13
|
|
|
use Tfboe\FmLib\Helpers\BasicEnum; |
14
|
|
|
use Tfboe\FmLib\TestHelpers\TestEnum; |
15
|
|
|
use Tfboe\FmLib\Tests\Helpers\UnitTestCase; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class BasicEnumTest |
19
|
|
|
* @package Tfboe\FmLib\TestHelpers |
20
|
|
|
*/ |
21
|
|
|
class BasicEnumTest extends UnitTestCase |
22
|
|
|
{ |
23
|
|
|
//<editor-fold desc="Public Methods"> |
24
|
|
|
/** |
25
|
|
|
* @covers \Tfboe\FmLib\Helpers\BasicEnum::ensureValidValue |
26
|
|
|
* @uses \Tfboe\FmLib\Exceptions\ValueNotValid::__construct |
27
|
|
|
* @uses \Tfboe\FmLib\Helpers\BasicEnum::getConstants |
28
|
|
|
* @uses \Tfboe\FmLib\Helpers\BasicEnum::getValues |
29
|
|
|
* @uses \Tfboe\FmLib\Helpers\BasicEnum::isValidValue |
30
|
|
|
*/ |
31
|
|
|
public function testEnsureValidValueException() |
32
|
|
|
{ |
33
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
34
|
|
|
TestEnum::ensureValidValue(TestEnum::INT_KEY); |
35
|
|
|
$this->expectException(ValueNotValid::class); |
36
|
|
|
$this->expectExceptionMessage( |
37
|
|
|
'The following value is not valid: "1" in Tfboe\FmLib\TestHelpers\TestEnum. Possible values: "value", 1.'); |
38
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
39
|
|
|
TestEnum::ensureValidValue('1'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @covers \Tfboe\FmLib\Helpers\BasicEnum::getNames |
44
|
|
|
* @covers \Tfboe\FmLib\Helpers\BasicEnum::getConstants |
45
|
|
|
*/ |
46
|
|
|
public function testGetNames() |
47
|
|
|
{ |
48
|
|
|
self::assertEquals(['KEY', 'INT_KEY'], TestEnum::getNames()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @covers \Tfboe\FmLib\Helpers\BasicEnum::getName |
53
|
|
|
* @covers \Tfboe\FmLib\Helpers\BasicEnum::getNamesArray |
54
|
|
|
* @uses \Tfboe\FmLib\Helpers\BasicEnum::getConstants |
55
|
|
|
*/ |
56
|
|
|
public function testGetName() |
57
|
|
|
{ |
58
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
59
|
|
|
self::assertEquals("KEY", TestEnum::getName(TestEnum::KEY)); |
60
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
61
|
|
|
self::assertEquals("INT_KEY", TestEnum::getName(TestEnum::INT_KEY)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @covers \Tfboe\FmLib\Helpers\BasicEnum::getName |
66
|
|
|
* @covers \Tfboe\FmLib\Helpers\BasicEnum::getNamesArray |
67
|
|
|
* @uses \Tfboe\FmLib\Exceptions\ValueNotValid::__construct |
68
|
|
|
* @uses \Tfboe\FmLib\Helpers\BasicEnum::getConstants |
69
|
|
|
* @uses \Tfboe\FmLib\Helpers\BasicEnum::getValues |
70
|
|
|
*/ |
71
|
|
|
public function testGetNameException() |
72
|
|
|
{ |
73
|
|
|
$this->expectException(ValueNotValid::class); |
74
|
|
|
$this->expectExceptionMessage('The following value is not valid: "int_key" in Tfboe\FmLib\TestHelpers\TestEnum.' . |
75
|
|
|
' Possible values: "value", 1.'); |
76
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
77
|
|
|
TestEnum::getName('int_key'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @covers \Tfboe\FmLib\Helpers\BasicEnum::getValue |
82
|
|
|
* @covers \Tfboe\FmLib\Helpers\BasicEnum::getCaseMapping |
83
|
|
|
* @uses \Tfboe\FmLib\Helpers\BasicEnum::getConstants |
84
|
|
|
* @uses \Tfboe\FmLib\Helpers\BasicEnum::getNames |
85
|
|
|
*/ |
86
|
|
|
public function testGetValue() |
87
|
|
|
{ |
88
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
89
|
|
|
self::assertEquals("value", TestEnum::getValue('KEY')); |
90
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
91
|
|
|
self::assertEquals(1, TestEnum::getValue('int_key')); |
92
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
93
|
|
|
self::assertEquals(1, TestEnum::getValue('INT_KEY', True)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @covers \Tfboe\FmLib\Helpers\BasicEnum::getValue |
98
|
|
|
* @uses \Tfboe\FmLib\Exceptions\ValueNotValid::__construct |
99
|
|
|
* @uses \Tfboe\FmLib\Helpers\BasicEnum::getConstants |
100
|
|
|
* @uses \Tfboe\FmLib\Helpers\BasicEnum::getValues |
101
|
|
|
*/ |
102
|
|
|
public function testGetValueException() |
103
|
|
|
{ |
104
|
|
|
$this->expectException(ValueNotValid::class); |
105
|
|
|
$this->expectExceptionMessage('The following value is not valid: "int_key" in Tfboe\FmLib\TestHelpers\TestEnum.' . |
106
|
|
|
' Possible values: "value", 1.'); |
107
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
108
|
|
|
TestEnum::getValue('int_key', True); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @covers \Tfboe\FmLib\Helpers\BasicEnum::getValues |
113
|
|
|
* @uses \Tfboe\FmLib\Helpers\BasicEnum::getConstants |
114
|
|
|
*/ |
115
|
|
|
public function testGetValues() |
116
|
|
|
{ |
117
|
|
|
self::assertEquals(['value', 1], TestEnum::getValues()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @covers \Tfboe\FmLib\Helpers\BasicEnum::isValidName |
122
|
|
|
* @uses \Tfboe\FmLib\Helpers\BasicEnum::getConstants |
123
|
|
|
*/ |
124
|
|
|
public function testIsValidName() |
125
|
|
|
{ |
126
|
|
|
self::assertTrue(TestEnum::isValidName('KEY')); |
127
|
|
|
self::assertTrue(TestEnum::isValidName('int_key')); |
128
|
|
|
self::assertFalse(TestEnum::isValidName('INT-KEY')); |
129
|
|
|
|
130
|
|
|
self::assertTrue(TestEnum::isValidName('INT_KEY', True)); |
131
|
|
|
self::assertFalse(TestEnum::isValidName('int_key', True)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @covers \Tfboe\FmLib\Helpers\BasicEnum::isValidValue |
136
|
|
|
* @uses \Tfboe\FmLib\Helpers\BasicEnum::getConstants |
137
|
|
|
* @uses \Tfboe\FmLib\Helpers\BasicEnum::getValues |
138
|
|
|
*/ |
139
|
|
|
public function testIsValidValue() |
140
|
|
|
{ |
141
|
|
|
self::assertTrue(TestEnum::isValidValue('value')); |
142
|
|
|
self::assertTrue(TestEnum::isValidValue(1)); |
143
|
|
|
self::assertFalse(TestEnum::isValidValue('1')); |
144
|
|
|
self::assertFalse(TestEnum::isValidValue('VALUE')); |
145
|
|
|
|
146
|
|
|
self::assertTrue(TestEnum::isValidValue('1', False)); |
147
|
|
|
self::assertFalse(TestEnum::isValidValue('VALUE', False)); |
148
|
|
|
} |
149
|
|
|
//</editor-fold desc="Public Methods"> |
150
|
|
|
|
151
|
|
|
//<editor-fold desc="Protected Methods"> |
152
|
|
|
/** |
153
|
|
|
* @before |
154
|
|
|
*/ |
155
|
|
|
protected function clearStaticVariables() |
156
|
|
|
{ |
157
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
158
|
|
|
self::getProperty(BasicEnum::class, 'constCacheArray')->setValue(NULL); |
159
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
160
|
|
|
self::getProperty(BasicEnum::class, 'constCacheArrayCaseMapping')->setValue(NULL); |
161
|
|
|
} |
162
|
|
|
//</editor-fold desc="Protected Methods"> |
163
|
|
|
} |