2
2
3
3
namespace App \Tests \Serializer \Normalizer ;
4
4
5
+ use ApiPlatform \Core \Api \IriConverterInterface ;
5
6
use ApiPlatform \Core \Bridge \Symfony \Routing \RouteNameResolverInterface ;
6
7
use App \Entity \ContentType ;
8
+ use App \Metadata \Resource \Factory \UriTemplateFactory ;
7
9
use App \Serializer \Normalizer \ContentTypeNormalizer ;
8
10
use PHPUnit \Framework \MockObject \MockObject ;
9
11
use PHPUnit \Framework \TestCase ;
12
+ use Rize \UriTemplate ;
10
13
use Symfony \Component \Routing \RouterInterface ;
11
14
use Symfony \Component \Serializer \Normalizer \ContextAwareNormalizerInterface ;
12
15
use Symfony \Component \Serializer \Normalizer \NormalizerInterface ;
@@ -21,16 +24,22 @@ class ContentTypeNormalizerTest extends TestCase {
21
24
private MockObject |NormalizerInterface $ decoratedMock ;
22
25
private MockObject |RouteNameResolverInterface $ routeNameResolver ;
23
26
private MockObject |RouterInterface $ routerMock ;
27
+ private MockObject |IriConverterInterface $ iriConverter ;
28
+ private MockObject |UriTemplate $ uriTemplate ;
29
+ private MockObject |UriTemplateFactory $ uriTemplateFactory ;
24
30
25
31
protected function setUp (): void {
26
32
$ this ->decoratedMock = $ this ->createMock (ContextAwareNormalizerInterface::class);
27
- $ this ->routeNameResolver = $ this ->createMock (RouteNameResolverInterface::class);
28
- $ this ->routerMock = $ this ->createMock (RouterInterface::class);
33
+
34
+ $ this ->iriConverter = $ this ->createMock (IriConverterInterface::class);
35
+ $ this ->uriTemplate = $ this ->createMock (UriTemplate::class);
36
+ $ this ->uriTemplateFactory = $ this ->createMock (UriTemplateFactory::class);
29
37
30
38
$ this ->normalizer = new ContentTypeNormalizer (
31
39
$ this ->decoratedMock ,
32
- $ this ->routeNameResolver ,
33
- $ this ->routerMock ,
40
+ $ this ->uriTemplate ,
41
+ $ this ->uriTemplateFactory ,
42
+ $ this ->iriConverter ,
34
43
);
35
44
$ this ->normalizer ->setSerializer ($ this ->createMock (SerializerInterface::class));
36
45
}
@@ -58,39 +67,45 @@ public function testDelegatesNormalizeToDecorated() {
58
67
;
59
68
60
69
// when
61
- $ result = $ this ->normalizer ->normalize ($ resource , null , ['resource_class ' => DummyEntity ::class]);
70
+ $ result = $ this ->normalizer ->normalize ($ resource , null , ['resource_class ' => \stdClass ::class]);
62
71
63
72
// then
64
73
$ this ->assertEquals ($ delegatedResult , $ result );
65
74
}
66
75
67
76
public function testNormalizeAddsEntityPath () {
68
77
// given
69
- $ resource = new ContentType ();
78
+ $ contentType = new ContentType ();
79
+ $ contentType ->entityClass = 'App\Entity\ContentNode\DummyContentNode ' ;
80
+
70
81
$ delegatedResult = [
71
82
'hello ' => 'world ' ,
72
- 'entityClass ' => 'DummyClass ' ,
73
83
];
74
84
$ this ->decoratedMock ->expects ($ this ->once ())
75
85
->method ('normalize ' )
76
86
->willReturn ($ delegatedResult )
77
87
;
78
- $ this ->routerMock ->expects ($ this ->once ())
79
- ->method ('generate ' )
80
- ->willReturn (' /path ' )
88
+ $ this ->uriTemplateFactory ->expects ($ this ->once ())
89
+ ->method ('createFromResourceClass ' )
90
+ ->willReturn ([ ' /templatedUri ' , ' true ' ] )
81
91
;
82
- $ this ->routeNameResolver ->expects ($ this ->once ())
83
- ->method ('getRouteName ' )
92
+
93
+ $ this ->uriTemplate ->expects ($ this ->once ())
94
+ ->method ('expand ' )
95
+ ->willReturn ('/expandedUri ' )
84
96
;
85
97
86
98
// when
87
- $ result = $ this ->normalizer ->normalize ($ resource , null , ['resource_class ' => DummyEntity ::class]);
99
+ $ result = $ this ->normalizer ->normalize ($ contentType , null , ['resource_class ' => ContentType ::class]);
88
100
89
101
// then
90
102
$ expectedResult = [
91
103
'hello ' => 'world ' ,
92
- 'entityClass ' => 'DummyClass ' ,
93
- 'entityPath ' => '/path ' ,
104
+ '_links ' => [
105
+ 'contentNodes ' => [
106
+ 'href ' => '/expandedUri ' ,
107
+ ],
108
+ ],
94
109
];
95
110
$ this ->assertEquals ($ expectedResult , $ result );
96
111
}
0 commit comments