-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FactoryTest.php
64 lines (51 loc) · 1.59 KB
/
FactoryTest.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
<?php
declare(strict_types=1);
namespace Bakame\Aide\Enum;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use ValueError;
final class FactoryTest extends TestCase
{
#[Test]
public function it_can_instantiate_a_non_backed_enum_from_name(): void
{
self::assertSame(HttpMethod::Get, HttpMethod::tryFromName('Get'));
self::assertSame(HttpMethod::Get, HttpMethod::tryFrom('Get'));
self::assertSame(HttpMethod::Get, HttpMethod::from('Get'));
self::assertNull(HttpMethod::tryFrom('Unknown'));
}
#[Test]
public function it_can_instantiate_a_backed_enum_from_name(): void
{
self::assertSame(HttpMethodString::Get, HttpMethodString::tryFromName('Get'));
self::assertSame(HttpMethodString::Get, HttpMethodString::fromName('Get'));
self::assertSame(HttpMethodString::Get, HttpMethodString::tryFrom('GET'));
self::assertSame(HttpMethodString::Get, HttpMethodString::from('GET'));
self::assertNull(HttpMethodString::tryFrom('Get'));
}
#[Test]
public function it_fails_if_the_name_is_unknown(): void
{
$this->expectException(ValueError::class);
$this->expectExceptionMessage('"GET" is not a valid name for "'.HttpMethod::class.'" enumeration.');
HttpMethod::fromName('GET');
}
}
enum HttpMethod
{
use Factory;
case Get;
case Post;
case Put;
case Head;
case Options;
}
enum HttpMethodString: string
{
use Factory;
case Get = 'GET';
case Post = 'POST';
case Put = 'PUT';
case Head = 'HEAD';
case Options = 'OPTIONS';
}