stderr = new ConsoleOutput();
$this->Error = $this->getMockBuilder('Cake\Error\ConsoleErrorHandler')
->onlyMethods(['_stop'])
->setConstructorArgs([['stderr' => $this->stderr]])
->getMock();
Log::drop('stdout');
Log::drop('stderr');
}
/**
* tearDown
*
* @return void
*/
public function tearDown(): void
{
unset($this->Error);
parent::tearDown();
}
/**
* test that the console error handler can deal with Exceptions.
*
* @return void
*/
public function testHandleError()
{
$content = "Notice Error: This is a notice error\nIn [/some/file, line 275]\n";
$this->Error->expects($this->never())
->method('_stop');
$this->Error->handleError(E_NOTICE, 'This is a notice error', '/some/file', 275);
$this->assertSame($content, $this->stderr->messages()[0]);
}
/**
* test that the console error handler can deal with fatal errors.
*
* @return void
*/
public function testHandleFatalError()
{
ob_start();
$content = "Fatal Error: This is a fatal error\nIn [/some/file, line 275]\n";
$this->Error->handleError(E_USER_ERROR, 'This is a fatal error', '/some/file', 275);
$this->assertCount(1, $this->stderr->messages());
$this->assertSame($content, $this->stderr->messages()[0]);
ob_end_clean();
}
/**
* test that the console error handler can deal with CakeExceptions.
*
* @return void
*/
public function testCakeErrors()
{
$exception = new MissingActionException('Missing action');
$message = sprintf("Exception: Missing action\nIn [%s, line %s]\n", $exception->getFile(), $exception->getLine());
$this->Error->expects($this->once())
->method('_stop')
->with(1);
$this->Error->handleException($exception);
$this->assertCount(1, $this->stderr->messages());
$this->assertSame($message, $this->stderr->messages()[0]);
}
/**
* test a non Cake Exception exception.
*
* @return void
*/
public function testNonCakeExceptions()
{
$exception = new \InvalidArgumentException('Too many parameters.');
$this->Error->expects($this->once())
->method('_stop')
->with(1);
$this->Error->handleException($exception);
$this->assertStringContainsString('Too many parameters', $this->stderr->messages()[0]);
}
/**
* Test error code is used as exit code for ConsoleException.
*
* @return void
*/
public function testConsoleExceptions()
{
$exception = new ConsoleException('Test ConsoleException', 2);
$this->Error->expects($this->once())
->method('_stop')
->with(2);
$this->Error->handleException($exception);
$this->assertStringContainsString('Test ConsoleException', $this->stderr->messages()[0]);
}
}