Skip to content

Commit f6edf5c

Browse files
committed
Fix tests
1 parent e3dbb00 commit f6edf5c

File tree

10 files changed

+40
-51
lines changed

10 files changed

+40
-51
lines changed

src/Bridge/Doctrine/EventListener/PurgeHttpCacheListener.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class PurgeHttpCacheListener
2828
private $container;
2929

3030
/**
31-
* @param ContainerInterface|\Symfony\Component\DependencyInjection\ContainerInterface $container The container is injected because of a circular reference.
31+
* @param ContainerInterface $container The container is injected because of a circular reference.
3232
*/
3333
public function __construct($container)
3434
{
@@ -41,7 +41,7 @@ public function onFlush(OnFlushEventArgs $eventArgs)
4141
return;
4242
}
4343

44-
$resources = $request->attributes->get('resources', []);
44+
$resources = $request->attributes->get('_resources', []);
4545
$iriConverter = $this->container->get('api_platform.iri_converter');
4646
$uow = $eventArgs->getEntityManager()->getUnitOfWork();
4747

@@ -57,7 +57,7 @@ public function onFlush(OnFlushEventArgs $eventArgs)
5757
$resources = $this->purge($iriConverter, $resources, $entity, true);
5858
}
5959

60-
$request->attributes->set('resources', $resources);
60+
$request->attributes->set('_resources', $resources);
6161
}
6262

6363
private function purge(IriConverterInterface $iriConverter, array $resources, $entity, bool $purgeItem): array

src/EventListener/SerializeListener.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,13 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
5959
}
6060

6161
$context = $this->serializerContextBuilder->createFromRequest($request, true, $attributes);
62-
$request->attributes->set('_api_respond', true);
62+
$resources = [];
63+
$context['resources'] = &$resources;
6364

6465
$event->setControllerResult($this->serializer->serialize($controllerResult, $request->getRequestFormat(), $context));
66+
67+
$request->attributes->set('_api_respond', true);
68+
$request->attributes->set('_resources', $request->attributes->get('_resources', []) + $resources);
6569
}
6670

6771
/**

src/HttpCache/EventListener/AddTagsListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function onKernelResponse(FilterResponseEvent $event)
4646
if (
4747
!$request->isMethodCacheable()
4848
|| (!$attributes = RequestAttributesExtractor::extractAttributes($request))
49-
|| !$resources = $request->attributes->get('resources')
49+
|| !$resources = $request->attributes->get('_resources')
5050
) {
5151
return;
5252
}

src/HttpCache/EventListener/PurgeListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(PurgerInterface $purger)
3030

3131
public function onKernelTerminate(PostResponseEvent $event)
3232
{
33-
if ($resources = $event->getRequest()->attributes->get('resources')) {
33+
if ($resources = $event->getRequest()->attributes->get('_resources')) {
3434
$this->purger->purge($resources);
3535
}
3636
}

src/Serializer/AbstractItemNormalizer.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ public function normalize($object, $format = null, array $context = [])
8787
$context = $this->initContext($resourceClass, $context);
8888
$context['api_normalize'] = true;
8989

90-
$this->updateResources($context['iri'] ?? $this->iriConverter->getIriFromItem($object), $context);
90+
if (isset($context['resources'])) {
91+
$resource = $context['iri'] ?? $this->iriConverter->getIriFromItem($object);
92+
$context['resources'][$resource] = $resource;
93+
}
9194

9295
return parent::normalize($object, $format, $context);
9396
}
@@ -435,25 +438,10 @@ private function normalizeRelation(PropertyMetadata $propertyMetadata, $relatedO
435438
}
436439

437440
$iri = $this->iriConverter->getIriFromItem($relatedObject);
438-
$this->updateResources($iri, $context);
439-
440-
return $iri;
441-
}
442-
443-
/**
444-
* Adds a new IRI to the list of resources included in the current HTTP response.
445-
*
446-
* @param string $iri
447-
* @param array $context
448-
*/
449-
private function updateResources(string $iri, array $context)
450-
{
451-
if (!isset($context['request']) || !$context['request'] instanceof Request) {
452-
return;
441+
if (isset($context['resources'])) {
442+
$context['resources'][$iri] = $iri;
453443
}
454444

455-
$resources = $context['request']->attributes->get('resources', []);
456-
$resources[$iri] = $iri;
457-
$context['request']->attributes->set('resources', $resources);
445+
return $iri;
458446
}
459447
}

src/Serializer/SerializerContextBuilder.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ public function createFromRequest(Request $request, bool $normalization, array $
7272
}
7373

7474
$context['resource_class'] = $attributes['resource_class'];
75-
$context['request'] = $request;
7675
$context['request_uri'] = $request->getRequestUri();
7776

