public indexAction ( |
||
$request | ||
리턴 |
public function indexAction(Request $request)
{
/** @var \Networking\InitCmsBundle\Lib\PhpCacheInterface $phpCache */
$phpCache = $this->get('networking_init_cms.lib.php_cache');
/** @var PageSnapshotInterface $page */
$page = $request->get('_content');
$template = $request->get('_template');
if ($template instanceof \Sensio\Bundle\FrameworkExtraBundle\Configuration\Template) {
$template = $template->getTemplate();
}
if ($phpCache->isCacheable($request, $this->getUser()) && $page instanceof PageSnapshotInterface) {
if (!$this->isSnapshotActive($page)) {
throw new NotFoundHttpException();
}
if ($this->getSnapshotVisibility($page) != PageInterface::VISIBILITY_PUBLIC) {
if (false === $this->get('security.authorization_checker')->isGranted('ROLE_USER')) {
throw new AccessDeniedException();
}
}
$updatedAt = $phpCache->get(sprintf('page_%s_created_at', $page->getId()));
$cacheKey = $request->getLocale() . $request->getPathInfo();
if ($updatedAt != $page->getSnapshotDate()) {
$phpCache->delete($cacheKey);
}
$response = $phpCache->get($request->getLocale() . $request->getPathInfo());
if (!$response || !$response instanceof Response) {
$params = $this->getPageParameters($request);
if ($params instanceof RedirectResponse) {
return $params;
}
$html = $this->renderView($template, $params);
$response = new Response($html);
$phpCache->set($cacheKey, $response);
$phpCache->set(sprintf('page_%s_created_at', $page->getId()), $page->getSnapshotDate());
} else {
$phpCache->touch($cacheKey);
$phpCache->touch(sprintf('page_%s_created_at', $page->getId()));
}
} else {
$params = $this->getPageParameters($request);
if ($params instanceof RedirectResponse) {
return $params;
}
$html = $this->renderView($template, $params);
$response = new Response($html);
}
$response->headers->setCookie(new Cookie('_locale', $request->getLocale()));
return $response;
}
/** * */ public function testLiveAction() { // MOCKS $mockPage = $this->getMock('Networking\\InitCmsBundle\\Model\\Page'); $mockPage->expects($this->once())->method('getVisibility')->will($this->returnValue(PageInterface::VISIBILITY_PUBLIC)); $mockPage->expects($this->once())->method('isActive')->will($this->returnValue(true)); $mockSnapshot = $this->getMockBuilder('Networking\\InitCmsBundle\\Entity\\PageSnapshot')->disableOriginalConstructor()->getMock(); $mockHelper = $this->getMockBuilder('Networking\\InitCmsBundle\\Helper\\PageHelper')->disableOriginalConstructor()->getMock(); $mockHelper->expects($this->once())->method('unserializePageSnapshotData')->will($this->returnValue($mockPage)); //cache class $mockCacheClass = $this->getMockBuilder('Networking\\InitCmsBundle\\Lib\\PhpCache')->disableOriginalConstructor()->getMock(); //security context $mockTokenStorage = $this->getMockBuilder('Symfony\\Component\\Security\\Core\\Authentication\\Token\\Storage\\TokenStorage')->disableOriginalConstructor()->getMock(); $mockTokenStorage->expects($this->any())->method('isGranted')->with('ROLE_USER')->will($this->returnValue(true)); //templating $mockTemplating = $this->getMockBuilder('\\Symfony\\Bundle\\TwigBundle\\Debug\\TimedTwigEngine')->disableOriginalConstructor()->getMock(); $mockResponse = $this->getMockBuilder('\\Symfony\\Component\\HttpFoundation\\Response')->getMock(); $mockTemplating->expects($this->once())->method('render')->will($this->returnValue($mockResponse)); //request $mockRequest = $this->getMockBuilder('\\Symfony\\Component\\HttpFoundation\\Request')->disableOriginalConstructor()->getMock(); $mockRequest->expects($this->at(0))->method('get')->with('_content')->will($this->returnValue($mockSnapshot)); $mockRequest->expects($this->at(1))->method('get')->with('_template'); $mockRequest->expects($this->at(2))->method('get')->with('_content')->will($this->returnValue($mockSnapshot)); //Container $mockContainer = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\Container')->disableOriginalConstructor()->getMock(); $mockContainer->expects($this->at(0))->method('get')->with('networking_init_cms.lib.php_cache')->will($this->returnValue($mockCacheClass)); $mockContainer->expects($this->at(1))->method('has')->with('security.token_storage')->will($this->returnValue(true)); $mockContainer->expects($this->at(2))->method('get')->with('security.token_storage')->will($this->returnValue($mockTokenStorage)); $mockContainer->expects($this->at(3))->method('get')->with('networking_init_cms.helper.page_helper')->will($this->returnValue($mockHelper)); $mockContainer->expects($this->at(4))->method('get')->with('security.token_storage')->will($this->returnValue($mockTokenStorage)); $mockContainer->expects($this->at(5))->method('has')->with('templating')->will($this->returnValue(true)); $mockContainer->expects($this->at(6))->method('get')->with('templating')->will($this->returnValue($mockTemplating)); // controller $controller = new FrontendPageController(); $controller->setContainer($mockContainer); $response = $controller->indexAction($mockRequest); $this->assertInstanceOf('\\Symfony\\Component\\HttpFoundation\\Response', $response, 'response object returned'); }