From 62e1436cf5aca3877a71076cd91a46128c4ab829 Mon Sep 17 00:00:00 2001 From: Xavier Coureau Date: Fri, 1 Aug 2014 17:59:31 +0200 Subject: [PATCH 1/7] Added 404 header support More efficient error support --- Controller/WordpressController.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Controller/WordpressController.php b/Controller/WordpressController.php index efdca6f..8de74e1 100755 --- a/Controller/WordpressController.php +++ b/Controller/WordpressController.php @@ -10,9 +10,10 @@ namespace Ekino\WordpressBundle\Controller; -use Symfony\Bundle\FrameworkBundle\Controller\Controller; - +use Ekino\WordpressBundle\Wordpress\WordpressResponse; use Ekino\WordpressBundle\Wordpress\Wordpress; +use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException; /** * Class WordpressController @@ -27,10 +28,20 @@ class WordpressController extends Controller * Wordpress catch-all route action * * @return WordpressResponse + * + * @throws NotAcceptableHttpException */ public function catchAllAction() { - return $this->getWordpress()->initialize()->getResponse(); + $content = $this->getWordpress()->getContent(); + + global $wp_query; + + if (!$wp_query) { + throw new NotAcceptableHttpException('The "$wp_query" wordpress global variable is not defined'); + } + + return new WordpressResponse($content, $wp_query->is_404() ? WordpressResponse::HTTP_NOT_FOUND : WordpressResponse::HTTP_OK); } /** From 184052f8373ac8d84a86b7121421a026b6090f81 Mon Sep 17 00:00:00 2001 From: Xavier Coureau Date: Mon, 4 Aug 2014 10:56:01 +0200 Subject: [PATCH 2/7] Refactored 404 implementation --- Controller/WordpressController.php | 19 +- .../WordpressResponseSubscriber.php | 79 ++++++++ Resources/config/hooks.xml | 7 + .../WordpressResponseSubscriberTest.php | 170 ++++++++++++++++++ 4 files changed, 259 insertions(+), 16 deletions(-) create mode 100644 Event/Subscriber/WordpressResponseSubscriber.php create mode 100644 Tests/Event/Subscriber/WordpressResponseSubscriberTest.php diff --git a/Controller/WordpressController.php b/Controller/WordpressController.php index 8de74e1..ed7db15 100755 --- a/Controller/WordpressController.php +++ b/Controller/WordpressController.php @@ -10,10 +10,7 @@ namespace Ekino\WordpressBundle\Controller; -use Ekino\WordpressBundle\Wordpress\WordpressResponse; -use Ekino\WordpressBundle\Wordpress\Wordpress; use Symfony\Bundle\FrameworkBundle\Controller\Controller; -use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException; /** * Class WordpressController @@ -27,27 +24,17 @@ class WordpressController extends Controller /** * Wordpress catch-all route action * - * @return WordpressResponse - * - * @throws NotAcceptableHttpException + * @return \Ekino\WordpressBundle\Wordpress\WordpressResponse */ public function catchAllAction() { - $content = $this->getWordpress()->getContent(); - - global $wp_query; - - if (!$wp_query) { - throw new NotAcceptableHttpException('The "$wp_query" wordpress global variable is not defined'); - } - - return new WordpressResponse($content, $wp_query->is_404() ? WordpressResponse::HTTP_NOT_FOUND : WordpressResponse::HTTP_OK); + return $this->getWordpress()->initialize()->getResponse(); } /** * Returns Wordpress service * - * @return Wordpress + * @return \Ekino\WordpressBundle\Wordpress\Wordpress */ protected function getWordpress() { diff --git a/Event/Subscriber/WordpressResponseSubscriber.php b/Event/Subscriber/WordpressResponseSubscriber.php new file mode 100644 index 0000000..8f3694e --- /dev/null +++ b/Event/Subscriber/WordpressResponseSubscriber.php @@ -0,0 +1,79 @@ +httpHeaderCallback = $httpHeaderCallback; + } + + /** + * @param FilterResponseEvent $event + */ + public function onKernelResponse(FilterResponseEvent $event) + { + $response = $event->getResponse(); + + if (!$response instanceof WordpressResponse || $event->getRequestType() != HttpKernelInterface::MASTER_REQUEST) { + return; + } + + /** @var \WP_Query|null $wp_query */ + global $wp_query; + + if (!$wp_query) { + return; + } + + $callback = $this->getHttpHeadersCallback(); + $wpHeaders = (array) $callback($event->getRequest()->getUri()); + + foreach ($wpHeaders as $name => $value) { + // TODO add cache headers support + if ($name == 'cache-control') { + //$response->setCache($this->parseCacheHeaders($value)); + continue; + } + + $response->headers->set($name, $value); + } + + if ($wp_query->is_404()) { + $response->setStatusCode(WordpressResponse::HTTP_NOT_FOUND); + } + } + + /** + * @return string + */ + public function getHttpHeadersCallback() + { + return $this->httpHeaderCallback; + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() + { + return array( + KernelEvents::RESPONSE => array('onKernelResponse'), + ); + } +} \ No newline at end of file diff --git a/Resources/config/hooks.xml b/Resources/config/hooks.xml index 25f63e1..86e906c 100644 --- a/Resources/config/hooks.xml +++ b/Resources/config/hooks.xml @@ -4,6 +4,9 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + + Ekino\WordpressBundle\Event\Subscriber\WordpressResponseSubscriber + @@ -16,5 +19,9 @@ + + + + diff --git a/Tests/Event/Subscriber/WordpressResponseSubscriberTest.php b/Tests/Event/Subscriber/WordpressResponseSubscriberTest.php new file mode 100644 index 0000000..775f7a2 --- /dev/null +++ b/Tests/Event/Subscriber/WordpressResponseSubscriberTest.php @@ -0,0 +1,170 @@ +event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\FilterResponseEvent')->disableOriginalConstructor()->getMock(); + $this->response = $this->getMockBuilder('Ekino\WordpressBundle\Wordpress\WordpressResponse')->disableOriginalConstructor()->getMock(); + $this->request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->disableOriginalConstructor()->getMock(); + + $this->subscriber = new WordpressResponseSubscriber(); + } + + public function testGetSubscribedEvents() + { + $expected = array( + KernelEvents::RESPONSE => array('onKernelResponse'), + ); + + $this->assertEquals($expected, WordpressResponseSubscriber::getSubscribedEvents()); + } + + public function testGetHttpHeadersCallback() + { + $this->assertEquals('wp_get_http_headers', $this->subscriber->getHttpHeadersCallback()); + } + + public function testOnKernelResponseNoWordpressResponse() + { + $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\FilterResponseEvent')->disableOriginalConstructor()->getMock(); + $response = $this->getMockBuilder('Symfony\Component\HttpFoundation\Response')->disableOriginalConstructor()->getMock(); + + $event->expects($this->once()) + ->method('getResponse') + ->will($this->returnValue($response)); + + $response->expects($this->never()) + ->method('getUri'); + + $this->subscriber->onKernelResponse($event); + } + + public function testOnKernelResponseNoMasterRequest() + { + $this->event->expects($this->once()) + ->method('getResponse') + ->will($this->returnValue($this->response)); + $this->event->expects($this->once()) + ->method('getRequestType') + ->will($this->returnValue(HttpKernelInterface::SUB_REQUEST)); + + $this->response->expects($this->never()) + ->method('getUri'); + + $this->subscriber->onKernelResponse($this->event); + } + + public function testOnKernelResponseNoWpQuery() + { + $this->event->expects($this->once()) + ->method('getResponse') + ->will($this->returnValue($this->response)); + $this->event->expects($this->once()) + ->method('getRequestType') + ->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); + + $this->request->expects($this->never()) + ->method('getUri'); + + $this->subscriber->onKernelResponse($this->event); + } + + public function testOnKernelResponseNo404() + { + $subscriber = new WordpressResponseSubscriberMock(); + + $this->event->expects($this->once()) + ->method('getRequest') + ->will($this->returnValue($this->request)); + $this->event->expects($this->once()) + ->method('getResponse') + ->will($this->returnValue($this->response)); + + $this->event->expects($this->once()) + ->method('getRequestType') + ->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); + + global $wp_query; + $wp_query = new WP_QueryMock(false); + + $this->request->expects($this->once()) + ->method('getUri') + ->will($this->returnValue('http://wwww.test.com/random-test')); + + $subscriber->onKernelResponse($this->event); + } + + public function testOnKernelResponse() + { + $subscriber = new WordpressResponseSubscriberMock(); + + $this->event->expects($this->once()) + ->method('getRequest') + ->will($this->returnValue($this->request)); + $this->event->expects($this->once()) + ->method('getResponse') + ->will($this->returnValue($this->response)); + + $this->event->expects($this->once()) + ->method('getRequestType') + ->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); + + global $wp_query; + $wp_query = new WP_QueryMock(true); + + $this->request->expects($this->once()) + ->method('getUri') + ->will($this->returnValue('http://wwww.test.com/random-test')); + $this->response->expects($this->once()) + ->method('setStatusCode') + ->with($this->equalTo(404)); + + $subscriber->onKernelResponse($this->event); + } +} + +final class WordpressResponseSubscriberMock extends WordpressResponseSubscriber +{ + public function getHttpHeadersCallback() + { + return array($this, 'getHeaders'); + } + + public function getHeaders($uri) + { + return array(); + } +} + +class WP_QueryMock { + /** + * @var bool + */ + protected $is404; + + public function __construct($is404 = false) + { + $this->is404 = $is404; + } + + public function is_404() + { + return $this->is404; + } +} From b08c2f1ac6154f9c112b266364433ce47d858015 Mon Sep 17 00:00:00 2001 From: Xavier Coureau Date: Tue, 5 Aug 2014 16:24:19 +0200 Subject: [PATCH 3/7] Fixed tests --- .../WordpressResponseSubscriber.php | 8 +++--- Resources/config/hooks.xml | 2 ++ Tests/Controller/WordpressControllerTest.php | 2 ++ .../WordpressResponseSubscriberTest.php | 27 +++++++------------ 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/Event/Subscriber/WordpressResponseSubscriber.php b/Event/Subscriber/WordpressResponseSubscriber.php index 8f3694e..2dc9def 100644 --- a/Event/Subscriber/WordpressResponseSubscriber.php +++ b/Event/Subscriber/WordpressResponseSubscriber.php @@ -11,14 +11,14 @@ class WordpressResponseSubscriber implements EventSubscriberInterface { /** - * @var string + * @var string|array */ protected $httpHeaderCallback; /** - * @param string $httpHeaderCallback + * @param string|array $httpHeaderCallback */ - public function __construct($httpHeaderCallback = 'wp_get_http_headers') + public function __construct($httpHeaderCallback) { $this->httpHeaderCallback = $httpHeaderCallback; } @@ -55,7 +55,7 @@ public function onKernelResponse(FilterResponseEvent $event) } if ($wp_query->is_404()) { - $response->setStatusCode(WordpressResponse::HTTP_NOT_FOUND); + $response->setStatusCode(404); } } diff --git a/Resources/config/hooks.xml b/Resources/config/hooks.xml index 86e906c..0d18fb9 100644 --- a/Resources/config/hooks.xml +++ b/Resources/config/hooks.xml @@ -21,6 +21,8 @@ + + wp_get_http_headers diff --git a/Tests/Controller/WordpressControllerTest.php b/Tests/Controller/WordpressControllerTest.php index 87a510d..317981e 100644 --- a/Tests/Controller/WordpressControllerTest.php +++ b/Tests/Controller/WordpressControllerTest.php @@ -19,6 +19,8 @@ */ class WordpressControllerTest extends \PHPUnit_Framework_TestCase { + protected $wp_query; + /** * @var \Ekino\WordpressBundle\Wordpress\Wordpress */ diff --git a/Tests/Event/Subscriber/WordpressResponseSubscriberTest.php b/Tests/Event/Subscriber/WordpressResponseSubscriberTest.php index 775f7a2..6afb4ce 100644 --- a/Tests/Event/Subscriber/WordpressResponseSubscriberTest.php +++ b/Tests/Event/Subscriber/WordpressResponseSubscriberTest.php @@ -23,7 +23,7 @@ protected function setUp() $this->response = $this->getMockBuilder('Ekino\WordpressBundle\Wordpress\WordpressResponse')->disableOriginalConstructor()->getMock(); $this->request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->disableOriginalConstructor()->getMock(); - $this->subscriber = new WordpressResponseSubscriber(); + $this->subscriber = new WordpressResponseSubscriber(array($this, 'getHeadersMock')); } public function testGetSubscribedEvents() @@ -37,7 +37,7 @@ public function testGetSubscribedEvents() public function testGetHttpHeadersCallback() { - $this->assertEquals('wp_get_http_headers', $this->subscriber->getHttpHeadersCallback()); + $this->assertEquals(array($this, 'getHeadersMock'), $this->subscriber->getHttpHeadersCallback()); } public function testOnKernelResponseNoWordpressResponse() @@ -87,8 +87,6 @@ public function testOnKernelResponseNoWpQuery() public function testOnKernelResponseNo404() { - $subscriber = new WordpressResponseSubscriberMock(); - $this->event->expects($this->once()) ->method('getRequest') ->will($this->returnValue($this->request)); @@ -107,13 +105,11 @@ public function testOnKernelResponseNo404() ->method('getUri') ->will($this->returnValue('http://wwww.test.com/random-test')); - $subscriber->onKernelResponse($this->event); + $this->subscriber->onKernelResponse($this->event); } public function testOnKernelResponse() { - $subscriber = new WordpressResponseSubscriberMock(); - $this->event->expects($this->once()) ->method('getRequest') ->will($this->returnValue($this->request)); @@ -135,18 +131,15 @@ public function testOnKernelResponse() ->method('setStatusCode') ->with($this->equalTo(404)); - $subscriber->onKernelResponse($this->event); - } -} - -final class WordpressResponseSubscriberMock extends WordpressResponseSubscriber -{ - public function getHttpHeadersCallback() - { - return array($this, 'getHeaders'); + $this->subscriber->onKernelResponse($this->event); } - public function getHeaders($uri) + /** + * @param string $uri + * + * @return array + */ + public function getHeadersMock($uri) { return array(); } From b1ea9da66b0365b9d22088f8147730c5d7dfa9b7 Mon Sep 17 00:00:00 2001 From: Xavier Coureau Date: Tue, 5 Aug 2014 17:12:32 +0200 Subject: [PATCH 4/7] Enhanced callback support --- Event/Subscriber/WordpressResponseSubscriber.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Event/Subscriber/WordpressResponseSubscriber.php b/Event/Subscriber/WordpressResponseSubscriber.php index 2dc9def..9e8bb64 100644 --- a/Event/Subscriber/WordpressResponseSubscriber.php +++ b/Event/Subscriber/WordpressResponseSubscriber.php @@ -42,7 +42,7 @@ public function onKernelResponse(FilterResponseEvent $event) } $callback = $this->getHttpHeadersCallback(); - $wpHeaders = (array) $callback($event->getRequest()->getUri()); + $wpHeaders = (array) call_user_func_array($callback, array($event->getRequest()->getUri())); foreach ($wpHeaders as $name => $value) { // TODO add cache headers support From 6c1d4153654ba9d41d0ca54938b16ccd0a0c4135 Mon Sep 17 00:00:00 2001 From: Xavier Coureau Date: Wed, 13 Aug 2014 12:27:06 +0200 Subject: [PATCH 5/7] Refactored way to check wordpress 404 status code --- .../WordpressResponseSubscriber.php | 17 ++- Resources/config/hooks.xml | 1 + .../WordpressResponseSubscriberTest.php | 107 ++++++++++++++++-- Wordpress/Wordpress.php | 14 +++ phpunit.xml.dist | 11 +- 5 files changed, 136 insertions(+), 14 deletions(-) diff --git a/Event/Subscriber/WordpressResponseSubscriber.php b/Event/Subscriber/WordpressResponseSubscriber.php index 9e8bb64..f4d9697 100644 --- a/Event/Subscriber/WordpressResponseSubscriber.php +++ b/Event/Subscriber/WordpressResponseSubscriber.php @@ -2,6 +2,7 @@ namespace Ekino\WordpressBundle\Event\Subscriber; +use Ekino\WordpressBundle\Wordpress\Wordpress; use Ekino\WordpressBundle\Wordpress\WordpressResponse; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\HttpKernelInterface; @@ -15,12 +16,19 @@ class WordpressResponseSubscriber implements EventSubscriberInterface */ protected $httpHeaderCallback; + /** + * @var Wordpress + */ + protected $wordpress; + /** * @param string|array $httpHeaderCallback + * @param Wordpress $wordpress */ - public function __construct($httpHeaderCallback) + public function __construct($httpHeaderCallback, Wordpress $wordpress) { $this->httpHeaderCallback = $httpHeaderCallback; + $this->wordpress = $wordpress; } /** @@ -34,10 +42,7 @@ public function onKernelResponse(FilterResponseEvent $event) return; } - /** @var \WP_Query|null $wp_query */ - global $wp_query; - - if (!$wp_query) { + if (!$wp_query = $this->wordpress->getWpQuery()) { return; } @@ -47,7 +52,7 @@ public function onKernelResponse(FilterResponseEvent $event) foreach ($wpHeaders as $name => $value) { // TODO add cache headers support if ($name == 'cache-control') { - //$response->setCache($this->parseCacheHeaders($value)); + //$response->setCache($value); continue; } diff --git a/Resources/config/hooks.xml b/Resources/config/hooks.xml index 0d18fb9..725a7b7 100644 --- a/Resources/config/hooks.xml +++ b/Resources/config/hooks.xml @@ -23,6 +23,7 @@ wp_get_http_headers + diff --git a/Tests/Event/Subscriber/WordpressResponseSubscriberTest.php b/Tests/Event/Subscriber/WordpressResponseSubscriberTest.php index 6afb4ce..d1e453f 100644 --- a/Tests/Event/Subscriber/WordpressResponseSubscriberTest.php +++ b/Tests/Event/Subscriber/WordpressResponseSubscriberTest.php @@ -11,6 +11,7 @@ class WordpressResponseSubscriberTest extends \PHPUnit_Framework_TestCase protected $event; protected $request; protected $response; + protected $wordpress; /** * @var WordpressResponseSubscriber @@ -22,8 +23,9 @@ protected function setUp() $this->event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\FilterResponseEvent')->disableOriginalConstructor()->getMock(); $this->response = $this->getMockBuilder('Ekino\WordpressBundle\Wordpress\WordpressResponse')->disableOriginalConstructor()->getMock(); $this->request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->disableOriginalConstructor()->getMock(); + $this->wordpress = $this->getMockBuilder('Ekino\WordpressBundle\Wordpress\Wordpress')->disableOriginalConstructor()->getMock(); - $this->subscriber = new WordpressResponseSubscriber(array($this, 'getHeadersMock')); + $this->subscriber = new WordpressResponseSubscriber(array($this, 'getHeadersMock'), $this->wordpress); } public function testGetSubscribedEvents() @@ -78,6 +80,9 @@ public function testOnKernelResponseNoWpQuery() $this->event->expects($this->once()) ->method('getRequestType') ->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); + $this->wordpress->expects($this->once()) + ->method('getWpQuery') + ->will($this->returnValue(null)); $this->request->expects($this->never()) ->method('getUri'); @@ -98,9 +103,9 @@ public function testOnKernelResponseNo404() ->method('getRequestType') ->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); - global $wp_query; - $wp_query = new WP_QueryMock(false); - + $this->wordpress->expects($this->once()) + ->method('getWpQuery') + ->will($this->returnValue(new WP_QueryMock())); $this->request->expects($this->once()) ->method('getUri') ->will($this->returnValue('http://wwww.test.com/random-test')); @@ -108,21 +113,85 @@ public function testOnKernelResponseNo404() $this->subscriber->onKernelResponse($this->event); } - public function testOnKernelResponse() + public function testOnKernelResponsePushHeader() { + $subscriber = new WordpressResponseSubscriber(array($this, 'getHavingHeadersMock'), $this->wordpress); + $this->event->expects($this->once()) ->method('getRequest') ->will($this->returnValue($this->request)); $this->event->expects($this->once()) ->method('getResponse') ->will($this->returnValue($this->response)); + $this->event->expects($this->once()) + ->method('getRequestType') + ->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); + $this->wordpress->expects($this->once()) + ->method('getWpQuery') + ->will($this->returnValue(new WP_QueryMock(true))); + + $parameterBag = $this->getMock('Symfony\Component\HttpFoundation\ParameterBag'); + $this->response->headers = $parameterBag; + $parameterBag->expects($this->once()) + ->method('set') + ->with($this->equalTo('test-header'), $this->equalTo('is working')); + + $this->request->expects($this->once()) + ->method('getUri') + ->will($this->returnValue('http://wwww.test.com/random-test')); + $this->response->expects($this->once()) + ->method('setStatusCode') + ->with($this->equalTo(404)); + + $subscriber->onKernelResponse($this->event); + } + public function testOnKernelResponseNoPushCacheHeader() + { + $subscriber = new WordpressResponseSubscriber(array($this, 'getHavingHeadersCacheMock'), $this->wordpress); + + $this->event->expects($this->once()) + ->method('getRequest') + ->will($this->returnValue($this->request)); + $this->event->expects($this->once()) + ->method('getResponse') + ->will($this->returnValue($this->response)); $this->event->expects($this->once()) ->method('getRequestType') ->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); + $this->wordpress->expects($this->once()) + ->method('getWpQuery') + ->will($this->returnValue(new WP_QueryMock(true))); - global $wp_query; - $wp_query = new WP_QueryMock(true); + $parameterBag = $this->getMock('Symfony\Component\HttpFoundation\ParameterBag'); + $this->response->headers = $parameterBag; + $parameterBag->expects($this->never()) + ->method('set'); + + $this->request->expects($this->once()) + ->method('getUri') + ->will($this->returnValue('http://wwww.test.com/random-test')); + $this->response->expects($this->once()) + ->method('setStatusCode') + ->with($this->equalTo(404)); + + $subscriber->onKernelResponse($this->event); + } + + public function testOnKernelResponse() + { + $this->event->expects($this->once()) + ->method('getRequest') + ->will($this->returnValue($this->request)); + $this->event->expects($this->once()) + ->method('getResponse') + ->will($this->returnValue($this->response)); + $this->event->expects($this->once()) + ->method('getRequestType') + ->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); + $this->wordpress->expects($this->once()) + ->method('getWpQuery') + ->will($this->returnValue(new WP_QueryMock(true))); $this->request->expects($this->once()) ->method('getUri') @@ -143,6 +212,30 @@ public function getHeadersMock($uri) { return array(); } + + /** + * @param string $uri + * + * @return array + */ + public function getHavingHeadersMock($uri) + { + return array( + 'test-header' => 'is working', + ); + } + + /** + * @param string $uri + * + * @return array + */ + public function getHavingHeadersCacheMock($uri) + { + return array( + 'cache-control' => 'should not work', + ); + } } class WP_QueryMock { diff --git a/Wordpress/Wordpress.php b/Wordpress/Wordpress.php index 23831ad..01c51e7 100644 --- a/Wordpress/Wordpress.php +++ b/Wordpress/Wordpress.php @@ -102,6 +102,20 @@ public function getResponse() return $this->response; } + /** + * @return null|\WP_Query + */ + public function getWpQuery() + { + global $wp_query; + + if (null === $wp_query || !$wp_query instanceof \WP_Query) { + return null; + } + + return $wp_query; + } + /** * Returns Wordpress directory if specified in configuration * otherwise returns default structure: diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 88d7c83..5aec859 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -13,8 +13,17 @@ > - ./Tests/ + ./Tests + + + ./ + + vendor + Tests + + + \ No newline at end of file From c5c6ead29601db516e2b3312f03202af74989ba1 Mon Sep 17 00:00:00 2001 From: Xavier Coureau Date: Wed, 13 Aug 2014 11:55:08 +0200 Subject: [PATCH 6/7] Fixed case when switching language from wordpress Fixed tests --- Event/Subscriber/I18n/I18nSubscriber.php | 5 ++++- Tests/Event/Subscriber/I18nSubscriberTest.php | 20 ++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Event/Subscriber/I18n/I18nSubscriber.php b/Event/Subscriber/I18n/I18nSubscriber.php index 5ae8ff0..7c60f89 100644 --- a/Event/Subscriber/I18n/I18nSubscriber.php +++ b/Event/Subscriber/I18n/I18nSubscriber.php @@ -34,7 +34,10 @@ public function __construct($defaultLocale, $wordpressI18nCookieName) public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); - $locale = $request->cookies->get($this->wordpressI18nCookieName, $this->defaultLocale); + $session = $request->getSession(); + $locale = $request->cookies->get($this->wordpressI18nCookieName, $session->get('_locale', $this->defaultLocale)); + + $session->set('_locale', $locale); $request->setLocale($locale); } diff --git a/Tests/Event/Subscriber/I18nSubscriberTest.php b/Tests/Event/Subscriber/I18nSubscriberTest.php index 782de18..3b06b02 100644 --- a/Tests/Event/Subscriber/I18nSubscriberTest.php +++ b/Tests/Event/Subscriber/I18nSubscriberTest.php @@ -32,9 +32,18 @@ public function testOnKernelRequestNoExisingCookie() $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock(); $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->disableOriginalConstructor()->getMock(); $cookies = $this->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag')->disableOriginalConstructor()->getMock(); + $session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')->disableOriginalConstructor()->getMock(); $request->cookies = $cookies; + $session->expects($this->once()) + ->method('get') + ->with($this->equalTo('_locale')) + ->will($this->returnValue($this->defaultLanguage)); + + $request->expects($this->once()) + ->method('getSession') + ->will($this->returnValue($session)); $event->expects($this->once()) ->method('getRequest') ->will($this->returnValue($request)); @@ -44,6 +53,9 @@ public function testOnKernelRequestNoExisingCookie() ->with($this->equalTo($this->cookieName), $this->equalTo($this->defaultLanguage)) ->will($this->returnValue($this->defaultLanguage)); + $session->expects($this->once()) + ->method('set') + ->with($this->equalTo('_locale'), $this->equalTo($this->defaultLanguage)); $request->expects($this->once()) ->method('setLocale') ->with($this->equalTo($this->defaultLanguage)); @@ -56,18 +68,24 @@ public function testOnKernelRequest() $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock(); $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->disableOriginalConstructor()->getMock(); $cookies = $this->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag')->disableOriginalConstructor()->getMock(); + $session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')->disableOriginalConstructor()->getMock(); $request->cookies = $cookies; + $request->expects($this->once()) + ->method('getSession') + ->will($this->returnValue($session)); $event->expects($this->once()) ->method('getRequest') ->will($this->returnValue($request)); $cookies->expects($this->once()) ->method('get') - ->with($this->equalTo($this->cookieName), $this->equalTo($this->defaultLanguage)) ->will($this->returnValue('en')); + $session->expects($this->once()) + ->method('set') + ->with($this->equalTo('_locale'), $this->equalTo('en')); $request->expects($this->once()) ->method('setLocale') ->with($this->equalTo('en')); From 255b5e4a72224d22e5edb7cc811fadc390b4a80f Mon Sep 17 00:00:00 2001 From: Xavier Coureau Date: Mon, 1 Sep 2014 21:04:57 +0200 Subject: [PATCH 7/7] Changed to model class to ease override --- Twig/Extension/PostExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Twig/Extension/PostExtension.php b/Twig/Extension/PostExtension.php index 6d4ad76..d281049 100644 --- a/Twig/Extension/PostExtension.php +++ b/Twig/Extension/PostExtension.php @@ -2,7 +2,7 @@ namespace Ekino\WordpressBundle\Twig\Extension; -use Ekino\WordpressBundle\Entity\Post; +use Ekino\WordpressBundle\Model\Post; use Ekino\WordpressBundle\Manager\PostManager; class PostExtension extends \Twig_Extension