Skip to content

Commit 977c6bd

Browse files
committed
Add normalizer fixture to make test implementation easier + fix CS
1 parent 3589b0e commit 977c6bd

3 files changed

Lines changed: 35 additions & 50 deletions

File tree

src/Normalizer/SimpleNormalizer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Torr\SimpleNormalizer\Normalizer;
44

5-
use Doctrine\Common\Util\ClassUtils;
65
use Doctrine\ORM\EntityManagerInterface;
76
use Doctrine\ORM\Mapping\ClassMetadataFactory;
87
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
@@ -136,6 +135,10 @@ private function recursiveNormalize (mixed $value, array $context = []) : mixed
136135

137136
/**
138137
* Normalizes the class name
138+
*
139+
* @param class-string $className
140+
*
141+
* @return class-string
139142
*/
140143
private function normalizeClassName (string $className) : string
141144
{
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Tests\Torr\SimpleNormalizer\Fixture;
4+
5+
use Torr\SimpleNormalizer\Normalizer\SimpleNormalizer;
6+
use Torr\SimpleNormalizer\Normalizer\SimpleObjectNormalizerInterface;
7+
8+
/**
9+
* @final
10+
*/
11+
readonly class DummyVONormalizer implements SimpleObjectNormalizerInterface
12+
{
13+
public function __construct (
14+
private mixed $returnValue = null,
15+
) {}
16+
17+
public function normalize (object $value, array $context, SimpleNormalizer $normalizer) : mixed
18+
{
19+
return $this->returnValue;
20+
}
21+
22+
public static function getNormalizedType () : string
23+
{
24+
return DummyVO::class;
25+
}
26+
}

tests/Normalizer/SimpleNormalizerTest.php

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
use PHPUnit\Framework\TestCase;
1010
use Symfony\Component\DependencyInjection\ServiceLocator;
1111
use Tests\Torr\SimpleNormalizer\Fixture\DummyVO;
12+
use Tests\Torr\SimpleNormalizer\Fixture\DummyVONormalizer;
1213
use Torr\SimpleNormalizer\Exception\IncompleteNormalizationException;
1314
use Torr\SimpleNormalizer\Normalizer\SimpleNormalizer;
14-
use Torr\SimpleNormalizer\Normalizer\SimpleObjectNormalizerInterface;
1515
use Torr\SimpleNormalizer\Normalizer\Validator\ValidJsonVerifier;
1616

1717
/**
@@ -136,17 +136,7 @@ public function testWithoutEntityManager () : void
136136
$locator->expects(self::once())
137137
->method("get")
138138
->with(DummyVO::class)
139-
->willReturn(new readonly class implements SimpleObjectNormalizerInterface {
140-
public function normalize (object $value, array $context, SimpleNormalizer $normalizer) : int
141-
{
142-
return 5;
143-
}
144-
145-
public static function getNormalizedType () : string
146-
{
147-
return DummyVO::class;
148-
}
149-
});
139+
->willReturn(new DummyVONormalizer(5));
150140

151141
$normalizer = new SimpleNormalizer(
152142
objectNormalizers: $locator,
@@ -176,17 +166,7 @@ public function testWithEntityManagerButNoMapping () : void
176166
$locator->expects(self::once())
177167
->method("get")
178168
->with(DummyVO::class)
179-
->willReturn(new readonly class implements SimpleObjectNormalizerInterface {
180-
public function normalize (object $value, array $context, SimpleNormalizer $normalizer) : int
181-
{
182-
return 5;
183-
}
184-
185-
public static function getNormalizedType () : string
186-
{
187-
return DummyVO::class;
188-
}
189-
});
169+
->willReturn(new DummyVONormalizer(5));
190170

191171
$normalizer = new SimpleNormalizer(
192172
objectNormalizers: $locator,
@@ -225,17 +205,7 @@ public function testWithEntityManagerWithMapping () : void
225205
$locator->expects(self::once())
226206
->method("get")
227207
->with("SomeClass")
228-
->willReturn(new readonly class implements SimpleObjectNormalizerInterface {
229-
public function normalize (object $value, array $context, SimpleNormalizer $normalizer) : int
230-
{
231-
return 5;
232-
}
233-
234-
public static function getNormalizedType () : string
235-
{
236-
return DummyVO::class;
237-
}
238-
});
208+
->willReturn(new DummyVONormalizer(5));
239209

240210
$normalizer = new SimpleNormalizer(
241211
objectNormalizers: $locator,
@@ -253,21 +223,7 @@ public static function getNormalizedType () : string
253223
private function createNormalizerObjectNormalizers (mixed $returnValue) : ServiceLocator
254224
{
255225
return new ServiceLocator([
256-
DummyVO::class => static fn () => new readonly class($returnValue) implements SimpleObjectNormalizerInterface {
257-
public function __construct (
258-
private mixed $returnValue,
259-
) {}
260-
261-
public function normalize (object $value, array $context, SimpleNormalizer $normalizer) : mixed
262-
{
263-
return $this->returnValue;
264-
}
265-
266-
public static function getNormalizedType () : string
267-
{
268-
return DummyVO::class;
269-
}
270-
},
226+
DummyVO::class => static fn () => new DummyVONormalizer($returnValue),
271227
]);
272228
}
273229
}

0 commit comments

Comments
 (0)