Skip to content

Commit

Permalink
Fix issue with single entity when ResultSetDecorators are used
Browse files Browse the repository at this point in the history
  • Loading branch information
dakota committed Apr 23, 2020
1 parent f4edb8a commit 0e7b21c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Listener/JsonApiListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ protected function _getSingleEntity(Subject $subject): ?EntityInterface
return (clone $subject->entities)->first();
}

if (!empty($subject->entities) && $subject->entities instanceof ResultSet) {
if (!empty($subject->entities) && $subject->entities instanceof ResultSetInterface) {
if ($subject->entities->first() === null) {
$repository = $subject->query->getRepository();
$entity = $repository->getEntityClass();
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/Listener/JsonApiListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Cake\Controller\Controller;
use Cake\Core\Plugin;
use Cake\Datasource\ResultSetDecorator;
use Cake\Event\Event;
use Cake\Filesystem\File;
use Cake\Http\Exception\BadRequestException;
Expand Down Expand Up @@ -848,6 +849,18 @@ public function testGetSingleEntity()
$result = $this->callProtectedMethod('_getSingleEntity', [$subject], $listener);
$this->assertSame($entity, $result);

$subject->entities = $this->getMockBuilder(ResultSetDecorator::class)
->disableOriginalConstructor()
->onlyMethods(['first'])
->getMock();

$subject->entities->method('first')
->willReturn($entity);

$this->setReflectionClassInstance($listener);
$result = $this->callProtectedMethod('_getSingleEntity', [$subject], $listener);
$this->assertSame($entity, $result);

unset($subject->entities);

$subject->entity = $entity;
Expand Down

0 comments on commit 0e7b21c

Please sign in to comment.