-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathBackedEnumNormalizerTest.php
129 lines (105 loc) · 5.71 KB
/
BackedEnumNormalizerTest.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Tests\Normalizer;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
use Symfony\Component\Serializer\Tests\Fixtures\IntegerBackedEnumDummy;
use Symfony\Component\Serializer\Tests\Fixtures\StringBackedEnumDummy;
use Symfony\Component\Serializer\Tests\Fixtures\UnitEnumDummy;
/**
* @author Alexandre Daubois <[email protected]>
*/
class BackedEnumNormalizerTest extends TestCase
{
private BackedEnumNormalizer $normalizer;
protected function setUp(): void
{
$this->normalizer = new BackedEnumNormalizer();
}
public function testSupportsNormalization()
{
$this->assertTrue($this->normalizer->supportsNormalization(StringBackedEnumDummy::GET));
$this->assertTrue($this->normalizer->supportsNormalization(IntegerBackedEnumDummy::SUCCESS));
$this->assertFalse($this->normalizer->supportsNormalization(UnitEnumDummy::GET));
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
}
public function testNormalize()
{
$this->assertSame('GET', $this->normalizer->normalize(StringBackedEnumDummy::GET));
$this->assertSame(200, $this->normalizer->normalize(IntegerBackedEnumDummy::SUCCESS));
}
public function testNormalizeBadObjectTypeThrowsException()
{
$this->expectException(InvalidArgumentException::class);
$this->normalizer->normalize(new \stdClass());
}
public function testSupportsDenormalization()
{
$this->assertTrue($this->normalizer->supportsDenormalization(null, StringBackedEnumDummy::class));
$this->assertTrue($this->normalizer->supportsDenormalization(null, IntegerBackedEnumDummy::class));
$this->assertFalse($this->normalizer->supportsDenormalization(null, UnitEnumDummy::class));
$this->assertFalse($this->normalizer->supportsDenormalization(null, \stdClass::class));
}
public function testDenormalize()
{
$this->assertSame(StringBackedEnumDummy::GET, $this->normalizer->denormalize('GET', StringBackedEnumDummy::class));
$this->assertSame(IntegerBackedEnumDummy::SUCCESS, $this->normalizer->denormalize(200, IntegerBackedEnumDummy::class));
}
public function testDenormalizeNullValueThrowsException()
{
$this->expectException(NotNormalizableValueException::class);
$this->normalizer->denormalize(null, StringBackedEnumDummy::class);
}
public function testDenormalizeBooleanValueThrowsException()
{
$this->expectException(NotNormalizableValueException::class);
$this->normalizer->denormalize(true, StringBackedEnumDummy::class);
}
public function testDenormalizeObjectThrowsException()
{
$this->expectException(NotNormalizableValueException::class);
$this->normalizer->denormalize(new \stdClass(), StringBackedEnumDummy::class);
}
public function testDenormalizeBadBackingValueThrowsException()
{
$this->expectException(NotNormalizableValueException::class);
$this->expectExceptionMessage('The data must belong to a backed enumeration of type '.StringBackedEnumDummy::class);
$this->normalizer->denormalize('POST', StringBackedEnumDummy::class);
}
public function testNormalizeShouldThrowExceptionForNonEnumObjects()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The data must belong to a backed enumeration.');
$this->normalizer->normalize(\stdClass::class);
}
public function testDenormalizeShouldThrowExceptionForNonEnumObjects()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The data must belong to a backed enumeration.');
$this->normalizer->denormalize('GET', \stdClass::class);
}
public function testSupportsNormalizationShouldFailOnAnyPHPVersionForNonEnumObjects()
{
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
}
public function testItUsesTryFromIfContextIsPassed()
{
$this->assertNull($this->normalizer->denormalize(1, IntegerBackedEnumDummy::class, null, [BackedEnumNormalizer::ALLOW_INVALID_VALUES => true]));
$this->assertNull($this->normalizer->denormalize('', IntegerBackedEnumDummy::class, null, [BackedEnumNormalizer::ALLOW_INVALID_VALUES => true]));
$this->assertNull($this->normalizer->denormalize(null, IntegerBackedEnumDummy::class, null, [BackedEnumNormalizer::ALLOW_INVALID_VALUES => true]));
$this->assertSame(IntegerBackedEnumDummy::SUCCESS, $this->normalizer->denormalize(200, IntegerBackedEnumDummy::class, null, [BackedEnumNormalizer::ALLOW_INVALID_VALUES => true]));
$this->assertNull($this->normalizer->denormalize(1, StringBackedEnumDummy::class, null, [BackedEnumNormalizer::ALLOW_INVALID_VALUES => true]));
$this->assertNull($this->normalizer->denormalize('foo', StringBackedEnumDummy::class, null, [BackedEnumNormalizer::ALLOW_INVALID_VALUES => true]));
$this->assertNull($this->normalizer->denormalize(null, StringBackedEnumDummy::class, null, [BackedEnumNormalizer::ALLOW_INVALID_VALUES => true]));
$this->assertSame(StringBackedEnumDummy::GET, $this->normalizer->denormalize('GET', StringBackedEnumDummy::class, null, [BackedEnumNormalizer::ALLOW_INVALID_VALUES => true]));
}
}