From 20b5070243b876d7a10515ed034ceb83ea06c972 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Mon, 3 Dec 2018 10:51:34 -0600 Subject: [PATCH] Provides a test verifying zf-rest ResourceEvent is present in DoctrineResourceEvent during pre-fetch operations --- test/src/Server/ORM/CRUD/CRUDTest.php | 29 +++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/src/Server/ORM/CRUD/CRUDTest.php b/test/src/Server/ORM/CRUD/CRUDTest.php index 3c7bb25a..a0a65b71 100644 --- a/test/src/Server/ORM/CRUD/CRUDTest.php +++ b/test/src/Server/ORM/CRUD/CRUDTest.php @@ -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; @@ -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; @@ -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();