This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5469 from snapshotpl/console-controller
Abstract console controller
- Loading branch information
Showing
5 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
tests/ZendTest/Mvc/Controller/TestAsset/ConsoleController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
{ | ||
|
||
} |