Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Abstract console controller #5469

Merged
merged 5 commits into from

5 participants

@snapshotpl

My idea is create simple console controller ready to use with console ;) What are you think?

@weierophinney

This would be even more useful with an initializer that injects the Console instance if available. :)

@stefanotorresi

:+1: now add a test!

...ary/Zend/Mvc/Controller/AbstractConsoleController.php
((29 lines not shown))
+ {
+ $this->console = $console;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function dispatch(RequestInterface $request, ResponseInterface $response = null)
+ {
+ if (! $request instanceof ConsoleRequest) {
+ throw new InvalidArgumentException('Expected an Console request');
+ }
+
+ return parent::dispatch($request, $response);
+ }
+}

add new line after } for eof_ending

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...ary/Zend/Mvc/Controller/AbstractConsoleController.php
((24 lines not shown))
+
+ /**
+ * @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 InvalidArgumentException('Expected an Console request');

"a" Console, not "an" Console

Please be more specific in the exception message.

@ThomasCantonnet Example? I think that stack trace will say everything in this case.

Exactly, I need more detail in the Exception message so I don't have to go directly to the stack trace ;)

@snapshotpl

throw new InvalidArgumentException(sprintf(
    '%s can only dispatch requests in a console environment',
    get_called_class()
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weierophinney weierophinney was assigned
@weierophinney weierophinney merged commit 619ba2f into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 12, 2013
  1. @snapshotpl
  2. @snapshotpl
Commits on Nov 13, 2013
  1. @snapshotpl

    Fix typo

    snapshotpl authored
  2. @snapshotpl
Commits on Nov 14, 2013
  1. @snapshotpl

    Change exception message

    snapshotpl authored
This page is out of date. Refresh to see the latest.
View
57 library/Zend/Mvc/Controller/AbstractConsoleController.php
@@ -0,0 +1,57 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @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 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;
+
+class AbstractConsoleController extends AbstractActionController
+{
+ /**
+ * @var ConsoleAdaper
+ */
+ protected $console;
+
+ /**
+ * @param ConsoleAdaper $console
+ * @return self
+ */
+ public function setConsole(ConsoleAdaper $console)
+ {
+ $this->console = $console;
+
+ return $this;
+ }
+
+ /**
+ * @return ConsoleAdaper
+ */
+ public function getConsole()
+ {
+ return $this->console;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function dispatch(RequestInterface $request, ResponseInterface $response = null)
+ {
+ if (! $request instanceof ConsoleRequest) {
+ throw new InvalidArgumentException(sprintf(
+ '%s can only dispatch requests in a console environment',
+ get_called_class()
+ ));
+ }
+ return parent::dispatch($request, $response);
+ }
+}
View
4 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'));
}
View
67 tests/ZendTest/Mvc/Controller/ConsoleControllerTest.php
@@ -0,0 +1,67 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @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 ZendTest\Mvc\Controller;
+
+use PHPUnit_Framework_TestCase as TestCase;
+use Zend\Console\Request as ConsoleRequest;
+use Zend\Http\Request;
+use Zend\Mvc\MvcEvent;
+use Zend\Mvc\Router\RouteMatch;
+
+class ConsoleControllerTest extends TestCase
+{
+ /**
+ * @var TestAsset\ConsoleController
+ */
+ public $controller;
+
+ public function setUp()
+ {
+ $this->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');
+
+ $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);
+ }
+}
View
10 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();
View
17 tests/ZendTest/Mvc/Controller/TestAsset/ConsoleController.php
@@ -0,0 +1,17 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @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 ZendTest\Mvc\Controller\TestAsset;
+
+use Zend\Mvc\Controller\AbstractConsoleController;
+
+class ConsoleController extends AbstractConsoleController
+{
+
+}
Something went wrong with that request. Please try again.