Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/zendframework/zendframework#6041-controller-tes…
Browse files Browse the repository at this point in the history
…t-exception-throwing'

Close zendframework/zendframework#6041
  • Loading branch information
Ocramius committed Apr 2, 2014
2 parents eda913c + 5432fb9 commit dc2bed0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 37 deletions.
81 changes: 44 additions & 37 deletions src/PHPUnit/Controller/AbstractControllerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ protected function setUp()
protected function tearDown()
{
Console::overrideIsConsole($this->usedConsoleBackup);

if (true !== $this->traceError) {
return;
}

$exception = $this->getApplication()->getMvcEvent()->getParam('exception');
if ($exception instanceof \Exception) {
throw $exception;
}
}

/**
Expand All @@ -78,12 +87,13 @@ public function getTraceError()

/**
* Set the trace error flag
* @param bool $traceError
* @param bool $traceError
* @return AbstractControllerTestCase
*/
public function setTraceError($traceError)
{
$this->traceError = $traceError;

return $this;
}

Expand All @@ -98,12 +108,13 @@ public function getUseConsoleRequest()

/**
* Set the usage of the console router or not
* @param bool $boolean
* @param bool $boolean
* @return AbstractControllerTestCase
*/
public function setUseConsoleRequest($boolean)
{
$this->useConsoleRequest = (bool) $boolean;

return $this;
}

Expand All @@ -118,7 +129,7 @@ public function getApplicationConfig()

/**
* Set the application config
* @param array $applicationConfig
* @param array $applicationConfig
* @return AbstractControllerTestCase
* @throws LogicException
*/
Expand All @@ -135,6 +146,7 @@ public function setApplicationConfig($applicationConfig)
$applicationConfig['module_listener_options']['config_cache_enabled'] = false;
}
$this->applicationConfig = $applicationConfig;

return $this;
}

Expand Down Expand Up @@ -187,9 +199,9 @@ public function getResponse()
/**
* Set the request URL
*
* @param string $url
* @param string|null $method
* @param array|null $params
* @param string $url
* @param string|null $method
* @param array|null $params
* @return AbstractControllerTestCase
*/
public function url($url, $method = HttpRequest::METHOD_GET, $params = array())
Expand All @@ -199,6 +211,7 @@ public function url($url, $method = HttpRequest::METHOD_GET, $params = array())
preg_match_all('/(--\S+[= ]"\S*\s*\S*")|(--\S+=\S+|--\S+\s\S+|\S+)/', $url, $matches);
$params = str_replace(array(' "', '"'), array('=', ''), $matches[0]);
$request->params()->exchangeArray($params);

return $this;
}

Expand Down Expand Up @@ -247,9 +260,9 @@ function (&$item, $key) { $item = $key . '=' . $item; }
*
* The URL provided set the request URI in the request object.
*
* @param string $url
* @param string $url
* @param string|null $method
* @param array|null $params
* @param array|null $params
* @throws \Exception
*/
public function dispatch($url, $method = null, $params = array(), $isXmlHttpRequest = false)
Expand All @@ -270,15 +283,6 @@ public function dispatch($url, $method = null, $params = array(), $isXmlHttpRequ

$this->url($url, $method, $params);
$this->getApplication()->run();

if (true !== $this->traceError) {
return;
}

$exception = $this->getApplication()->getMvcEvent()->getParam('exception');
if ($exception instanceof \Exception) {
throw $exception;
}
}

/**
Expand Down Expand Up @@ -309,7 +313,7 @@ public function reset($keepPersistence = false)
/**
* Trigger an application event
*
* @param string $eventName
* @param string $eventName
* @return \Zend\EventManager\ResponseCollection
*/
public function triggerApplicationEvent($eventName)
Expand Down Expand Up @@ -357,7 +361,7 @@ public function assertModulesLoaded(array $modules)
/**
* Assert modules were not loaded with the module manager
*
* @param array $modules
* @param array $modules
*/
public function assertNotModulesLoaded(array $modules)
{
Expand Down Expand Up @@ -388,13 +392,14 @@ protected function getResponseStatusCode()
if (null === $match) {
$match = 0;
}

return $match;
}

