From 3577cd2cf5ff308181363894891976056e0671d0 Mon Sep 17 00:00:00 2001 From: snapshotpl Date: Tue, 12 Nov 2013 21:59:42 +0100 Subject: [PATCH 1/6] Abstract console controller --- .../Controller/AbstractConsoleController.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 library/Zend/Mvc/Controller/AbstractConsoleController.php diff --git a/library/Zend/Mvc/Controller/AbstractConsoleController.php b/library/Zend/Mvc/Controller/AbstractConsoleController.php new file mode 100644 index 00000000000..579f9795687 --- /dev/null +++ b/library/Zend/Mvc/Controller/AbstractConsoleController.php @@ -0,0 +1,32 @@ + + */ +namespace Zend\Mvc\Controller; + +use Zend\Console\Request as ConsoleRequest; +use Zend\Stdlib\RequestInterface; +use Zend\Stdlib\ResponseInterface; + +class AbstractConsoleController extends AbstractActionController +{ + protected $console; + + public function setConsole(Zend\Console\Adapter\AdapterInterface $console) + { + $this->console = $console; + } + + public function dispatch(RequestInterface $request, ResponseInterface $response = null) + { + if (! $request instanceof ConsoleRequest) { + throw new Exception\InvalidArgumentException( + 'Expected an Console request'); + } + + parent::dispatch($request, $response); + } +} \ No newline at end of file From dd4b94eee9b831c67c4bcd87940b31180ffea885 Mon Sep 17 00:00:00 2001 From: snapshotpl Date: Tue, 12 Nov 2013 22:42:14 +0100 Subject: [PATCH 2/6] Inject console in ConsoleManager, missing license and phpdocs --- .../Controller/AbstractConsoleController.php | 26 ++++++++++++++----- .../Zend/Mvc/Controller/ControllerManager.php | 4 +++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/library/Zend/Mvc/Controller/AbstractConsoleController.php b/library/Zend/Mvc/Controller/AbstractConsoleController.php index 579f9795687..c6bd1926deb 100644 --- a/library/Zend/Mvc/Controller/AbstractConsoleController.php +++ b/library/Zend/Mvc/Controller/AbstractConsoleController.php @@ -1,32 +1,44 @@ + * @link http://github.com/zendframework/zf2 for the canonical source repository + * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License */ + namespace Zend\Mvc\Controller; +use Exception\InvalidArgumentException; +use Zend\Console\Adapter\AdapterInterface as ConsoleAdaper; use Zend\Console\Request as ConsoleRequest; use Zend\Stdlib\RequestInterface; use Zend\Stdlib\ResponseInterface; class AbstractConsoleController extends AbstractActionController { + /** + * @var ConsoleAdaper + */ protected $console; - public function setConsole(Zend\Console\Adapter\AdapterInterface $console) + /** + * @param ConsoleAdaper $console + */ + public function setConsole(ConsoleAdaper $console) { $this->console = $console; } + /** + * {@inheritdoc} + */ public function dispatch(RequestInterface $request, ResponseInterface $response = null) { if (! $request instanceof ConsoleRequest) { - throw new Exception\InvalidArgumentException( - 'Expected an Console request'); + throw new InvalidArgumentException('Expected an Console request'); } - parent::dispatch($request, $response); + return parent::dispatch($request, $response); } } \ No newline at end of file diff --git a/library/Zend/Mvc/Controller/ControllerManager.php b/library/Zend/Mvc/Controller/ControllerManager.php index 3e1d1e62486..bcb40b4c26b 100644 --- a/library/Zend/Mvc/Controller/ControllerManager.php +++ b/library/Zend/Mvc/Controller/ControllerManager.php @@ -80,6 +80,10 @@ public function injectControllerDependencies($controller, ServiceLocatorInterfac } } + if ($controller instanceof AbstractConsoleController) { + $controller->setConsole($parentLocator->get('Console')); + } + if (method_exists($controller, 'setPluginManager')) { $controller->setPluginManager($parentLocator->get('ControllerPluginManager')); } From 30ebfec6f56f4fba3aab40aad827804417eebe50 Mon Sep 17 00:00:00 2001 From: snapshotpl Date: Wed, 13 Nov 2013 18:01:10 +0100 Subject: [PATCH 3/6] Fix typo --- library/Zend/Mvc/Controller/AbstractConsoleController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/Zend/Mvc/Controller/AbstractConsoleController.php b/library/Zend/Mvc/Controller/AbstractConsoleController.php index c6bd1926deb..5612a5f60aa 100644 --- a/library/Zend/Mvc/Controller/AbstractConsoleController.php +++ b/library/Zend/Mvc/Controller/AbstractConsoleController.php @@ -36,9 +36,9 @@ public function setConsole(ConsoleAdaper $console) public function dispatch(RequestInterface $request, ResponseInterface $response = null) { if (! $request instanceof ConsoleRequest) { - throw new InvalidArgumentException('Expected an Console request'); + throw new InvalidArgumentException('Expected a Console request'); } return parent::dispatch($request, $response); } -} \ No newline at end of file +} From f34039c9bce049e402f78faf20fa7b7279c940c0 Mon Sep 17 00:00:00 2001 From: snapshotpl Date: Wed, 13 Nov 2013 22:39:02 +0100 Subject: [PATCH 4/6] Tests for console controller --- .../Controller/AbstractConsoleController.php | 16 ++++- .../Mvc/Controller/ConsoleControllerTest.php | 67 +++++++++++++++++++ .../Mvc/Controller/ControllerManagerTest.php | 10 +++ .../TestAsset/ConsoleController.php | 17 +++++ 4 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 tests/ZendTest/Mvc/Controller/ConsoleControllerTest.php create mode 100644 tests/ZendTest/Mvc/Controller/TestAsset/ConsoleController.php diff --git a/library/Zend/Mvc/Controller/AbstractConsoleController.php b/library/Zend/Mvc/Controller/AbstractConsoleController.php index 5612a5f60aa..9a60d5787ad 100644 --- a/library/Zend/Mvc/Controller/AbstractConsoleController.php +++ b/library/Zend/Mvc/Controller/AbstractConsoleController.php @@ -9,9 +9,9 @@ namespace Zend\Mvc\Controller; -use Exception\InvalidArgumentException; use Zend\Console\Adapter\AdapterInterface as ConsoleAdaper; use Zend\Console\Request as ConsoleRequest; +use Zend\Mvc\Exception\InvalidArgumentException; use Zend\Stdlib\RequestInterface; use Zend\Stdlib\ResponseInterface; @@ -24,10 +24,21 @@ class AbstractConsoleController extends AbstractActionController /** * @param ConsoleAdaper $console + * @return self */ public function setConsole(ConsoleAdaper $console) { $this->console = $console; + + return $this; + } + + /** + * @return ConsoleAdaper + */ + public function getConsole() + { + return $this->console; } /** @@ -36,9 +47,8 @@ public function setConsole(ConsoleAdaper $console) public function dispatch(RequestInterface $request, ResponseInterface $response = null) { if (! $request instanceof ConsoleRequest) { - throw new InvalidArgumentException('Expected a Console request'); + throw new InvalidArgumentException('Expected a console request'); } - return parent::dispatch($request, $response); } } diff --git a/tests/ZendTest/Mvc/Controller/ConsoleControllerTest.php b/tests/ZendTest/Mvc/Controller/ConsoleControllerTest.php new file mode 100644 index 00000000000..f72e3a8a3c1 --- /dev/null +++ b/tests/ZendTest/Mvc/Controller/ConsoleControllerTest.php @@ -0,0 +1,67 @@ +controller = new TestAsset\ConsoleController(); + $routeMatch = new RouteMatch(array('controller' => 'controller-sample')); + $event = new MvcEvent(); + $event->setRouteMatch($routeMatch); + $this->controller->setEvent($event); + } + + public function testDispatchCorrectRequest() + { + $request = new ConsoleRequest(); + $result = $this->controller->dispatch($request); + + $this->assertNotNull($result); + } + + public function testDispatchIncorrectRequest() + { + $this->setExpectedException('\Zend\Mvc\Exception\InvalidArgumentException', 'Expected a console request'); + + $request = new Request(); + $this->controller->dispatch($request); + } + + public function testGetNoInjectedConsole() + { + $console = $this->controller->getConsole(); + + $this->assertNull($console); + } + + public function testGetInjectedConsole() + { + $consoleAdapter = $this->getMock('\Zend\Console\Adapter\AdapterInterface'); + + $controller = $this->controller->setConsole($consoleAdapter); + $console = $this->controller->getConsole(); + + $this->assertInstanceOf('\Zend\Mvc\Controller\AbstractConsoleController', $controller); + $this->assertInstanceOf('\Zend\Console\Adapter\AdapterInterface', $console); + } +} diff --git a/tests/ZendTest/Mvc/Controller/ControllerManagerTest.php b/tests/ZendTest/Mvc/Controller/ControllerManagerTest.php index 83d98e25ae0..c36e8e9022b 100644 --- a/tests/ZendTest/Mvc/Controller/ControllerManagerTest.php +++ b/tests/ZendTest/Mvc/Controller/ControllerManagerTest.php @@ -15,17 +15,20 @@ use Zend\Mvc\Controller\ControllerManager; use Zend\Mvc\Controller\PluginManager as ControllerPluginManager; use Zend\ServiceManager\ServiceManager; +use Zend\Console\Adapter\Virtual as ConsoleAdapter; class ControllerManagerTest extends TestCase { public function setUp() { $this->events = new EventManager(); + $this->consoleAdapter = new ConsoleAdapter(); $this->sharedEvents = new SharedEventManager; $this->events->setSharedManager($this->sharedEvents); $this->plugins = new ControllerPluginManager(); $this->services = new ServiceManager(); + $this->services->setService('Console', $this->consoleAdapter); $this->services->setService('Zend\ServiceManager\ServiceLocatorInterface', $this->services); $this->services->setService('EventManager', $this->events); $this->services->setService('SharedEventManager', $this->sharedEvents); @@ -51,6 +54,13 @@ public function testInjectControllerDependenciesInjectsExpectedDependencies() $this->assertSame($this->sharedEvents, $events->getSharedManager()); } + public function testInjectControllerDependenciesToConsoleController() + { + $controller = new TestAsset\ConsoleController(); + $this->controllers->injectControllerDependencies($controller, $this->controllers); + $this->assertInstanceOf('Zend\Console\Adapter\AdapterInterface', $controller->getConsole()); + } + public function testInjectControllerDependenciesWillNotOverwriteExistingEventManager() { $events = new EventManager(); diff --git a/tests/ZendTest/Mvc/Controller/TestAsset/ConsoleController.php b/tests/ZendTest/Mvc/Controller/TestAsset/ConsoleController.php new file mode 100644 index 00000000000..f4d47fe7304 --- /dev/null +++ b/tests/ZendTest/Mvc/Controller/TestAsset/ConsoleController.php @@ -0,0 +1,17 @@ + Date: Fri, 15 Nov 2013 00:02:05 +0100 Subject: [PATCH 5/6] Change exception message --- library/Zend/Mvc/Controller/AbstractConsoleController.php | 5 ++++- tests/ZendTest/Mvc/Controller/ConsoleControllerTest.php | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/library/Zend/Mvc/Controller/AbstractConsoleController.php b/library/Zend/Mvc/Controller/AbstractConsoleController.php index 9a60d5787ad..d62007edbaf 100644 --- a/library/Zend/Mvc/Controller/AbstractConsoleController.php +++ b/library/Zend/Mvc/Controller/AbstractConsoleController.php @@ -47,7 +47,10 @@ public function getConsole() public function dispatch(RequestInterface $request, ResponseInterface $response = null) { if (! $request instanceof ConsoleRequest) { - throw new InvalidArgumentException('Expected a console request'); + throw new InvalidArgumentException(sprintf( + '%s can only dispatch requests in a console environment', + get_called_class() + )); } return parent::dispatch($request, $response); } diff --git a/tests/ZendTest/Mvc/Controller/ConsoleControllerTest.php b/tests/ZendTest/Mvc/Controller/ConsoleControllerTest.php index f72e3a8a3c1..fc28452267a 100644 --- a/tests/ZendTest/Mvc/Controller/ConsoleControllerTest.php +++ b/tests/ZendTest/Mvc/Controller/ConsoleControllerTest.php @@ -41,7 +41,7 @@ public function testDispatchCorrectRequest() public function testDispatchIncorrectRequest() { - $this->setExpectedException('\Zend\Mvc\Exception\InvalidArgumentException', 'Expected a console request'); + $this->setExpectedException('\Zend\Mvc\Exception\InvalidArgumentException'); $request = new Request(); $this->controller->dispatch($request); From 072d017ddc0d5bba8f7660bd2f6a1cce17824201 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Fri, 3 Jan 2014 16:27:52 -0600 Subject: [PATCH 6/6] [#5469] Added note to README changelog --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 693fc9d3078..78b98cf1e9d 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,10 @@ version of PHP available to ensure you have the latest security fixes. Additional updates that may affect existing applications include: +- [#5469](https://github.com/zendframework/zf2/pull/5469) adds a new abstract + controller, `Zend\Mvc\Controller\AbstractConsoleController`, for simplifying + the creation of console controllers. + - [#5364](https://github.com/zendframework/zf2/pull/5364) adds "naming strategies" to hydrators, allowing transformation of the data keys when either hydrating or extracting data sets. This is implemented via a new