Skip to content

Commit

Permalink
Merge pull request #2693 from Tjeerd/persist-result-fix
Browse files Browse the repository at this point in the history
Make sure to always use the persistResult for follow-up actions
  • Loading branch information
teohhanhui committed Apr 9, 2019
2 parents bb04d57 + 431cf6f commit 39900b9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/EventListener/WriteListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ public function onKernelView(GetResponseForControllerResultEvent $event): void

if (null === $persistResult) {
@trigger_error(sprintf('Returning void from %s::persist() is deprecated since API Platform 2.3 and will not be supported in API Platform 3, an object should always be returned.', DataPersisterInterface::class), E_USER_DEPRECATED);
} else {
$controllerResult = $persistResult;
$event->setControllerResult($controllerResult);
}

$event->setControllerResult($persistResult ?? $controllerResult);

if (null === $this->iriConverter) {
return;
}
Expand Down
16 changes: 8 additions & 8 deletions tests/EventListener/WriteListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,25 @@ public function testOnKernelViewWithControllerResultAndPersistReturningVoid()

/**
* @see https://github.com/api-platform/core/issues/1799
* @see https://github.com/api-platform/core/issues/2692
*/
public function testOnKernelViewWithControllerResultAndPersistWithImmutableResource()
{
$dummy = new Dummy();
$dummy->setName('Dummyrino');

$dummy2 = new Dummy();
$dummy2->setId(2);
$dummy2->setName('Dummyferoce');

$dataPersisterProphecy = $this->prophesize(DataPersisterInterface::class);
$dataPersisterProphecy->supports($dummy, Argument::type('array'))->willReturn(true)->shouldBeCalled();
$dataPersisterProphecy->supports($dummy, Argument::type('array'))->willReturn(true);
$dataPersisterProphecy->persist($dummy, Argument::type('array'))->willReturn($dummy2);

$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getIriFromItem($dummy)->willReturn('/dummy/1')->shouldBeCalled();
$iriConverterProphecy->getIriFromItem($dummy2)->willReturn('/dummy/2');

$dataPersisterProphecy
->persist($dummy, Argument::type('array'))
->willReturn($dummy2) // Persist is not mutating $dummy, but return a brand new technically unrelated object instead
->shouldBeCalled();
$writeListener = new WriteListener($dataPersisterProphecy->reveal(), $iriConverterProphecy->reveal());

$request = new Request([], [], ['_api_resource_class' => Dummy::class]);

Expand All @@ -128,10 +128,10 @@ public function testOnKernelViewWithControllerResultAndPersistWithImmutableResou
$request->setMethod($httpMethod);
$request->attributes->set(sprintf('_api_%s_operation_name', 'POST' === $httpMethod ? 'collection' : 'item'), strtolower($httpMethod));

(new WriteListener($dataPersisterProphecy->reveal(), $iriConverterProphecy->reveal()))->onKernelView($event);
$writeListener->onKernelView($event);

$this->assertSame($dummy2, $event->getControllerResult());
$this->assertEquals('/dummy/1', $request->attributes->get('_api_write_item_iri'));
$this->assertEquals('/dummy/2', $request->attributes->get('_api_write_item_iri'));
}
}

Expand Down

0 comments on commit 39900b9

Please sign in to comment.