-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleNormalizerTest.php
More file actions
229 lines (193 loc) · 5.91 KB
/
SimpleNormalizerTest.php
File metadata and controls
229 lines (193 loc) · 5.91 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php declare(strict_types=1);
namespace Tests\Torr\SimpleNormalizer\Normalizer;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Tests\Torr\SimpleNormalizer\Fixture\DummyVO;
use Tests\Torr\SimpleNormalizer\Fixture\DummyVONormalizer;
use Torr\SimpleNormalizer\Exception\IncompleteNormalizationException;
use Torr\SimpleNormalizer\Normalizer\SimpleNormalizer;
use Torr\SimpleNormalizer\Normalizer\Validator\ValidJsonVerifier;
/**
* @internal
*/
final class SimpleNormalizerTest extends TestCase
{
/**
*
*/
public static function provideJsonVerifierEnabled () : iterable
{
yield "normalize" => [
static fn (SimpleNormalizer $normalizer, mixed $value) => $normalizer->normalize($value),
];
yield "normalizeArray" => [
static fn (SimpleNormalizer $normalizer, mixed $value) => $normalizer->normalizeArray($value),
];
yield "normalizeMap" => [
static fn (SimpleNormalizer $normalizer, mixed $value) => $normalizer->normalizeMap($value),
];
}
/**
*
*/
#[DataProvider('provideJsonVerifierEnabled')]
public function testJsonVerifierEnabled (callable $call) : void
{
$verifier = $this->createMock(ValidJsonVerifier::class);
$verifier
->expects(self::once())
->method('ensureValidOnlyJsonTypes');
$normalizer = new SimpleNormalizer(
objectNormalizers: $this->createNormalizerObjectNormalizers("ok"),
isDebug: true,
validJsonVerifier: $verifier,
);
$call($normalizer, [
"nested" => [
"deeply" => [
"ohai" => new DummyVO(42),
],
"valid" => true,
],
]);
}
/**
*
*/
public function testJsonVerifierDisabled () : void
{
$verifier = $this->createMock(ValidJsonVerifier::class);
$verifier
->expects(self::never())
->method('ensureValidOnlyJsonTypes');
$normalizer = new SimpleNormalizer(
objectNormalizers: $this->createNormalizerObjectNormalizers("ok"),
isDebug: false,
validJsonVerifier: $verifier,
);
$normalizer->normalize([
"key" => new DummyVO(42),
]);
self::assertTrue(true); // Just to ensure the test runs without exceptions
}
/**
*
*/
public function testInvalidNormalizer () : void
{
$normalizer = new SimpleNormalizer(
objectNormalizers: $this->createNormalizerObjectNormalizers(new DummyVO(11)),
isDebug: true,
validJsonVerifier: new ValidJsonVerifier(),
);
$this->expectException(IncompleteNormalizationException::class);
$this->expectExceptionMessage("Found a JSON-incompatible value when normalizing. Found 'Tests\Torr\SimpleNormalizer\Fixture\DummyVO' at path '$', but expected only scalars, arrays and empty objects.");
$normalizer->normalize(new DummyVO(42));
}
/**
*
*/
public function testInvalidNestedNormalizer () : void
{
$normalizer = new SimpleNormalizer(
objectNormalizers: $this->createNormalizerObjectNormalizers(new DummyVO(11)),
isDebug: true,
validJsonVerifier: new ValidJsonVerifier(),
);
$this->expectException(IncompleteNormalizationException::class);
$this->expectExceptionMessage("Found a JSON-incompatible value when normalizing. Found 'Tests\Torr\SimpleNormalizer\Fixture\DummyVO' at path '$.nested.deeply.ohai', but expected only scalars, arrays and empty objects.");
$normalizer->normalize([
"nested" => [
"deeply" => [
"ohai" => new DummyVO(42),
],
"valid" => true,
],
]);
}
/**
*/
public function testWithoutEntityManager () : void
{
$locator = $this->createMock(ServiceLocator::class);
$locator->expects(self::once())
->method("get")
->with(DummyVO::class)
->willReturn(new DummyVONormalizer(5));
$normalizer = new SimpleNormalizer(
objectNormalizers: $locator,
isDebug: true,
validJsonVerifier: new ValidJsonVerifier(),
);
$normalizer->normalize(new DummyVO(5));
}
/**
*/
public function testWithEntityManagerButNoMapping () : void
{
$metadataFactory = $this->createMock(ClassMetadataFactory::class);
$metadataFactory
->expects(self::once())
->method("hasMetadataFor")
->with(DummyVO::class)
->willReturn(false);
$entityManager = $this->createMock(EntityManagerInterface::class);
$entityManager->method("getMetadataFactory")->willReturn($metadataFactory);
$locator = $this->createMock(ServiceLocator::class);
$locator->expects(self::once())
->method("get")
->with(DummyVO::class)
->willReturn(new DummyVONormalizer(5));
$normalizer = new SimpleNormalizer(
objectNormalizers: $locator,
isDebug: true,
validJsonVerifier: new ValidJsonVerifier(),
entityManager: $entityManager,
);
$normalizer->normalize(new DummyVO(5));
}
/**
*/
public function testWithEntityManagerWithMapping () : void
{
$classMetaData = new ClassMetadata("SomeClass");
$metadataFactory = $this->createMock(ClassMetadataFactory::class);
$metadataFactory
->expects(self::once())
->method("hasMetadataFor")
->with(DummyVO::class)
->willReturn(true);
$metadataFactory
->expects(self::once())
->method("getMetadataFor")
->with(DummyVO::class)
->willReturn($classMetaData);
$entityManager = $this->createMock(EntityManagerInterface::class);
$entityManager->method("getMetadataFactory")->willReturn($metadataFactory);
$locator = $this->createMock(ServiceLocator::class);
$locator->expects(self::once())
->method("get")
->with("SomeClass")
->willReturn(new DummyVONormalizer(5));
$normalizer = new SimpleNormalizer(
objectNormalizers: $locator,
isDebug: true,
validJsonVerifier: new ValidJsonVerifier(),
entityManager: $entityManager,
);
$normalizer->normalize(new DummyVO(5));
}
/**
* @return ServiceLocator<mixed>
*/
private function createNormalizerObjectNormalizers (mixed $returnValue) : ServiceLocator
{
return new ServiceLocator([
DummyVO::class => static fn () => new DummyVONormalizer($returnValue),
]);
}
}