7877
if (isset($attributes['subresource_context'])) {

tests/EventListener/SerializeListenerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function testDoNotSerializeWhenOperationNotSet()
102102

103103
public function testSerializeCollectionOperation()
104104
{
105-
$expectedContext = ['request_uri' => '', 'resource_class' => 'Foo', 'collection_operation_name' => 'get'];
105+
$expectedContext = ['request_uri' => '', 'resource_class' => 'Foo', 'collection_operation_name' => 'get', 'resources' => []];
106106
$serializerProphecy = $this->prophesize(SerializerInterface::class);
107107
$serializerProphecy->serialize(Argument::any(), 'xml', $expectedContext)->willReturn('bar')->shouldBeCalled();
108108

@@ -123,7 +123,7 @@ public function testSerializeCollectionOperation()
123123

124124
public function testSerializeItemOperation()
125125
{
126-
$expectedContext = ['request_uri' => '', 'resource_class' => 'Foo', 'item_operation_name' => 'get'];
126+
$expectedContext = ['request_uri' => '', 'resource_class' => 'Foo', 'item_operation_name' => 'get', 'resources' => []];
127127
$serializerProphecy = $this->prophesize(SerializerInterface::class);
128128
$serializerProphecy->serialize(Argument::any(), 'xml', $expectedContext)->willReturn('bar')->shouldBeCalled();
129129

tests/Serializer/AbstractItemNormalizerTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ public function testNormalize()
111111
$iriConverterProphecy->getIriFromItem($dummy)->willReturn('/dummies/1')->shouldBeCalled();
112112
$iriConverterProphecy->getIriFromItem($relatedDummy)->willReturn('/dummies/2')->shouldBeCalled();
113113

114-
$propertyAccesorProphecy = $this->prophesize(PropertyAccessorInterface::class);
115-
$propertyAccesorProphecy->getValue($dummy, 'name')->willReturn('foo')->shouldBeCalled();
116-
$propertyAccesorProphecy->getValue($dummy, 'relatedDummy')->willReturn($relatedDummy)->shouldBeCalled();
117-
$propertyAccesorProphecy->getValue($dummy, 'relatedDummies')->willReturn(
114+
$propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
115+
$propertyAccessorProphecy->getValue($dummy, 'name')->willReturn('foo')->shouldBeCalled();
116+
$propertyAccessorProphecy->getValue($dummy, 'relatedDummy')->willReturn($relatedDummy)->shouldBeCalled();
117+
$propertyAccessorProphecy->getValue($dummy, 'relatedDummies')->willReturn(
118118
new ArrayCollection([$relatedDummy])
119119
)->shouldBeCalled();
120120

@@ -132,15 +132,15 @@ public function testNormalize()
132132
$propertyMetadataFactoryProphecy->reveal(),
133133
$iriConverterProphecy->reveal(),
134134
$resourceClassResolverProphecy->reveal(),
135-
$propertyAccesorProphecy->reveal(),
135+
$propertyAccessorProphecy->reveal(),
136136
]);
137137
$normalizer->setSerializer($serializerProphecy->reveal());
138138

139139
$this->assertEquals([
140140
'name' => 'foo',
141141
'relatedDummy' => '/dummies/2',
142142
'relatedDummies' => ['/dummies/2'],
143-
], $normalizer->normalize($dummy));
143+
], $normalizer->normalize($dummy, null, ['resources' => []]));
144144
}
145145

146146
public function testNormalizeReadableLinks()
@@ -206,7 +206,7 @@ public function testNormalizeReadableLinks()
206206
$this->assertEquals([
207207
'relatedDummy' => ['foo' => 'hello'],
208208
'relatedDummies' => [['foo' => 'hello']],
209-
], $normalizer->normalize($dummy));
209+
], $normalizer->normalize($dummy, null, ['resources' => []]));
210210
}
211211

212212
public function testDenormalize()
@@ -613,9 +613,9 @@ public function testChildInheritedProperty()
613613
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
614614
$iriConverterProphecy->getIriFromItem($dummy)->willReturn('/dummies/1')->shouldBeCalled();
615615

616-
$propertyAccesorProphecy = $this->prophesize(PropertyAccessorInterface::class);
617-
$propertyAccesorProphecy->getValue($dummy, 'name')->willReturn('foo')->shouldBeCalled();
618-
$propertyAccesorProphecy->getValue($dummy, 'nickname')->willThrow(new NoSuchPropertyException())->shouldBeCalled();
616+
$propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
617+
$propertyAccessorProphecy->getValue($dummy, 'name')->willReturn('foo')->shouldBeCalled();
618+
$propertyAccessorProphecy->getValue($dummy, 'nickname')->willThrow(new NoSuchPropertyException())->shouldBeCalled();
619619

620620
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
621621
$resourceClassResolverProphecy->getResourceClass($dummy, DummyTableInheritance::class, true)->willReturn(DummyTableInheritance::class)->shouldBeCalled();
@@ -630,13 +630,13 @@ public function testChildInheritedProperty()
630630
$propertyMetadataFactoryProphecy->reveal(),
631631
$iriConverterProphecy->reveal(),
632632
$resourceClassResolverProphecy->reveal(),
633-
$propertyAccesorProphecy->reveal(),
633+
$propertyAccessorProphecy->reveal(),
634634
]);
635635
$normalizer->setSerializer($serializerProphecy->reveal());
636636

