Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http Cache #169

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/Controller/Api/PerformancesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}",
Expand All @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions tests/Functional/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

/**
Expand Down
15 changes: 14 additions & 1 deletion tests/Functional/Controller/PerformancesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +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);
}

Expand Down