From fd4cd8f45fd5f1d39bf86e64f1b57d94deff35ab Mon Sep 17 00:00:00 2001 From: Serhii Polishchuk Date: Mon, 24 Feb 2020 10:59:59 +0700 Subject: [PATCH 1/3] Cached performance item --- src/Controller/Api/PerformancesController.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Controller/Api/PerformancesController.php b/src/Controller/Api/PerformancesController.php index 09590ebc..4eafc309 100644 --- a/src/Controller/Api/PerformancesController.php +++ b/src/Controller/Api/PerformancesController.php @@ -2,12 +2,14 @@ namespace App\Controller\Api; +use App\Entity\Performance; use App\Model\Link; use App\Model\PaginationLinks; use App\Model\PerformancesResponse; use FOS\RestBundle\Controller\Annotations\QueryParam; use FOS\RestBundle\Request\ParamFetcher; use Nelmio\ApiDocBundle\Annotation\Model; +use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; use Swagger\Annotations as SWG; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Routing\Annotation\Route; @@ -129,6 +131,10 @@ public function getList(ParamFetcher $paramFetcher) /** * @Route("/{slug}", name="get_performance", methods={"GET"}) + * @Cache( + * lastModified="performance.getUpdatedAt()", + * Etag="'Post' ~ performance.getId() ~ performance.getUpdatedAt().getTimestamp()" + * ) * @SWG\Response( * response=200, * description="Returns Performance by unique property {slug}", @@ -141,17 +147,10 @@ public function getList(ParamFetcher $paramFetcher) * * @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive") */ - public function getAction(ParamFetcher $paramFetcher, $slug) + public function getAction(ParamFetcher $paramFetcher, Performance $performance) { $em = $this->getDoctrine()->getManager(); - $performance = $em - ->getRepository('App:Performance')->findOneByslug($slug); - - if (!$performance) { - throw $this->createNotFoundException('Unable to find '.$slug.' entity'); - } - $performance->setLocale($paramFetcher->get('locale')); $em->refresh($performance); From eef3039c82075ed8531510cb9cf9576a0114ead8 Mon Sep 17 00:00:00 2001 From: Serhii Polishchuk Date: Mon, 24 Feb 2020 11:24:44 +0700 Subject: [PATCH 2/3] Added tests for cache --- tests/Functional/Controller/AbstractController.php | 4 ++-- tests/Functional/Controller/PerformancesControllerTest.php | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/Functional/Controller/AbstractController.php b/tests/Functional/Controller/AbstractController.php index 79cd6f1d..31bfab64 100644 --- a/tests/Functional/Controller/AbstractController.php +++ b/tests/Functional/Controller/AbstractController.php @@ -65,9 +65,9 @@ protected function request($path, $method = 'GET', $expectedStatusCode = 200, ar return $crawler; } - protected function restRequest(string $path, string $method = 'GET', int $expectedStatusCode = 200) + protected function restRequest(string $path, string $method = 'GET', int $expectedStatusCode = 200, array $headers = []) { - return $this->request($path, $method, $expectedStatusCode, ['HTTP_accept' => 'application/json']); + return $this->request($path, $method, $expectedStatusCode, array_merge(['HTTP_accept' => 'application/json'], $headers)); } /** diff --git a/tests/Functional/Controller/PerformancesControllerTest.php b/tests/Functional/Controller/PerformancesControllerTest.php index a0496585..a8038a83 100644 --- a/tests/Functional/Controller/PerformancesControllerTest.php +++ b/tests/Functional/Controller/PerformancesControllerTest.php @@ -25,6 +25,11 @@ public function testGetPerformancesSlug() { $slug = $this->getEm()->getRepository('App:Performance')->findOneBy([])->getSlug(); $this->restRequest('/api/performances/'.$slug); + + $eTag = $this->getSessionClient()->getResponse()->headers->get('Etag'); + $this->assertNotNull($eTag); + $this->restRequest('/api/performances/'.$slug, 'GET', 304, ['HTTP_if_none_match' => $eTag]); + $this->restRequest('/api/performances/nonexistent-slug', 'GET', 404); } From 05faf56e6a60aae26d024fd780c2326121b697d9 Mon Sep 17 00:00:00 2001 From: Serhii Polishchuk Date: Tue, 25 Feb 2020 10:16:16 +0700 Subject: [PATCH 3/3] Added test for expired etag --- tests/Functional/Controller/AbstractController.php | 2 +- .../Controller/PerformancesControllerTest.php | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/Functional/Controller/AbstractController.php b/tests/Functional/Controller/AbstractController.php index 31bfab64..6a8bd423 100644 --- a/tests/Functional/Controller/AbstractController.php +++ b/tests/Functional/Controller/AbstractController.php @@ -31,7 +31,7 @@ protected function setUp(): void public function getEm() { if (!$this->em) { - $this->em = $this->getContainer()->get('doctrine')->getManager(); + $this->em = $this->getContainer()->get('doctrine.orm.entity_manager'); } return $this->em; diff --git a/tests/Functional/Controller/PerformancesControllerTest.php b/tests/Functional/Controller/PerformancesControllerTest.php index a8038a83..d9425405 100644 --- a/tests/Functional/Controller/PerformancesControllerTest.php +++ b/tests/Functional/Controller/PerformancesControllerTest.php @@ -23,13 +23,21 @@ public function testGetPerformances() public function testGetPerformancesSlug() { - $slug = $this->getEm()->getRepository('App:Performance')->findOneBy([])->getSlug(); + /** @var Performance $performance */ + $performance = $this->getEm()->getRepository(Performance::class)->findOneBy([]); + $slug = $performance->getSlug(); $this->restRequest('/api/performances/'.$slug); $eTag = $this->getSessionClient()->getResponse()->headers->get('Etag'); $this->assertNotNull($eTag); $this->restRequest('/api/performances/'.$slug, 'GET', 304, ['HTTP_if_none_match' => $eTag]); + /** @var Performance $performance */ + $performance = $this->getEm()->find(Performance::class, $performance->getId()); + $performance->setUpdatedAt(new \DateTime()); + $this->getEm()->flush($performance); + $this->restRequest('/api/performances/'.$slug, 'GET', 200, ['HTTP_if_none_match' => $eTag]); + $this->restRequest('/api/performances/nonexistent-slug', 'GET', 404); }