637637
$this->assertEquals([
638638
'name' => 'foo',
639639
'nickname' => null,
640-
], $normalizer->normalize($dummy, null, ['resource_class' => DummyTableInheritance::class]));
640+
], $normalizer->normalize($dummy, null, ['resource_class' => DummyTableInheritance::class, 'resources' => []]));
641641
}
642642
}

tests/Serializer/ItemNormalizerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function testNormalize()
9595
);
9696
$normalizer->setSerializer($serializerProphecy->reveal());
9797

98-
$this->assertEquals(['name' => 'hello'], $normalizer->normalize($dummy));
98+
$this->assertEquals(['name' => 'hello'], $normalizer->normalize($dummy, null, ['resources' => []]));
9999
}
100100

101101
public function testDenormalize()

tests/Serializer/SerializerContextBuilderTest.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,27 @@ protected function setUp()
4848
public function testCreateFromRequest()
4949
{
5050
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_item_operation_name' => 'get', '_api_format' => 'xml', '_api_mime_type' => 'text/xml']);
51-
$expected = ['foo' => 'bar', 'item_operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '', 'operation_type' => 'item', 'request' => $request];
51+
$expected = ['foo' => 'bar', 'item_operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '', 'operation_type' => 'item'];
5252
$this->assertEquals($expected, $this->builder->createFromRequest($request, true));
5353

5454
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'pot', '_api_format' => 'xml', '_api_mime_type' => 'text/xml']);
55-
$expected = ['foo' => 'bar', 'collection_operation_name' => 'pot', 'resource_class' => 'Foo', 'request_uri' => '', 'operation_type' => 'collection', 'request' => $request];
55+
$expected = ['foo' => 'bar', 'collection_operation_name' => 'pot', 'resource_class' => 'Foo', 'request_uri' => '', 'operation_type' => 'collection'];
5656
$this->assertEquals($expected, $this->builder->createFromRequest($request, true));
5757

5858
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_item_operation_name' => 'get', '_api_format' => 'xml', '_api_mime_type' => 'text/xml']);
59-
$expected = ['bar' => 'baz', 'item_operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '', 'api_allow_update' => false, 'operation_type' => 'item', 'request' => $request];
59+
$expected = ['bar' => 'baz', 'item_operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '', 'api_allow_update' => false, 'operation_type' => 'item'];
6060
$this->assertEquals($expected, $this->builder->createFromRequest($request, false));
6161

6262
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'post', '_api_format' => 'xml', '_api_mime_type' => 'text/xml'], [], [], ['REQUEST_METHOD' => 'POST']);
63-
$expected = ['bar' => 'baz', 'collection_operation_name' => 'post', 'resource_class' => 'Foo', 'request_uri' => '', 'api_allow_update' => false, 'operation_type' => 'collection', 'request' => $request];
63+
$expected = ['bar' => 'baz', 'collection_operation_name' => 'post', 'resource_class' => 'Foo', 'request_uri' => '', 'api_allow_update' => false, 'operation_type' => 'collection'];
6464
$this->assertEquals($expected, $this->builder->createFromRequest($request, false));
6565

6666
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'put', '_api_format' => 'xml', '_api_mime_type' => 'text/xml'], [], [], ['REQUEST_METHOD' => 'PUT']);
67-
$expected = ['bar' => 'baz', 'collection_operation_name' => 'put', 'resource_class' => 'Foo', 'request_uri' => '', 'api_allow_update' => true, 'operation_type' => 'collection', 'request' => $request];
67+
$expected = ['bar' => 'baz', 'collection_operation_name' => 'put', 'resource_class' => 'Foo', 'request_uri' => '', 'api_allow_update' => true, 'operation_type' => 'collection'];
6868
$this->assertEquals($expected, $this->builder->createFromRequest($request, false));
6969

7070
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_subresource_operation_name' => 'get', '_api_format' => 'xml', '_api_mime_type' => 'text/xml'], [], [], ['REQUEST_METHOD' => 'GET']);
71-
$expected = ['bar' => 'baz', 'subresource_operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '', 'operation_type' => 'subresource', 'api_allow_update' => false, 'request' => $request];
71+
$expected = ['bar' => 'baz', 'subresource_operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '', 'operation_type' => 'subresource', 'api_allow_update' => false];
7272
$this->assertEquals($expected, $this->builder->createFromRequest($request, false));
7373
}
7474

@@ -82,9 +82,7 @@ public function testThrowExceptionOnInvalidRequest()
8282

8383
public function testReuseExistingAttributes()
8484
{
85-
$request = new Request();
86-
87-
$expected = ['bar' => 'baz', 'item_operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '', 'api_allow_update' => false, 'operation_type' => 'item', 'request' => $request];
88-
$this->assertEquals($expected, $this->builder->createFromRequest($request, false, ['resource_class' => 'Foo', 'item_operation_name' => 'get']));
85+
$expected = ['bar' => 'baz', 'item_operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '', 'api_allow_update' => false, 'operation_type' => 'item'];
86+
$this->assertEquals($expected, $this->builder->createFromRequest(new Request(), false, ['resource_class' => 'Foo', 'item_operation_name' => 'get']));
8987
}
9088
}

0 commit comments

Comments
 (0)