Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/316-resource-event-pre-fetch' into develop
Browse files Browse the repository at this point in the history
Forward port #316
Fixes #315

Conflicts:
	CHANGELOG.md
  • Loading branch information
weierophinney committed Dec 3, 2018
2 parents 3a03432 + 420ce0e commit 1413db8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ All notable changes to this project will be documented in this file, in reverse

- Nothing.

## 2.2.1 - TBD
## 2.2.1 - 2018-12-03

### Added

Expand All @@ -44,7 +44,9 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#316](https://github.com/zfcampus/zf-apigility-doctrine/pull/316) updates the `DoctrineResource::fetch()` method to add the `ZF\Apigility\ResourceEvent`
to the generated `DoctrineResourceEvent` prior to triggering the
`EVENT_FETCH_PRE` event, ensuring users have access to all necessary data.

## 2.2.0 - 2018-01-18

Expand Down
1 change: 1 addition & 0 deletions src/Server/Resource/DoctrineResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ public function fetch($id)
{
$event = new DoctrineResourceEvent(DoctrineResourceEvent::EVENT_FETCH_PRE, $this);
$event->setEntityClassName($this->getEntityClass());
$event->setResourceEvent($this->getEvent());
$event->setEntityId($id);
$eventManager = $this->getEventManager();
$response = $eventManager->triggerEvent($event);
Expand Down
29 changes: 29 additions & 0 deletions test/src/Server/ORM/CRUD/CRUDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Tools\SchemaTool;
use PHPUnit\Framework\Assert;
use Zend\Filter\FilterChain;
use Zend\Http\Request;
use Zend\ServiceManager\ServiceManager;
Expand All @@ -21,6 +22,7 @@
use ZF\Apigility\Doctrine\Server\Event\DoctrineResourceEvent;
use ZF\ApiProblem\ApiProblem;
use ZF\ApiProblem\ApiProblemResponse;
use ZF\Rest\ResourceEvent;
use ZFTest\Apigility\Doctrine\TestCase;
use ZFTestApigilityDb\Entity\Album;
use ZFTestApigilityDb\Entity\Artist;
Expand Down Expand Up @@ -261,6 +263,33 @@ public function testFetchByCustomIdField()
]);
}

/**
* @see https://github.com/zfcampus/zf-apigility-doctrine/pull/316
*/
public function testFetchByCustomIdFieldIncludesApigilityResourceEventInDoctrineResourceEvent()
{
$product = $this->createProduct();

$this->getRequest()->getHeaders()->addHeaderLine('Accept', 'application/json');
$this->getRequest()->setMethod(Request::METHOD_GET);

$spy = (object) ['caught' => false];
$sharedEvents = $this->getApplication()->getEventManager()->getSharedManager();
$sharedEvents->attach(
DoctrineResource::class,
DoctrineResourceEvent::EVENT_FETCH_PRE,
function (DoctrineResourceEvent $e) use ($spy) {
Assert::assertInstanceOf(ResourceEvent::class, $e->getResourceEvent());
$spy->caught = true;
}
);

$this->dispatch('/test/rest/product/' . $product->getId());

$this->assertResponseStatusCode(200);
$this->assertTrue($spy->caught, 'EVENT_FETCH_PRE listener was not triggered');
}

public function testFetchEntityWithVersionFieldWithVersionParamInPath()
{
$product = $this->createProduct();
Expand Down

0 comments on commit 1413db8

Please sign in to comment.