-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleNormalizerTest.php
More file actions
152 lines (128 loc) · 3.91 KB
/
SimpleNormalizerTest.php
File metadata and controls
152 lines (128 loc) · 3.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
<?php declare(strict_types=1);
namespace Tests\Torr\SimpleNormalizer\Normalizer;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Tests\Torr\SimpleNormalizer\Fixture\DummyVO;
use Torr\SimpleNormalizer\Exception\IncompleteNormalizationException;
use Torr\SimpleNormalizer\Normalizer\SimpleNormalizer;
use PHPUnit\Framework\TestCase;
use Torr\SimpleNormalizer\Normalizer\SimpleObjectNormalizerInterface;
use Torr\SimpleNormalizer\Normalizer\Validator\ValidJsonVerifier;
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($this->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($this->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,
],
]);
}
/**
*
*/
private function createNormalizerObjectNormalizers (mixed $returnValue) : ServiceLocator
{
return new ServiceLocator([
DummyVO::class => static fn () => new readonly class ($returnValue) implements SimpleObjectNormalizerInterface
{
public function __construct (
private mixed $returnValue,
) {}
public function normalize (object $value, array $context, SimpleNormalizer $normalizer) : mixed
{
return $this->returnValue;
}
public static function getNormalizedType () : string
{
return DummyVO::class;
}
},
]);
}
}