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/error-reporting'
Browse files Browse the repository at this point in the history
Close #26
  • Loading branch information
weierophinney committed Jun 15, 2016
2 parents d6bbef7 + ab2b401 commit e344ab9
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 121 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 3.0.1 - TBD
## 3.0.1 - 2016-06-15

### Added

Expand All @@ -18,7 +18,10 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#26](https://github.com/zendframework/zend-test/pull/26) fixes how
`$traceErrors` works under PHP 7 and PHPUnit 5. Any zend-test-specific
assertion failures now append a list of all exception messages to the base
message when the flag is enabled.

## 3.0.0 - 2016-05-31

Expand Down
8 changes: 4 additions & 4 deletions src/PHPUnit/Controller/AbstractConsoleControllerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public function assertConsoleOutputContains($match)
{
$response = $this->getResponse();
if (false === stripos($response->getContent(), $match)) {
throw new PHPUnit_Framework_ExpectationFailedException(
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage(
sprintf(
'Failed asserting output CONTAINS content "%s", actual content is "%s"',
$match,
$response->getContent()
)
);
));
}
$this->assertNotSame(false, stripos($response->getContent(), $match));
}
Expand All @@ -50,10 +50,10 @@ public function assertNotConsoleOutputContains($match)
{
$response = $this->getResponse();
if (false !== stripos($response->getContent(), $match)) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage(sprintf(
'Failed asserting output DOES NOT CONTAIN content "%s"',
$match
));
)));
}
$this->assertSame(false, stripos($response->getContent(), $match));
}
Expand Down
118 changes: 71 additions & 47 deletions src/PHPUnit/Controller/AbstractControllerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,39 @@ protected function setUp()
protected function tearDown()
{
Console::overrideIsConsole($this->usedConsoleBackup);
}

/**
* Create a failure message.
*
* If $traceError is true, appends exception details, if any.
*
* @param string $message
* @return string
*/
protected function createFailureMessage($message)
{
if (true !== $this->traceError) {
return;
return $message;
}

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

$messages = [];
do {
$messages[] = sprintf(
"Exception '%s' with message '%s' in %s:%d",
get_class($exception),
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
);
} while ($exception = $exception->getPrevious());

return sprintf("%s\n\nExceptions raised:\n%s\n", $message, implode("\n\n", $messages));
}

