22
33namespace App \Tests \Serializer \Normalizer ;
44
5+ use ApiPlatform \Core \Api \IriConverterInterface ;
56use ApiPlatform \Core \Bridge \Symfony \Routing \RouteNameResolverInterface ;
67use App \Entity \ContentType ;
8+ use App \Metadata \Resource \Factory \UriTemplateFactory ;
79use App \Serializer \Normalizer \ContentTypeNormalizer ;
810use PHPUnit \Framework \MockObject \MockObject ;
911use PHPUnit \Framework \TestCase ;
12+ use Rize \UriTemplate ;
1013use Symfony \Component \Routing \RouterInterface ;
1114use Symfony \Component \Serializer \Normalizer \ContextAwareNormalizerInterface ;
1215use Symfony \Component \Serializer \Normalizer \NormalizerInterface ;
@@ -21,16 +24,22 @@ class ContentTypeNormalizerTest extends TestCase {
2124 private MockObject |NormalizerInterface $ decoratedMock ;
2225 private MockObject |RouteNameResolverInterface $ routeNameResolver ;
2326 private MockObject |RouterInterface $ routerMock ;
27+ private MockObject |IriConverterInterface $ iriConverter ;
28+ private MockObject |UriTemplate $ uriTemplate ;
29+ private MockObject |UriTemplateFactory $ uriTemplateFactory ;
2430
2531 protected function setUp (): void {
2632 $ 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);
2937
3038 $ this ->normalizer = new ContentTypeNormalizer (
3139 $ this ->decoratedMock ,
32- $ this ->routeNameResolver ,
33- $ this ->routerMock ,
40+ $ this ->uriTemplate ,
41+ $ this ->uriTemplateFactory ,
42+ $ this ->iriConverter ,
3443 );
3544 $ this ->normalizer ->setSerializer ($ this ->createMock (SerializerInterface::class));
3645 }
@@ -58,39 +67,45 @@ public function testDelegatesNormalizeToDecorated() {
5867 ;
5968
6069 // when
61- $ result = $ this ->normalizer ->normalize ($ resource , null , ['resource_class ' => DummyEntity ::class]);
70+ $ result = $ this ->normalizer ->normalize ($ resource , null , ['resource_class ' => \stdClass ::class]);
6271
6372 // then
6473 $ this ->assertEquals ($ delegatedResult , $ result );
6574 }
6675
6776 public function testNormalizeAddsEntityPath () {
6877 // given
69- $ resource = new ContentType ();
78+ $ contentType = new ContentType ();
79+ $ contentType ->entityClass = 'App\Entity\ContentNode\DummyContentNode ' ;
80+
7081 $ delegatedResult = [
7182 'hello ' => 'world ' ,
72- 'entityClass ' => 'DummyClass ' ,
7383 ];
7484 $ this ->decoratedMock ->expects ($ this ->once ())
7585 ->method ('normalize ' )
7686 ->willReturn ($ delegatedResult )
7787 ;
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 ' ] )
8191 ;
82- $ this ->routeNameResolver ->expects ($ this ->once ())
83- ->method ('getRouteName ' )
92+
93+ $ this ->uriTemplate ->expects ($ this ->once ())
94+ ->method ('expand ' )
95+ ->willReturn ('/expandedUri ' )
8496 ;
8597
8698 // when
87- $ result = $ this ->normalizer ->normalize ($ resource , null , ['resource_class ' => DummyEntity ::class]);
99+ $ result = $ this ->normalizer ->normalize ($ contentType , null , ['resource_class ' => ContentType ::class]);
88100
89101 // then
90102 $ expectedResult = [
91103 'hello ' => 'world ' ,
92- 'entityClass ' => 'DummyClass ' ,
93- 'entityPath ' => '/path ' ,
104+ '_links ' => [
105+ 'contentNodes ' => [
106+ 'href ' => '/expandedUri ' ,
107+ ],
108+ ],
94109 ];
95110 $ this ->assertEquals ($ expectedResult , $ result );
96111 }
0 commit comments