Skip to content

Commit 48eab35

Browse files
committed
tests for ContentTypeNormalizer
1 parent 7355098 commit 48eab35

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

api/tests/Api/ContentTypes/ReadContentTypeTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public function testGetSingleContentTypeIsAllowedForAnonymousUser() {
1818
'id' => $contentType->getId(),
1919
'name' => $contentType->name,
2020
'active' => $contentType->active,
21+
'entityClass' => 'App\\Entity\\ContentNode\\SingleText',
22+
'entityPath' => '/content_node/single_texts',
2123
]);
2224
}
2325

@@ -30,6 +32,8 @@ public function testGetSingleContentTypeIsAllowedForLoggedInUser() {
3032
'id' => $contentType->getId(),
3133
'name' => $contentType->name,
3234
'active' => $contentType->active,
35+
'entityClass' => 'App\\Entity\\ContentNode\\SingleText',
36+
'entityPath' => '/content_node/single_texts',
3337
]);
3438
}
3539
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace App\Tests\Serializer\Normalizer;
4+
5+
use ApiPlatform\Core\Bridge\Symfony\Routing\RouteNameResolverInterface;
6+
use App\Entity\ContentType;
7+
use App\Serializer\Normalizer\ContentTypeNormalizer;
8+
use PHPUnit\Framework\MockObject\MockObject;
9+
use PHPUnit\Framework\TestCase;
10+
use Symfony\Component\Routing\RouterInterface;
11+
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
12+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
13+
use Symfony\Component\Serializer\SerializerInterface;
14+
15+
/**
16+
* @internal
17+
*/
18+
class ContentTypeNormalizerTest extends TestCase {
19+
private ContentTypeNormalizer $normalizer;
20+
21+
private MockObject|NormalizerInterface $decoratedMock;
22+
private MockObject|RouteNameResolverInterface $routeNameResolver;
23+
private MockObject|RouterInterface $routerMock;
24+
25+
protected function setUp(): void {
26+
$this->decoratedMock = $this->createMock(ContextAwareNormalizerInterface::class);
27+
$this->routeNameResolver = $this->createMock(RouteNameResolverInterface::class);
28+
$this->routerMock = $this->createMock(RouterInterface::class);
29+
30+
$this->normalizer = new ContentTypeNormalizer(
31+
$this->decoratedMock,
32+
$this->routeNameResolver,
33+
$this->routerMock,
34+
);
35+
$this->normalizer->setSerializer($this->createMock(SerializerInterface::class));
36+
}
37+
38+
public function testDelegatesSupportCheckToDecorated() {
39+
$this->decoratedMock
40+
->expects($this->exactly(2))
41+
->method('supportsNormalization')
42+
->willReturnOnConsecutiveCalls(true, false)
43+
;
44+
45+
$this->assertTrue($this->normalizer->supportsNormalization([]));
46+
$this->assertFalse($this->normalizer->supportsNormalization([]));
47+
}
48+
49+
public function testDelegatesNormalizeToDecorated() {
50+
// given
51+
$resource = new \stdClass();
52+
$delegatedResult = [
53+
'hello' => 'world',
54+
];
55+
$this->decoratedMock->expects($this->once())
56+
->method('normalize')
57+
->willReturn($delegatedResult)
58+
;
59+
60+
// when
61+
$result = $this->normalizer->normalize($resource, null, ['resource_class' => DummyEntity::class]);
62+
63+
// then
64+
$this->assertEquals($delegatedResult, $result);
65+
}
66+
67+
public function testNormalizeAddsEntityPath() {
68+
// given
69+
$resource = new ContentType();
70+
$delegatedResult = [
71+
'hello' => 'world',
72+
'entityClass' => 'DummyClass',
73+
];
74+
$this->decoratedMock->expects($this->once())
75+
->method('normalize')
76+
->willReturn($delegatedResult)
77+
;
78+
$this->routerMock->expects($this->once())
79+
->method('generate')
80+
->willReturn('/path')
81+
;
82+
$this->routeNameResolver->expects($this->once())
83+
->method('getRouteName')
84+
;
85+
86+
// when
87+
$result = $this->normalizer->normalize($resource, null, ['resource_class' => DummyEntity::class]);
88+
89+
// then
90+
$expectedResult = [
91+
'hello' => 'world',
92+
'entityClass' => 'DummyClass',
93+
'entityPath' => '/path',
94+
];
95+
$this->assertEquals($expectedResult, $result);
96+
}
97+
}

0 commit comments

Comments
 (0)