/**
Expand Down Expand Up @@ -356,9 +380,9 @@ public function assertModulesLoaded(array $modules)
$modulesLoaded = $moduleManager->getModules();
$list = array_diff($modules, $modulesLoaded);
if ($list) {
throw new PHPUnit_Framework_ExpectationFailedException(
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage(
sprintf('Several modules are not loaded "%s"', implode(', ', $list))
);
));
}
$this->assertEquals(count($list), 0);
}
Expand All @@ -374,9 +398,9 @@ public function assertNotModulesLoaded(array $modules)
$modulesLoaded = $moduleManager->getModules();
$list = array_intersect($modules, $modulesLoaded);
if ($list) {
throw new PHPUnit_Framework_ExpectationFailedException(
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage(
sprintf('Several modules WAS not loaded "%s"', implode(', ', $list))
);
));
}
$this->assertEquals(count($list), 0);
}
Expand Down Expand Up @@ -410,16 +434,16 @@ public function assertResponseStatusCode($code)
{
if ($this->useConsoleRequest) {
if (!in_array($code, [0, 1])) {
throw new PHPUnit_Framework_ExpectationFailedException(
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage(
'Console status code assert value must be O (valid) or 1 (error)'
);
));
}
}
$match = $this->getResponseStatusCode();
if ($code != $match) {
throw new PHPUnit_Framework_ExpectationFailedException(
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage(
sprintf('Failed asserting response code "%s", actual status code is "%s"', $code, $match)
);
));
}
$this->assertEquals($code, $match);
}
Expand All @@ -433,16 +457,16 @@ public function assertNotResponseStatusCode($code)
{
if ($this->useConsoleRequest) {
if (!in_array($code, [0, 1])) {
throw new PHPUnit_Framework_ExpectationFailedException(
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage(
'Console status code assert value must be O (valid) or 1 (error)'
);
));
}
}
$match = $this->getResponseStatusCode();
if ($code == $match) {
throw new PHPUnit_Framework_ExpectationFailedException(
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage(
sprintf('Failed asserting response code was NOT "%s"', $code)
);
));
}
$this->assertNotEquals($code, $match);
}
Expand All @@ -457,9 +481,9 @@ public function assertApplicationException($type, $message = null)
{
$exception = $this->getApplication()->getMvcEvent()->getParam('exception');
if (!$exception) {
throw new PHPUnit_Framework_ExpectationFailedException(
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage(
'Failed asserting application exception, exception not exist'
);
));
}
if (true === $this->traceError) {
// set exception as null because we know and have assert the exception
Expand All @@ -478,7 +502,7 @@ protected function getControllerFullClassName()
{
$routeMatch = $this->getApplication()->getMvcEvent()->getRouteMatch();
if (!$routeMatch) {
throw new PHPUnit_Framework_ExpectationFailedException('No route matched');
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage('No route matched'));
}
$controllerIdentifier = $routeMatch->getParam('controller');
$controllerManager = $this->getApplicationServiceLocator()->get('ControllerManager');
Expand All @@ -499,9 +523,9 @@ public function assertModuleName($module)
$match = strtolower($match);
$module = strtolower($module);
if ($module != $match) {
throw new PHPUnit_Framework_ExpectationFailedException(
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage(
sprintf('Failed asserting module name "%s", actual module name is "%s"', $module, $match)
);
));
}
$this->assertEquals($module, $match);
}
Expand All @@ -518,9 +542,9 @@ public function assertNotModuleName($module)
$match = strtolower($match);
$module = strtolower($module);
if ($module == $match) {
throw new PHPUnit_Framework_ExpectationFailedException(
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage(
sprintf('Failed asserting module was NOT "%s"', $module)
);
));
}
$this->assertNotEquals($module, $match);
}
Expand All @@ -537,9 +561,9 @@ public function assertControllerClass($controller)
$match = strtolower($match);
$controller = strtolower($controller);
if ($controller != $match) {
throw new PHPUnit_Framework_ExpectationFailedException(
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage(
sprintf('Failed asserting controller class "%s", actual controller class is "%s"', $controller, $match)
);
));
}
$this->assertEquals($controller, $match);
}
Expand All @@ -556,9 +580,9 @@ public function assertNotControllerClass($controller)
$match = strtolower($match);
$controller = strtolower($controller);
if ($controller == $match) {
throw new PHPUnit_Framework_ExpectationFailedException(
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage(
sprintf('Failed asserting controller class was NOT "%s"', $controller)
);
));
}
$this->assertNotEquals($controller, $match);
}
Expand All @@ -572,15 +596,15 @@ public function assertControllerName($controller)
{
$routeMatch = $this->getApplication()->getMvcEvent()->getRouteMatch();
if (!$routeMatch) {
throw new PHPUnit_Framework_ExpectationFailedException('No route matched');
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage('No route matched'));
}
$match = $routeMatch->getParam('controller');
$match = strtolower($match);
$controller = strtolower($controller);
if ($controller != $match) {
throw new PHPUnit_Framework_ExpectationFailedException(
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage(
sprintf('Failed asserting controller name "%s", actual controller name is "%s"', $controller, $match)
);
));
}
$this->assertEquals($controller, $match);
}
Expand All @@ -594,15 +618,15 @@ public function assertNotControllerName($controller)
{
$routeMatch = $this->getApplication()->getMvcEvent()->getRouteMatch();
if (!$routeMatch) {
throw new PHPUnit_Framework_ExpectationFailedException('No route matched');
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage('No route matched'));
}
$match = $routeMatch->getParam('controller');
$match = strtolower($match);
$controller = strtolower($controller);
if ($controller == $match) {
throw new PHPUnit_Framework_ExpectationFailedException(
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage(
sprintf('Failed asserting controller name was NOT "%s"', $controller)
);
));
}
$this->assertNotEquals($controller, $match);
}
Expand All @@ -616,15 +640,15 @@ public function assertActionName($action)
{
$routeMatch = $this->getApplication()->getMvcEvent()->getRouteMatch();
if (!$routeMatch) {
throw new PHPUnit_Framework_ExpectationFailedException('No route matched');
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage('No route matched'));
}
$match = $routeMatch->getParam('action');
$match = strtolower($match);
$action = strtolower($action);
if ($action != $match) {
throw new PHPUnit_Framework_ExpectationFailedException(
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage(
sprintf('Failed asserting action name "%s", actual action name is "%s"', $action, $match)
);
));
}
$this->assertEquals($action, $match);
}
Expand All @@ -638,15 +662,15 @@ public function assertNotActionName($action)
{
$routeMatch = $this->getApplication()->getMvcEvent()->getRouteMatch();
if (!$routeMatch) {
throw new PHPUnit_Framework_ExpectationFailedException('No route matched');
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage('No route matched'));
}
$match = $routeMatch->getParam('action');
$match = strtolower($match);
$action = strtolower($action);
if ($action == $match) {
throw new PHPUnit_Framework_ExpectationFailedException(
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage(
sprintf('Failed asserting action name was NOT "%s"', $action)
);
));
}
$this->assertNotEquals($action, $match);
}
Expand All @@ -660,15 +684,15 @@ public function assertMatchedRouteName($route)
{
$routeMatch = $this->getApplication()->getMvcEvent()->getRouteMatch();
if (!$routeMatch) {
throw new PHPUnit_Framework_ExpectationFailedException('No route matched');
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage('No route matched'));
}
$match = $routeMatch->getMatchedRouteName();
$match = strtolower($match);
$route = strtolower($route);
if ($route != $match) {
throw new PHPUnit_Framework_ExpectationFailedException(
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage(
sprintf('Failed asserting matched route name was "%s", actual matched route name is "%s"', $route, $match)
);
));
}
$this->assertEquals($route, $match);
}
Expand All @@ -682,15 +706,15 @@ public function assertNotMatchedRouteName($route)
{
$routeMatch = $this->getApplication()->getMvcEvent()->getRouteMatch();
if (!$routeMatch) {
throw new PHPUnit_Framework_ExpectationFailedException('No route matched');
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage('No route matched'));
}
$match = $routeMatch->getMatchedRouteName();
$match = strtolower($match);
$route = strtolower($route);
if ($route == $match) {
throw new PHPUnit_Framework_ExpectationFailedException(
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage(
sprintf('Failed asserting route matched was NOT "%s"', $route)
);
));
}
$this->assertNotEquals($route, $match);
}
Expand All @@ -704,10 +728,10 @@ public function assertNoMatchedRoute()
if ($routeMatch) {
$match = $routeMatch->getMatchedRouteName();
$match = strtolower($match);
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
throw new PHPUnit_Framework_ExpectationFailedException($this->createFailureMessage(sprintf(
'Failed asserting that no route matched, actual matched route name is "%s"',
$match
));
)));
}
$this->assertNull($routeMatch);
}
Expand Down
Loading

0 comments on commit e344ab9

Please sign in to comment.