From 4677f116d73fd607dd1294ed0023769f5e1f5d31 Mon Sep 17 00:00:00 2001 From: Wilt Date: Tue, 23 Aug 2016 18:55:19 +0200 Subject: [PATCH 01/14] X-HTTP-Method-Override listener --- src/HttpMethodOverrideListener.php | 58 +++++++++++++++++ test/HttpMethodOverrideListenerTest.php | 85 +++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 src/HttpMethodOverrideListener.php create mode 100644 test/HttpMethodOverrideListenerTest.php diff --git a/src/HttpMethodOverrideListener.php b/src/HttpMethodOverrideListener.php new file mode 100644 index 0000000..206a486 --- /dev/null +++ b/src/HttpMethodOverrideListener.php @@ -0,0 +1,58 @@ +getRequest(); + + if(!$request instanceof HttpRequest){ + return; + } + + if(!$request->getHeaders()->has('X-HTTP-Method-Override')){ + return; + } + + $header = $request->getHeader('X-HTTP-Method-Override'); + + $method = $header->getFieldValue(); + + if (!in_array($method, $this->methods)) { + return new ApiProblemResponse(new ApiProblem( + 400, + 'unrecognized method in X-HTTP-Method-Ovverride header' + )); + } + + $request->setMethod($method); + } +} diff --git a/test/HttpMethodOverrideListenerTest.php b/test/HttpMethodOverrideListenerTest.php new file mode 100644 index 0000000..1b8669a --- /dev/null +++ b/test/HttpMethodOverrideListenerTest.php @@ -0,0 +1,85 @@ +listener = new HttpMethodOverrideListener(); + } + + /** + * @return array + */ + public function httpMethods() + { + return [ + 'head' => [HttpRequest::METHOD_HEAD], + 'post' => [HttpRequest::METHOD_POST], + 'put' => [HttpRequest::METHOD_PUT], + 'delete' => [HttpRequest::METHOD_DELETE], + 'patch' => [HttpRequest::METHOD_PATCH], + ]; + } + + /** + * @dataProvider httpMethods + */ + public function testHttpMethodOverrideListener($method) + { + $listener = $this->listener; + + $request = new HttpRequest(); + $request->setMethod('GET'); + $request->getHeaders()->addHeaderLine('X-HTTP-Method-Override', $method); + + $event = new MvcEvent(); + $event->setRequest($request); + $event->setRouteMatch($this->createRouteMatch([])); + + $result = $listener($event); + $this->assertEquals($method, $request->getMethod()); + + } + + /** + * + */ + public function testHttpMethodOverrideListenerReturnsProblemResponse() + { + $listener = $this->listener; + + $request = new HttpRequest(); + $request->setMethod('GET'); + $request->getHeaders()->addHeaderLine('X-HTTP-Method-Override', 'TEST'); + + $event = new MvcEvent(); + $event->setRequest($request); + + $result = $listener($event); + $this->assertInstanceOf('ZF\ApiProblem\ApiProblemResponse', $result); + $problem = $result->getApiProblem(); + $this->assertEquals(400, $problem->status); + $this->assertContains('unrecognized method in X-HTTP-Method-Ovverride header', $problem->detail); + } +} From 9db72f2c55b41f09c23f50c3ae396c6b7de6f5f1 Mon Sep 17 00:00:00 2001 From: Wilt Date: Tue, 23 Aug 2016 19:07:30 +0200 Subject: [PATCH 02/14] Changes following @webimpress his comments --- src/HttpMethodOverrideListener.php | 6 +++--- test/HttpMethodOverrideListenerTest.php | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/HttpMethodOverrideListener.php b/src/HttpMethodOverrideListener.php index 206a486..4014c63 100644 --- a/src/HttpMethodOverrideListener.php +++ b/src/HttpMethodOverrideListener.php @@ -34,11 +34,11 @@ public function __invoke(MvcEvent $event) { $request = $event->getRequest(); - if(!$request instanceof HttpRequest){ + if (!$request instanceof HttpRequest) { return; } - if(!$request->getHeaders()->has('X-HTTP-Method-Override')){ + if (!$request->getHeaders()->has('X-HTTP-Method-Override')) { return; } @@ -46,7 +46,7 @@ public function __invoke(MvcEvent $event) $method = $header->getFieldValue(); - if (!in_array($method, $this->methods)) { + if (! in_array($method, $this->methods)) { return new ApiProblemResponse(new ApiProblem( 400, 'unrecognized method in X-HTTP-Method-Ovverride header' diff --git a/test/HttpMethodOverrideListenerTest.php b/test/HttpMethodOverrideListenerTest.php index 1b8669a..6e702ce 100644 --- a/test/HttpMethodOverrideListenerTest.php +++ b/test/HttpMethodOverrideListenerTest.php @@ -59,7 +59,6 @@ public function testHttpMethodOverrideListener($method) $result = $listener($event); $this->assertEquals($method, $request->getMethod()); - } /** From e36c3ebb7aed3f75de28312d36be1f1ae458f6f2 Mon Sep 17 00:00:00 2001 From: Wilt Date: Tue, 23 Aug 2016 19:08:01 +0200 Subject: [PATCH 03/14] Changes following @webimpress his comments --- src/HttpMethodOverrideListener.php | 2 +- test/HttpMethodOverrideListenerTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/HttpMethodOverrideListener.php b/src/HttpMethodOverrideListener.php index 4014c63..d4b93d1 100644 --- a/src/HttpMethodOverrideListener.php +++ b/src/HttpMethodOverrideListener.php @@ -1,7 +1,7 @@ Date: Tue, 23 Aug 2016 19:08:38 +0200 Subject: [PATCH 04/14] Changes following @webimpress his comments --- test/HttpMethodOverrideListenerTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/HttpMethodOverrideListenerTest.php b/test/HttpMethodOverrideListenerTest.php index 2fce64f..c9bf9fe 100644 --- a/test/HttpMethodOverrideListenerTest.php +++ b/test/HttpMethodOverrideListenerTest.php @@ -9,6 +9,7 @@ use PHPUnit_Framework_TestCase as TestCase; use Zend\Http\Request as HttpRequest; use Zend\Mvc\MvcEvent; +use ZF\ApiProblem\ApiProblemResponse; use ZF\ContentNegotiation\HttpMethodOverrideListener; class HttpMethodOverrideListenerTest extends TestCase @@ -76,7 +77,7 @@ public function testHttpMethodOverrideListenerReturnsProblemResponse() $event->setRequest($request); $result = $listener($event); - $this->assertInstanceOf('ZF\ApiProblem\ApiProblemResponse', $result); + $this->assertInstanceOf(ApiProblemResponse::class, $result); $problem = $result->getApiProblem(); $this->assertEquals(400, $problem->status); $this->assertContains('unrecognized method in X-HTTP-Method-Ovverride header', $problem->detail); From 4b1f500ac4369c1aee7138e89070d0e72cbacb2a Mon Sep 17 00:00:00 2001 From: Wilt Date: Wed, 24 Aug 2016 09:19:12 +0200 Subject: [PATCH 05/14] Small code style improvements/typo fixes --- src/HttpMethodOverrideListener.php | 6 +++--- test/HttpMethodOverrideListenerTest.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/HttpMethodOverrideListener.php b/src/HttpMethodOverrideListener.php index d4b93d1..61af310 100644 --- a/src/HttpMethodOverrideListener.php +++ b/src/HttpMethodOverrideListener.php @@ -34,11 +34,11 @@ public function __invoke(MvcEvent $event) { $request = $event->getRequest(); - if (!$request instanceof HttpRequest) { + if (! $request instanceof HttpRequest) { return; } - if (!$request->getHeaders()->has('X-HTTP-Method-Override')) { + if (! $request->getHeaders()->has('X-HTTP-Method-Override')) { return; } @@ -49,7 +49,7 @@ public function __invoke(MvcEvent $event) if (! in_array($method, $this->methods)) { return new ApiProblemResponse(new ApiProblem( 400, - 'unrecognized method in X-HTTP-Method-Ovverride header' + 'Unrecognized method in X-HTTP-Method-Override header' )); } diff --git a/test/HttpMethodOverrideListenerTest.php b/test/HttpMethodOverrideListenerTest.php index c9bf9fe..2e2beaa 100644 --- a/test/HttpMethodOverrideListenerTest.php +++ b/test/HttpMethodOverrideListenerTest.php @@ -80,6 +80,6 @@ public function testHttpMethodOverrideListenerReturnsProblemResponse() $this->assertInstanceOf(ApiProblemResponse::class, $result); $problem = $result->getApiProblem(); $this->assertEquals(400, $problem->status); - $this->assertContains('unrecognized method in X-HTTP-Method-Ovverride header', $problem->detail); + $this->assertContains('Unrecognized method in X-HTTP-Method-Override header', $problem->detail); } } From ac6e42e6ffaf1840fdab07406eaccb2a9b770594 Mon Sep 17 00:00:00 2001 From: Wilt Date: Wed, 24 Aug 2016 09:56:21 +0200 Subject: [PATCH 06/14] Made HttpMethodOverrideListener extend AbstractListenerAggregate Attached listener depending on `x_http_method_override_enabled` Added $xHttpMethodOverrideEnabled to ContentNegotiationOptions (defaults to false); --- config/module.config.php | 9 +++++++++ src/ContentNegotiationOptions.php | 21 +++++++++++++++++++++ src/HttpMethodOverrideListener.php | 18 ++++++++++++++++-- src/Module.php | 5 +++++ test/ContentNegotiationOptionsTest.php | 1 + test/HttpMethodOverrideListenerTest.php | 4 ++-- 6 files changed, 54 insertions(+), 4 deletions(-) diff --git a/config/module.config.php b/config/module.config.php index ab4ba3d..ac700ca 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -11,6 +11,7 @@ use ZF\ContentNegotiation\AcceptListener; use ZF\ContentNegotiation\ContentNegotiationOptions; use ZF\ContentNegotiation\ContentTypeFilterListener; +use ZF\ContentNegotiation\HttpMethodOverrideListener; use ZF\ContentNegotiation\ContentTypeListener; use ZF\ContentNegotiation\ControllerPlugin; use ZF\ContentNegotiation\Factory; @@ -38,6 +39,9 @@ ], 'service_manager' => [ + 'invokables' => [ + HttpMethodOverrideListener::class => HttpMethodOverrideListener::class, + ], 'factories' => [ ContentTypeListener::class => InvokableFactory::class, 'Request' => Factory\RequestFactory::class, @@ -76,6 +80,11 @@ // Array of controller service name => allowed content type pairs. // The allowed content type may be a string, or an array of strings. 'content_type_whitelist' => [], + + // Enable x-http method override feature + // When set to 'true' the http method in the request will be overridden + // by the method inside the 'X-HTTP-Method-Override' header (if present) + 'x_http_method_override_enabled' => false, ], 'controller_plugins' => [ diff --git a/src/ContentNegotiationOptions.php b/src/ContentNegotiationOptions.php index dfca9de..0540af7 100644 --- a/src/ContentNegotiationOptions.php +++ b/src/ContentNegotiationOptions.php @@ -30,6 +30,11 @@ class ContentNegotiationOptions extends AbstractOptions */ protected $contentTypeWhitelist = []; + /** + * @var boolean + */ + protected $xHttpMethodOverrideEnabled = false; + /** * {@inheritDoc} * @@ -128,4 +133,20 @@ public function getContentTypeWhitelist() { return $this->contentTypeWhitelist; } + + /** + * @param boolean $xHttpMethodOverrideEnabled + */ + public function setXHttpMethodOverrideEnabled($xHttpMethodOverrideEnabled) + { + $this->xHttpMethodOverrideEnabled = $xHttpMethodOverrideEnabled; + } + + /** + * @return boolean + */ + public function getXHttpMethodOverrideEnabled() + { + return $this->xHttpMethodOverrideEnabled; + } } diff --git a/src/HttpMethodOverrideListener.php b/src/HttpMethodOverrideListener.php index 61af310..e68a3d9 100644 --- a/src/HttpMethodOverrideListener.php +++ b/src/HttpMethodOverrideListener.php @@ -6,12 +6,14 @@ namespace ZF\ContentNegotiation; +use Zend\EventManager\AbstractListenerAggregate; +use Zend\EventManager\EventManagerInterface; use Zend\Mvc\MvcEvent; use ZF\ApiProblem\ApiProblem; use ZF\ApiProblem\ApiProblemResponse; use Zend\Http\Request as HttpRequest; -class HttpMethodOverrideListener +class HttpMethodOverrideListener extends AbstractListenerAggregate { /** * @var array @@ -24,13 +26,25 @@ class HttpMethodOverrideListener HttpRequest::METHOD_PATCH, ]; + /** + * Priority is set very high (should be executed before all other listeners that rely on the request method value). + * TODO: Check priority value, maybe value should be even higher?? + * + * @param EventManagerInterface $events + * @param int $priority + */ + public function attach(EventManagerInterface $events, $priority = 1) + { + $this->listeners[] = $events->attach(MvcEvent::EVENT_ROUTE, [$this, 'onRoute'], -40); + } + /** * Checks for X-HTTP-Method-Override header and sets header inside request object. * * @param MvcEvent $event * @return void|ApiProblemResponse */ - public function __invoke(MvcEvent $event) + public function onRoute(MvcEvent $event) { $request = $event->getRequest(); diff --git a/src/Module.php b/src/Module.php index a593d00..54d2a46 100644 --- a/src/Module.php +++ b/src/Module.php @@ -48,6 +48,11 @@ public function onBootstrap(MvcEvent $e) $services->get(AcceptFilterListener::class)->attach($eventManager); $services->get(ContentTypeFilterListener::class)->attach($eventManager); + $contentNegotiationOptions = $services->get(ContentNegotiationOptions::class); + if ($contentNegotiationOptions->getXHttpMethodOverrideEnabled()) { + $services->get(HttpMethodOverrideListener::class)->attach($eventManager); + } + $sharedEventManager = $eventManager->getSharedManager(); $sharedEventManager->attach( DispatchableInterface::class, diff --git a/test/ContentNegotiationOptionsTest.php b/test/ContentNegotiationOptionsTest.php index 70fc1a5..81cb594 100644 --- a/test/ContentNegotiationOptionsTest.php +++ b/test/ContentNegotiationOptionsTest.php @@ -16,6 +16,7 @@ public function dashSeparatedOptions() return [ 'accept-whitelist' => ['accept-whitelist', 'accept_whitelist'], 'content-type-whitelist' => ['content-type-whitelist', 'content_type_whitelist'], + 'x-http-method-override-enabled' => ['x-http-method-override-enabled', 'x_http_method_override_enabled'], ]; } diff --git a/test/HttpMethodOverrideListenerTest.php b/test/HttpMethodOverrideListenerTest.php index 2e2beaa..41ec062 100644 --- a/test/HttpMethodOverrideListenerTest.php +++ b/test/HttpMethodOverrideListenerTest.php @@ -58,7 +58,7 @@ public function testHttpMethodOverrideListener($method) $event->setRequest($request); $event->setRouteMatch($this->createRouteMatch([])); - $result = $listener($event); + $result = $listener->onRoute($event); $this->assertEquals($method, $request->getMethod()); } @@ -76,7 +76,7 @@ public function testHttpMethodOverrideListenerReturnsProblemResponse() $event = new MvcEvent(); $event->setRequest($request); - $result = $listener($event); + $result = $listener->onRoute($event); $this->assertInstanceOf(ApiProblemResponse::class, $result); $problem = $result->getApiProblem(); $this->assertEquals(400, $problem->status); From d9586c512e650c4d737ee06e24f4b9f4441bb4d4 Mon Sep 17 00:00:00 2001 From: Wilt Date: Wed, 24 Aug 2016 10:00:16 +0200 Subject: [PATCH 07/14] Removed useless comment --- test/HttpMethodOverrideListenerTest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/HttpMethodOverrideListenerTest.php b/test/HttpMethodOverrideListenerTest.php index 41ec062..a860c32 100644 --- a/test/HttpMethodOverrideListenerTest.php +++ b/test/HttpMethodOverrideListenerTest.php @@ -62,9 +62,6 @@ public function testHttpMethodOverrideListener($method) $this->assertEquals($method, $request->getMethod()); } - /** - * - */ public function testHttpMethodOverrideListenerReturnsProblemResponse() { $listener = $this->listener; From 43768f70efa1a4eabd413a36371293a4a5432b28 Mon Sep 17 00:00:00 2001 From: Wilt Date: Wed, 24 Aug 2016 12:06:42 +0200 Subject: [PATCH 08/14] Added configuration of http-override-methods --- src/ContentNegotiationOptions.php | 21 +++++++++++ src/HttpMethodOverrideListener.php | 37 +++++++++++++------ test/ContentNegotiationOptionsTest.php | 1 + test/HttpMethodOverrideListenerTest.php | 49 ++++++++++++++++++++++--- 4 files changed, 91 insertions(+), 17 deletions(-) diff --git a/src/ContentNegotiationOptions.php b/src/ContentNegotiationOptions.php index 0540af7..295bd8a 100644 --- a/src/ContentNegotiationOptions.php +++ b/src/ContentNegotiationOptions.php @@ -35,6 +35,11 @@ class ContentNegotiationOptions extends AbstractOptions */ protected $xHttpMethodOverrideEnabled = false; + /** + * @var array + */ + protected $httpOverrideMethods = []; + /** * {@inheritDoc} * @@ -149,4 +154,20 @@ public function getXHttpMethodOverrideEnabled() { return $this->xHttpMethodOverrideEnabled; } + + /** + * @param array $httpOverrideMethods + */ + public function setHttpOverrideMethods(array $httpOverrideMethods) + { + $this->httpOverrideMethods = $httpOverrideMethods; + } + + /** + * @return array + */ + public function getHttpOverrideMethods() + { + return $this->httpOverrideMethods; + } } diff --git a/src/HttpMethodOverrideListener.php b/src/HttpMethodOverrideListener.php index e68a3d9..216aa58 100644 --- a/src/HttpMethodOverrideListener.php +++ b/src/HttpMethodOverrideListener.php @@ -18,13 +18,17 @@ class HttpMethodOverrideListener extends AbstractListenerAggregate /** * @var array */ - protected $methods = [ - HttpRequest::METHOD_HEAD, - HttpRequest::METHOD_POST, - HttpRequest::METHOD_PUT, - HttpRequest::METHOD_DELETE, - HttpRequest::METHOD_PATCH, - ]; + protected $httpMethodOverride = []; + + /** + * HttpMethodOverrideListener constructor. + * + * @param array $httpMethodOverride + */ + public function __construct(array $httpMethodOverride) + { + $this->httpMethodOverride = $httpMethodOverride; + } /** * Priority is set very high (should be executed before all other listeners that rely on the request method value). @@ -56,17 +60,26 @@ public function onRoute(MvcEvent $event) return; } - $header = $request->getHeader('X-HTTP-Method-Override'); + $method = $request->getMethod(); - $method = $header->getFieldValue(); + if (! array_key_exists($method, $this->httpMethodOverride)) { + return new ApiProblemResponse(new ApiProblem( + 400, + sprintf('Overriding %s method with X-HTTP-Method-Override header is not allowed', $method) + )); + } + + $header = $request->getHeader('X-HTTP-Method-Override'); + $overrideMethod = $header->getFieldValue(); + $allowedMethods = $this->httpMethodOverride[$method]; - if (! in_array($method, $this->methods)) { + if (! in_array($overrideMethod, $allowedMethods)) { return new ApiProblemResponse(new ApiProblem( 400, - 'Unrecognized method in X-HTTP-Method-Override header' + sprintf('Illegal override method %s in X-HTTP-Method-Override header', $overrideMethod) )); } - $request->setMethod($method); + $request->setMethod($overrideMethod); } } diff --git a/test/ContentNegotiationOptionsTest.php b/test/ContentNegotiationOptionsTest.php index 81cb594..f6f7f55 100644 --- a/test/ContentNegotiationOptionsTest.php +++ b/test/ContentNegotiationOptionsTest.php @@ -17,6 +17,7 @@ public function dashSeparatedOptions() 'accept-whitelist' => ['accept-whitelist', 'accept_whitelist'], 'content-type-whitelist' => ['content-type-whitelist', 'content_type_whitelist'], 'x-http-method-override-enabled' => ['x-http-method-override-enabled', 'x_http_method_override_enabled'], + 'http-override-methods' => ['http-override-methods', 'http_override_methods'], ]; } diff --git a/test/HttpMethodOverrideListenerTest.php b/test/HttpMethodOverrideListenerTest.php index a860c32..0f7ee01 100644 --- a/test/HttpMethodOverrideListenerTest.php +++ b/test/HttpMethodOverrideListenerTest.php @@ -21,12 +21,27 @@ class HttpMethodOverrideListenerTest extends TestCase */ protected $listener; + /** + * @var array + */ + protected $httpMethodOverride = [ + HttpRequest::METHOD_GET => [ + HttpRequest::METHOD_HEAD, + HttpRequest::METHOD_POST, + HttpRequest::METHOD_PUT, + HttpRequest::METHOD_DELETE, + HttpRequest::METHOD_PATCH, + ], + HttpRequest::METHOD_POST => [ + ] + ]; + /** * Set up test */ public function setUp() { - $this->listener = new HttpMethodOverrideListener(); + $this->listener = new HttpMethodOverrideListener($this->httpMethodOverride); } /** @@ -62,13 +77,37 @@ public function testHttpMethodOverrideListener($method) $this->assertEquals($method, $request->getMethod()); } - public function testHttpMethodOverrideListenerReturnsProblemResponse() + /** + * @dataProvider httpMethods + */ + public function testHttpMethodOverrideListenerReturnsProblemResponseForMethodNotInConfig($method) { $listener = $this->listener; $request = new HttpRequest(); - $request->setMethod('GET'); - $request->getHeaders()->addHeaderLine('X-HTTP-Method-Override', 'TEST'); + $request->setMethod('PATCH'); + $request->getHeaders()->addHeaderLine('X-HTTP-Method-Override', $method); + + $event = new MvcEvent(); + $event->setRequest($request); + + $result = $listener->onRoute($event); + $this->assertInstanceOf(ApiProblemResponse::class, $result); + $problem = $result->getApiProblem(); + $this->assertEquals(400, $problem->status); + $this->assertContains('Overriding PATCH method with X-HTTP-Method-Override header is not allowed', $problem->detail); + } + + /** + * @dataProvider httpMethods + */ + public function testHttpMethodOverrideListenerReturnsProblemResponseForIllegalOverrideValue($method) + { + $listener = $this->listener; + + $request = new HttpRequest(); + $request->setMethod('POST'); + $request->getHeaders()->addHeaderLine('X-HTTP-Method-Override', $method); $event = new MvcEvent(); $event->setRequest($request); @@ -77,6 +116,6 @@ public function testHttpMethodOverrideListenerReturnsProblemResponse() $this->assertInstanceOf(ApiProblemResponse::class, $result); $problem = $result->getApiProblem(); $this->assertEquals(400, $problem->status); - $this->assertContains('Unrecognized method in X-HTTP-Method-Override header', $problem->detail); + $this->assertContains(sprintf('Illegal override method %s in X-HTTP-Method-Override header', $method), $problem->detail); } } From 30887a7fb0b1cee9faa8e5ee270e6345ea739d97 Mon Sep 17 00:00:00 2001 From: Wilt Date: Wed, 24 Aug 2016 12:09:23 +0200 Subject: [PATCH 09/14] Added configuration of http-override-methods --- config/module.config.php | 4 +-- .../HttpMethodOverrideListenerFactory.php | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 src/Factory/HttpMethodOverrideListenerFactory.php diff --git a/config/module.config.php b/config/module.config.php index ac700ca..651f1ec 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -39,9 +39,6 @@ ], 'service_manager' => [ - 'invokables' => [ - HttpMethodOverrideListener::class => HttpMethodOverrideListener::class, - ], 'factories' => [ ContentTypeListener::class => InvokableFactory::class, 'Request' => Factory\RequestFactory::class, @@ -49,6 +46,7 @@ AcceptFilterListener::class => Factory\AcceptFilterListenerFactory::class, ContentTypeFilterListener::class => Factory\ContentTypeFilterListenerFactory::class, ContentNegotiationOptions::class => Factory\ContentNegotiationOptionsFactory::class, + HttpMethodOverrideListener::class => Factory\HttpMethodOverrideListenerFactory::class, ], ], diff --git a/src/Factory/HttpMethodOverrideListenerFactory.php b/src/Factory/HttpMethodOverrideListenerFactory.php new file mode 100644 index 0000000..4f6f658 --- /dev/null +++ b/src/Factory/HttpMethodOverrideListenerFactory.php @@ -0,0 +1,27 @@ +get(ContentNegotiationOptions::class); + $httpOverrideMethods = $options->getHttpOverrideMethods(); + $listener = new HttpMethodOverrideListener($httpOverrideMethods); + + return $listener; + } +} From 37a4d74dfc65d5e040c1ab97690b8fb36de02e83 Mon Sep 17 00:00:00 2001 From: Wilt Date: Wed, 24 Aug 2016 12:13:03 +0200 Subject: [PATCH 10/14] Added HttpMethodOverrideListenerFactory test --- .../HttpMethodOverrideListenerFactoryTest.php | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 test/Factory/HttpMethodOverrideListenerFactoryTest.php diff --git a/test/Factory/HttpMethodOverrideListenerFactoryTest.php b/test/Factory/HttpMethodOverrideListenerFactoryTest.php new file mode 100644 index 0000000..e6afd2b --- /dev/null +++ b/test/Factory/HttpMethodOverrideListenerFactoryTest.php @@ -0,0 +1,31 @@ +setService( + ContentNegotiationOptions::class, + new ContentNegotiationOptions() + ); + + $factory = new HttpMethodOverrideListenerFactory(); + + $service = $factory($serviceManager, 'HttpMethodOverrideListener'); + + $this->assertInstanceOf(HttpMethodOverrideListener::class, $service); + } +} From 3023a4aebf24bcbbce181e79b98a7c4405e58243 Mon Sep 17 00:00:00 2001 From: Wilt Date: Wed, 24 Aug 2016 12:22:59 +0200 Subject: [PATCH 11/14] Fix for travis test fail (line length) --- test/HttpMethodOverrideListenerTest.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/HttpMethodOverrideListenerTest.php b/test/HttpMethodOverrideListenerTest.php index 0f7ee01..95b121a 100644 --- a/test/HttpMethodOverrideListenerTest.php +++ b/test/HttpMethodOverrideListenerTest.php @@ -95,7 +95,10 @@ public function testHttpMethodOverrideListenerReturnsProblemResponseForMethodNot $this->assertInstanceOf(ApiProblemResponse::class, $result); $problem = $result->getApiProblem(); $this->assertEquals(400, $problem->status); - $this->assertContains('Overriding PATCH method with X-HTTP-Method-Override header is not allowed', $problem->detail); + $this->assertContains( + 'Overriding PATCH method with X-HTTP-Method-Override header is not allowed', + $problem->detail + ); } /** @@ -116,6 +119,9 @@ public function testHttpMethodOverrideListenerReturnsProblemResponseForIllegalOv $this->assertInstanceOf(ApiProblemResponse::class, $result); $problem = $result->getApiProblem(); $this->assertEquals(400, $problem->status); - $this->assertContains(sprintf('Illegal override method %s in X-HTTP-Method-Override header', $method), $problem->detail); + $this->assertContains( + sprintf('Illegal override method %s in X-HTTP-Method-Override header', $method), + $problem->detail + ); } } From 027784532bb66d819005278d0a933f9e6960c566 Mon Sep 17 00:00:00 2001 From: Wilt Date: Wed, 24 Aug 2016 14:41:06 +0200 Subject: [PATCH 12/14] changed test to use prophecy removed trailing comma removed space --- src/HttpMethodOverrideListener.php | 4 ++-- .../HttpMethodOverrideListenerFactoryTest.php | 17 +++++++++-------- test/HttpMethodOverrideListenerTest.php | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/HttpMethodOverrideListener.php b/src/HttpMethodOverrideListener.php index 216aa58..301ea31 100644 --- a/src/HttpMethodOverrideListener.php +++ b/src/HttpMethodOverrideListener.php @@ -34,8 +34,8 @@ public function __construct(array $httpMethodOverride) * Priority is set very high (should be executed before all other listeners that rely on the request method value). * TODO: Check priority value, maybe value should be even higher?? * - * @param EventManagerInterface $events - * @param int $priority + * @param EventManagerInterface $events + * @param int $priority */ public function attach(EventManagerInterface $events, $priority = 1) { diff --git a/test/Factory/HttpMethodOverrideListenerFactoryTest.php b/test/Factory/HttpMethodOverrideListenerFactoryTest.php index e6afd2b..118f61a 100644 --- a/test/Factory/HttpMethodOverrideListenerFactoryTest.php +++ b/test/Factory/HttpMethodOverrideListenerFactoryTest.php @@ -1,12 +1,13 @@ setService( - ContentNegotiationOptions::class, - new ContentNegotiationOptions() - ); + $options = $this->prophesize(ContentNegotiationOptions::class); + $options->getHttpOverrideMethods()->willReturn([]); - $factory = new HttpMethodOverrideListenerFactory(); + $container = $this->prophesize(ServiceManager::class); + $container->willImplement(ServiceLocatorInterface::class); + $container->get(ContentNegotiationOptions::class)->willReturn($options); - $service = $factory($serviceManager, 'HttpMethodOverrideListener'); + $factory = new HttpMethodOverrideListenerFactory(); + $service = $factory($container->reveal(), HttpMethodOverrideListener::class); $this->assertInstanceOf(HttpMethodOverrideListener::class, $service); } diff --git a/test/HttpMethodOverrideListenerTest.php b/test/HttpMethodOverrideListenerTest.php index 95b121a..a2d039b 100644 --- a/test/HttpMethodOverrideListenerTest.php +++ b/test/HttpMethodOverrideListenerTest.php @@ -30,7 +30,7 @@ class HttpMethodOverrideListenerTest extends TestCase HttpRequest::METHOD_POST, HttpRequest::METHOD_PUT, HttpRequest::METHOD_DELETE, - HttpRequest::METHOD_PATCH, + HttpRequest::METHOD_PATCH ], HttpRequest::METHOD_POST => [ ] From 8bac35c251071f10b4947cc4a761e9b8276ed2d1 Mon Sep 17 00:00:00 2001 From: Wilt Date: Wed, 24 Aug 2016 14:43:09 +0200 Subject: [PATCH 13/14] year 2016 --- src/Factory/HttpMethodOverrideListenerFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Factory/HttpMethodOverrideListenerFactory.php b/src/Factory/HttpMethodOverrideListenerFactory.php index 4f6f658..a437f0a 100644 --- a/src/Factory/HttpMethodOverrideListenerFactory.php +++ b/src/Factory/HttpMethodOverrideListenerFactory.php @@ -1,7 +1,7 @@ Date: Wed, 24 Aug 2016 14:59:04 +0200 Subject: [PATCH 14/14] added trailing commas added php doc --- test/Factory/HttpMethodOverrideListenerFactoryTest.php | 3 +++ test/HttpMethodOverrideListenerTest.php | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/test/Factory/HttpMethodOverrideListenerFactoryTest.php b/test/Factory/HttpMethodOverrideListenerFactoryTest.php index 118f61a..5bf9517 100644 --- a/test/Factory/HttpMethodOverrideListenerFactoryTest.php +++ b/test/Factory/HttpMethodOverrideListenerFactoryTest.php @@ -7,6 +7,7 @@ namespace ZFTest\ContentNegotiation\Factory; use PHPUnit_Framework_TestCase as TestCase; +use Prophecy\Prophecy\ObjectProphecy; use Zend\ServiceManager\ServiceLocatorInterface; use Zend\ServiceManager\ServiceManager; use ZF\ContentNegotiation\ContentNegotiationOptions; @@ -17,9 +18,11 @@ class HttpMethodOverrideListenerFactoryTest extends TestCase { public function testCreateServiceShouldReturnContentTypeFilterListenerInstance() { + /** @var ContentNegotiationOptions|ObjectProphecy $options */ $options = $this->prophesize(ContentNegotiationOptions::class); $options->getHttpOverrideMethods()->willReturn([]); + /** @var ServiceManager|ObjectProphecy $container */ $container = $this->prophesize(ServiceManager::class); $container->willImplement(ServiceLocatorInterface::class); $container->get(ContentNegotiationOptions::class)->willReturn($options); diff --git a/test/HttpMethodOverrideListenerTest.php b/test/HttpMethodOverrideListenerTest.php index a2d039b..653385a 100644 --- a/test/HttpMethodOverrideListenerTest.php +++ b/test/HttpMethodOverrideListenerTest.php @@ -30,10 +30,10 @@ class HttpMethodOverrideListenerTest extends TestCase HttpRequest::METHOD_POST, HttpRequest::METHOD_PUT, HttpRequest::METHOD_DELETE, - HttpRequest::METHOD_PATCH + HttpRequest::METHOD_PATCH, ], HttpRequest::METHOD_POST => [ - ] + ], ]; /**