|
3 | 3 |
|
4 | 4 | namespace Nelexa\Tests; |
5 | 5 |
|
6 | | -use Nelexa\Tests\Enums\ExampleEnum; |
7 | 6 | use Nelexa\Tests\Enums\EnumExtended; |
| 7 | +use Nelexa\Tests\Enums\ExampleEnum; |
8 | 8 | use Nelexa\Tests\Enums\OverrideToStringEnum; |
9 | 9 | use PHPUnit\Framework\TestCase; |
10 | 10 |
|
@@ -195,4 +195,56 @@ public function testEnumProperty(): void |
195 | 195 |
|
196 | 196 | unset($enum->new_field_name); // __unset invoke |
197 | 197 | } |
| 198 | + |
| 199 | + /** |
| 200 | + * @dataProvider provideEnumFromValue |
| 201 | + * |
| 202 | + * @param string $name |
| 203 | + * @param mixed $value |
| 204 | + */ |
| 205 | + public function testEnumFromValue(string $name, $value): void |
| 206 | + { |
| 207 | + $enum = ExampleEnum::valueOf($name); |
| 208 | + |
| 209 | + $this->assertSame(ExampleEnum::fromValue($value), $enum); |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * @return array |
| 214 | + */ |
| 215 | + public function provideEnumFromValue(): array |
| 216 | + { |
| 217 | + return [ |
| 218 | + ['VALUE_INT', ExampleEnum::VALUE_INT], |
| 219 | + ['VALUE_INT_1000', ExampleEnum::VALUE_INT_1000], |
| 220 | + ['VALUE_STRING', ExampleEnum::VALUE_STRING], |
| 221 | + ['VALUE_BOOL_TRUE', ExampleEnum::VALUE_BOOL_TRUE], |
| 222 | + ['VALUE_BOOL_FALSE', ExampleEnum::VALUE_BOOL_FALSE], |
| 223 | + ['VALUE_FLOAT', ExampleEnum::VALUE_FLOAT], |
| 224 | + ['VALUE_NULL', ExampleEnum::VALUE_NULL], |
| 225 | + ['VALUE_EMPTY_STRING', ExampleEnum::VALUE_EMPTY_STRING], |
| 226 | + ['SCALAR_EXPRESSION', ExampleEnum::SCALAR_EXPRESSION], |
| 227 | + ['CONST', ExampleEnum::CONST], |
| 228 | + ['PUBLIC_CONST', ExampleEnum::PUBLIC_CONST], |
| 229 | + ['PRIVATE_CONST', 'private'], |
| 230 | + ['PROTECTED_CONST', 'protected'], |
| 231 | + ['LANG_CODES', ExampleEnum::LANG_CODES], |
| 232 | + ['COUNTRY_CODES', ExampleEnum::COUNTRY_CODES], |
| 233 | + ]; |
| 234 | + } |
| 235 | + |
| 236 | + public function testEnumFromValueForSameValues(): void |
| 237 | + { |
| 238 | + $this->assertSame(ExampleEnum::fromValue(ExampleEnum::VALUE_EQUALS_1), ExampleEnum::VALUE_EQUALS_1()); |
| 239 | + $this->assertSame(ExampleEnum::fromValue(ExampleEnum::VALUE_EQUALS_2), ExampleEnum::VALUE_EQUALS_1()); |
| 240 | + $this->assertNotSame(ExampleEnum::fromValue(ExampleEnum::VALUE_EQUALS_2), ExampleEnum::VALUE_EQUALS_2()); |
| 241 | + } |
| 242 | + |
| 243 | + public function testEnumFromValueIncorrectValue(): void |
| 244 | + { |
| 245 | + $this->expectException(\InvalidArgumentException::class); |
| 246 | + $this->expectExceptionMessage('Constant value "Unknown value" is not defined'); |
| 247 | + |
| 248 | + ExampleEnum::fromValue('Unknown value'); |
| 249 | + } |
198 | 250 | } |
0 commit comments