/**
* Assert response status code
*
* @param int $code
* @param int $code
*/
public function assertResponseStatusCode($code)
{
Expand All @@ -418,7 +423,7 @@ public function assertResponseStatusCode($code)
/**
* Assert not response status code
*
* @param int $code
* @param int $code
*/
public function assertNotResponseStatusCode($code)
{
Expand Down Expand Up @@ -472,13 +477,14 @@ protected function getControllerFullClassName()
$controllerIdentifier = $routeMatch->getParam('controller');
$controllerManager = $this->getApplicationServiceLocator()->get('ControllerManager');
$controllerClass = $controllerManager->get($controllerIdentifier);

return get_class($controllerClass);
}

/**
* Assert that the application route match used the given module
*
* @param string $module
* @param string $module
*/
public function assertModuleName($module)
{
Expand All @@ -498,7 +504,7 @@ public function assertModuleName($module)
/**
* Assert that the application route match used NOT the given module
*
* @param string $module
* @param string $module
*/
public function assertNotModuleName($module)
{
Expand All @@ -518,7 +524,7 @@ public function assertNotModuleName($module)
/**
* Assert that the application route match used the given controller class
*
* @param string $controller
* @param string $controller
*/
public function assertControllerClass($controller)
{
Expand All @@ -538,7 +544,7 @@ public function assertControllerClass($controller)
/**
* Assert that the application route match used NOT the given controller class
*
* @param string $controller
* @param string $controller
*/
public function assertNotControllerClass($controller)
{
Expand All @@ -558,7 +564,7 @@ public function assertNotControllerClass($controller)
/**
* Assert that the application route match used the given controller name
*
* @param string $controller
* @param string $controller
*/
public function assertControllerName($controller)
{
Expand All @@ -578,7 +584,7 @@ public function assertControllerName($controller)
/**
* Assert that the application route match used NOT the given controller name
*
* @param string $controller
* @param string $controller
*/
public function assertNotControllerName($controller)
{
Expand All @@ -598,7 +604,7 @@ public function assertNotControllerName($controller)
/**
* Assert that the application route match used the given action
*
* @param string $action
* @param string $action
*/
public function assertActionName($action)
{
Expand All @@ -618,7 +624,7 @@ public function assertActionName($action)
/**
* Assert that the application route match used NOT the given action
*
* @param string $action
* @param string $action
*/
public function assertNotActionName($action)
{
Expand All @@ -638,7 +644,7 @@ public function assertNotActionName($action)
/**
* Assert that the application route match used the given route name
*
* @param string $route
* @param string $route
*/
public function assertMatchedRouteName($route)
{
Expand All @@ -658,7 +664,7 @@ public function assertMatchedRouteName($route)
/**
* Assert that the application route match used NOT the given route name
*
* @param string $route
* @param string $route
*/
public function assertNotMatchedRouteName($route)
{
Expand All @@ -678,7 +684,7 @@ public function assertNotMatchedRouteName($route)
* Assert template name
* Assert that a template was used somewhere in the view model tree
*
* @param string $templateName
* @param string $templateName
*/
public function assertTemplateName($templateName)
{
Expand All @@ -690,7 +696,7 @@ public function assertTemplateName($templateName)
* Assert not template name
* Assert that a template was not used somewhere in the view model tree
*
* @param string $templateName
* @param string $templateName
*/
public function assertNotTemplateName($templateName)
{
Expand All @@ -701,18 +707,19 @@ public function assertNotTemplateName($templateName)
/**
* Recursively search a view model and it's children for the given templateName
*
* @param ViewModel $viewModel
* @param string $templateName
* @param ViewModel $viewModel
* @param string $templateName
* @return boolean
*/
protected function searchTemplates($viewModel, $templateName)
{
if ($viewModel->getTemplate($templateName) == $templateName){
if ($viewModel->getTemplate($templateName) == $templateName) {
return true;
}
foreach ($viewModel->getChildren() as $child){
foreach ($viewModel->getChildren() as $child) {
return $this->searchTemplates($child, $templateName);
}

return false;
}
}
14 changes: 14 additions & 0 deletions test/PHPUnit/Controller/AbstractHttpControllerTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,8 @@ public function testAssertWithEventShared()

public function testAssertExceptionInAction()
{
$this->setTraceError(true);

$this->dispatch('/exception');
$this->assertResponseStatusCode(500);
$this->assertApplicationException('RuntimeException');
Expand All @@ -637,6 +639,18 @@ public function testAssertExceptionAndMessageInAction()
$this->assertApplicationException('RuntimeException', 'Foo error');
}

public function testGetErrorWithTraceErrorEnabled()
{
$this->dispatch('/exception');
$this->assertResponseStatusCode(500);

$exception = $this->getApplication()->getMvcEvent()->getParam('exception');
$this->assertInstanceOf('RuntimeException', $exception);

// set to null to avoid the throwing of the exception
$this->getApplication()->getMvcEvent()->setParam('exception', null);
}

/**
* Sample tests on MvcEvent
*/
Expand Down

0 comments on commit dc2bed0

Please sign in to comment.