1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace DeSmart \Enum \Tests ;
6+
7+ use DeSmart \Enum \Enumeration ;
8+ use PHPUnit \Framework \TestCase ;
9+
10+ class EnumerationTest extends TestCase
11+ {
12+ /** @test */
13+ public function it_should_create_enum (): void
14+ {
15+ $ this ->assertInstanceOf (Character::class, Character::good ());
16+ $ this ->assertEquals ('good ' , Character::good ()->getValue ());
17+ $ this ->assertEquals ('good ' , Character::GOOD ()->getValue ());
18+ $ this ->assertEquals ('good ' , Character::fromName ('good ' )->getValue ());
19+ $ this ->assertEquals ('good ' , Character::fromValue ('good ' )->getValue ());
20+ $ this ->assertEquals ('evil ' , Character::evil ()->getValue ());
21+ $ this ->assertEquals ('evil ' , Character::EVIL ()->getValue ());
22+ $ this ->assertEquals ('evil ' , Character::fromName ('evil ' )->getValue ());
23+ $ this ->assertEquals ('evil ' , Character::fromValue ('evil ' )->getValue ());
24+ $ this ->assertEquals ('sometimes_good_or_evil ' , Character::sometimesGoodSometimesEvil ()->getValue ());
25+ $ this ->assertEquals ('sometimes_good_or_evil ' , Character::SOMETIMES_GOOD_SOMETIMES_EVIL ()->getValue ());
26+ $ this ->assertEquals ('sometimes_good_or_evil ' , Character::sometimes_good_sometimes_evil ()->getValue ());
27+ $ this ->assertEquals ('sometimes_good_or_evil ' , Character::fromName ('sometimes_good_sometimes_evil ' )->getValue ());
28+ $ this ->assertEquals ('sometimes_good_or_evil ' , Character::fromValue ('sometimes_good_or_evil ' )->getValue ());
29+ $ this ->assertTrue (Character::good ()->equals (Character::good ()));
30+ $ this ->assertTrue (Character::good ()->equals (Character::fromName ('good ' )));
31+ $ this ->assertTrue (Character::good ()->equals (Character::fromValue ('good ' )));
32+ }
33+
34+ /** @test */
35+ public function it_should_throw_exception_when_unknown_const_name (): void
36+ {
37+ $ this ->expectException (\BadMethodCallException::class);
38+ $ this ->expectErrorMessage ('Constant DeSmart\Enum\Tests\Character::NON_EXISTING does not exist. ' );
39+
40+ Character::fromName ('non_existing ' );
41+ }
42+
43+ /** @test */
44+ public function it_should_throw_exception_when_unknown_const_value (): void
45+ {
46+ $ this ->expectException (\UnexpectedValueException::class);
47+ $ this ->expectErrorMessage ('Constant with value non_existing in enum class DeSmart\Enum\Tests\Character does not exist. ' );
48+
49+ Character::fromValue ('non_existing ' );
50+ }
51+
52+ /** @test */
53+ public function it_should_throw_error_when_value_type_is_disallowed (): void
54+ {
55+ $ this ->expectException (\TypeError::class);
56+ $ this ->expectErrorMessage ('Enum value can only be integer or string. ' );
57+
58+ Character::fromValue (['non_existing ' ]);
59+ }
60+ }
61+
62+ /**
63+ * @method static Character good()
64+ * @method static Character evil()
65+ * @method static Character sometimesGoodSometimesEvil()
66+ */
67+ class Character extends Enumeration
68+ {
69+ const GOOD = 'good ' ;
70+ const EVIL = 'evil ' ;
71+ const SOMETIMES_GOOD_SOMETIMES_EVIL = 'sometimes_good_or_evil ' ;
72+ }
0 commit comments