Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Adding a new line to the ConsoleErrorHandler
Refactoring ConsoleErrorHandler test to use dynamic mocks.
  • Loading branch information
markstory committed Sep 5, 2010
1 parent 9fee81c commit 8bd0f18
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
1 change: 1 addition & 0 deletions cake/console/console_error_handler.php
Expand Up @@ -97,6 +97,7 @@ public function error400($error) {
public function error500($error) {
$this->_outputMessage();
}

/**
* Outputs the exception to STDERR.
*
Expand Down
64 changes: 29 additions & 35 deletions cake/tests/cases/console/console_error_handler.test.php
Expand Up @@ -19,27 +19,21 @@
*/
require CAKE . 'console' . DS . 'console_error_handler.php';

class TestConsoleErrorHandler extends ConsoleErrorHandler {
public $output = array();

/**
* Override stderr() so it doesn't do bad things.
* ConsoleErrorHandler Test case.
*
* @param string $line
* @return void
* @package cake.tests.cases.console
*/
function stderr($line) {
$this->output[] = $line;
}
}

class ConsoleErrorHandlerTest extends CakeTestCase {

/**
* ConsoleErrorHandler Test case.
* Factory method for error handlers with stderr() mocked out.
*
* @package cake.tests.cases.console
* @return Mock object
*/
class ConsoleErrorHandlerTest extends CakeTestCase {
function getErrorHandler($exception) {
return $this->getMock('ConsoleErrorHandler', array('stderr'), array($exception));
}

/**
* test that the console error handler can deal with CakeExceptions.
Expand All @@ -48,12 +42,12 @@ class ConsoleErrorHandlerTest extends CakeTestCase {
*/
function testCakeErrors() {
$exception = new MissingActionException('Missing action');
$error = new TestConsoleErrorHandler($exception);
$error->render();
$error = $this->getErrorHandler($exception);

$error->expects($this->once())->method('stderr')
->with('Missing action');

$result = $error->output;
$this->assertEquals(1, count($result));
$this->assertEquals('Missing action', $result[0]);
$error->render();
}

/**
Expand All @@ -63,12 +57,12 @@ function testCakeErrors() {
*/
function testNonCakeExceptions() {
$exception = new InvalidArgumentException('Too many parameters.');
$error = new TestConsoleErrorHandler($exception);
$error->render();
$error = $this->getErrorHandler($exception);

$result = $error->output;
$this->assertEquals(1, count($result));
$this->assertEquals('Too many parameters.', $result[0]);
$error->expects($this->once())->method('stderr')
->with('Too many parameters.');

$error->render();
}

/**
Expand All @@ -78,12 +72,12 @@ function testNonCakeExceptions() {
*/
function testError404Exception() {
$exception = new NotFoundException('dont use me in cli.');
$error = new TestConsoleErrorHandler($exception);
$error->render();
$error = $this->getErrorHandler($exception);

$result = $error->output;
$this->assertEquals(1, count($result));
$this->assertEquals('dont use me in cli.', $result[0]);
$error->expects($this->once())->method('stderr')
->with('dont use me in cli.');

$error->render();
}

/**
Expand All @@ -93,12 +87,12 @@ function testError404Exception() {
*/
function testError500Exception() {
$exception = new InternalErrorException('dont use me in cli.');
$error = new TestConsoleErrorHandler($exception);
$error->render();
$error = $this->getErrorHandler($exception);

$result = $error->output;
$this->assertEquals(1, count($result));
$this->assertEquals('dont use me in cli.', $result[0]);
$error->expects($this->once())->method('stderr')
->with('dont use me in cli.');

$error->render();
}

/**
Expand All @@ -108,7 +102,7 @@ function testError500Exception() {
*/
function testStdErrFilehandle() {
$exception = new InternalErrorException('dont use me in cli.');
$error = new TestConsoleErrorHandler($exception);
$error = new ConsoleErrorHandler($exception);

$this->assertTrue(is_resource($error->stderr), 'No handle.');
}
Expand Down

0 comments on commit 8bd0f18

Please sign in to comment.