-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEnumNormalizerTest.php
More file actions
115 lines (91 loc) · 3.56 KB
/
Copy pathEnumNormalizerTest.php
File metadata and controls
115 lines (91 loc) · 3.56 KB
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
<?php
declare(strict_types=1);
namespace Patchlevel\Hydrator\Tests\Unit\Normalizer;
use Attribute;
use Patchlevel\Hydrator\Normalizer\EnumNormalizer;
use Patchlevel\Hydrator\Normalizer\InvalidArgument;
use Patchlevel\Hydrator\Normalizer\InvalidType;
use Patchlevel\Hydrator\Tests\Unit\Fixture\AnotherEnum;
use Patchlevel\Hydrator\Tests\Unit\Fixture\AutoTypeDto;
use Patchlevel\Hydrator\Tests\Unit\Fixture\Status;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionType;
use RuntimeException;
#[Attribute(Attribute::TARGET_PROPERTY)]
final class EnumNormalizerTest extends TestCase
{
public function testNormalizeWithNull(): void
{
$normalizer = new EnumNormalizer(Status::class);
$this->assertEquals(null, $normalizer->normalize(null));
}
public function testDenormalizeWithNull(): void
{
$normalizer = new EnumNormalizer(Status::class);
$this->assertEquals(null, $normalizer->denormalize(null));
}
public function testNormalizeWithInvalidArgument(): void
{
$this->expectException(InvalidArgument::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage('type "Patchlevel\Hydrator\Tests\Unit\Fixture\Status|null" was expected but "string" was passed.');
$normalizer = new EnumNormalizer(Status::class);
$normalizer->normalize('foo');
}
public function testDenormalizeWithInvalidArgument(): void
{
$this->expectException(InvalidArgument::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage('foo');
$this->expectExceptionMessage('Patchlevel\Hydrator\Tests\Unit\Fixture\Status');
$normalizer = new EnumNormalizer(Status::class);
$normalizer->denormalize('foo');
}
public function testNormalizeWithValue(): void
{
$normalizer = new EnumNormalizer(Status::class);
$this->assertEquals('pending', $normalizer->normalize(Status::Pending));
}
public function testDenormalizeWithValue(): void
{
$normalizer = new EnumNormalizer(Status::class);
$this->assertEquals(Status::Pending, $normalizer->denormalize('pending'));
}
public function testAutoDetect(): void
{
$normalizer = new EnumNormalizer();
$normalizer->handleReflectionType($this->reflectionType(AutoTypeDto::class, 'status'));
self::assertEquals(Status::class, $normalizer->className());
}
public function testAutoDetectOverrideNotPossible(): void
{
$normalizer = new EnumNormalizer(AnotherEnum::class);
$normalizer->handleReflectionType($this->reflectionType(AutoTypeDto::class, 'status'));
self::assertEquals(AnotherEnum::class, $normalizer->className());
}
public function testAutoDetectMissingType(): void
{
$this->expectException(InvalidType::class);
$normalizer = new EnumNormalizer();
$normalizer->className();
}
public function testAutoDetectMissingTypeBecauseNull(): void
{
$this->expectException(InvalidType::class);
$normalizer = new EnumNormalizer();
$normalizer->handleReflectionType(null);
$normalizer->className();
}
/** @param class-string $class */
private function reflectionType(string $class, string $property): ReflectionType
{
$reflection = new ReflectionClass($class);
$property = $reflection->getProperty($property);
$type = $property->getType();
if (!$type instanceof ReflectionType) {
throw new RuntimeException('no type');
}
return $type;
}
}