Skip to content

Commit

Permalink
Fix failing tests caused by Cache/Log changes.
Browse files Browse the repository at this point in the history
Use mocks where possible to make tests simpler and hit the filesystem
less frequently.
  • Loading branch information
markstory committed Aug 15, 2013
1 parent 30037f9 commit 46a8a31
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 70 deletions.
4 changes: 2 additions & 2 deletions lib/Cake/Console/Shell.php
Expand Up @@ -893,7 +893,7 @@ protected function _useLogger($enable = true) {
'types' => ['emergency', 'alert', 'critical', 'error', 'warning'],
'stream' => $this->stderr,
]);
Log::engine('stdout', $stdout);
Log::engine('stderr', $stderr);
Log::config('stdout', ['engine' => $stdout]);
Log::config('stderr', ['engine' => $stderr]);
}
}
4 changes: 3 additions & 1 deletion lib/Cake/Test/TestCase/Console/ShellTest.php
Expand Up @@ -833,14 +833,16 @@ public function testFileAndConsoleLogging() {
['write'],
[['types' => 'error']]
);
Log::engine('console', $mock);
Log::config('console', ['engine' => $mock]);
$mock->expects($this->once())
->method('write')
->with('error', $this->Shell->testMessage);
$this->Shell->log_something();
$this->assertTrue(file_exists(LOGS . 'error.log'));
$contents = file_get_contents(LOGS . 'error.log');
$this->assertContains($this->Shell->testMessage, $contents);

Log::drop('console');
}

/**
Expand Down
90 changes: 42 additions & 48 deletions lib/Cake/Test/TestCase/Error/ErrorHandlerTest.php
Expand Up @@ -57,6 +57,11 @@ public function setUp() {
$request->base = '';
Router::setRequestInfo($request);
Configure::write('debug', 2);

$this->_logger = $this->getMock('Cake\Log\LogInterface');
Log::config('error_test', [
'engine' => $this->_logger
]);
}

/**
Expand All @@ -66,6 +71,7 @@ public function setUp() {
*/
public function tearDown() {
parent::tearDown();
Log::drop('error_test');
if ($this->_restoreError) {
restore_error_handler();
}
Expand Down Expand Up @@ -143,24 +149,15 @@ public function testErrorSuppressed() {
public function testHandleErrorDebugOff() {
Configure::write('debug', 0);
Configure::write('Error.trace', false);
if (file_exists(LOGS . 'debug.log')) {
unlink(LOGS . 'debug.log');
}

$this->_logger->expects($this->once())
->method('write')
->with('notice', 'Notice (8): Undefined variable: out in [' . __FILE__ . ', line 160]');

set_error_handler('Cake\Error\ErrorHandler::handleError');
$this->_restoreError = true;

$out .= '';

$result = file(LOGS . 'debug.log');
$this->assertEquals(1, count($result));
$this->assertRegExp(
'/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (Notice|Debug): Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
$result[0]
);
if (file_exists(LOGS . 'debug.log')) {
unlink(LOGS . 'debug.log');
}
}

/**
Expand All @@ -171,25 +168,19 @@ public function testHandleErrorDebugOff() {
public function testHandleErrorLoggingTrace() {
Configure::write('debug', 0);
Configure::write('Error.trace', true);
if (file_exists(LOGS . 'debug.log')) {
unlink(LOGS . 'debug.log');
}

$this->_logger->expects($this->once())
->method('write')
->with('notice', $this->logicalAnd(
$this->stringContains('Notice (8): Undefined variable: out in '),
$this->stringContains('Trace:'),
$this->stringContains(__NAMESPACE__ . '\ErrorHandlerTest::testHandleErrorLoggingTrace()')
));

set_error_handler('Cake\Error\ErrorHandler::handleError');
$this->_restoreError = true;

$out .= '';

$result = file(LOGS . 'debug.log');
$this->assertRegExp(
'/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (Notice|Debug): Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
$result[0]
);
$this->assertRegExp('/^Trace:/', $result[1]);
$this->assertRegExp('/^' . preg_quote(__NAMESPACE__, '/') . '\\\ErrorHandlerTest\:\:testHandleErrorLoggingTrace\(\)/', $result[2]);
if (file_exists(LOGS . 'debug.log')) {
unlink(LOGS . 'debug.log');
}
}

/**
Expand Down Expand Up @@ -217,24 +208,24 @@ public function testHandleException() {
* @return void
*/
public function testHandleExceptionLog() {
if (file_exists(LOGS . 'error.log')) {
unlink(LOGS . 'error.log');
}
Configure::write('Exception', [
'handler' => 'Cake\Error\ErrorHandler::handleException',
'renderer' => 'Cake\Error\ExceptionRenderer',
'log' => true
]);
$error = new Error\NotFoundException('Kaboom!');

$this->_logger->expects($this->once())
->method('write')
->with('error', $this->logicalAnd(
$this->stringContains('[Cake\Error\NotFoundException] Kaboom!'),
$this->stringContains('ErrorHandlerTest->testHandleExceptionLog')
));

ob_start();
ErrorHandler::handleException($error);
$result = ob_get_clean();
$this->assertRegExp('/Kaboom!/', $result, 'message missing.');

$log = file(LOGS . 'error.log');
$this->assertContains('[Cake\Error\NotFoundException] Kaboom!', $log[0], 'message missing.');
$this->assertContains('ErrorHandlerTest->testHandleExceptionLog', $log[2], 'Stack trace missing.');
}

/**
Expand All @@ -243,9 +234,6 @@ public function testHandleExceptionLog() {
* @return void
*/
public function testHandleExceptionLogSkipping() {
if (file_exists(LOGS . 'error.log')) {
unlink(LOGS . 'error.log');
}
Configure::write('Exception', [
'handler' => 'Cake\Error\ErrorHandler::handleException',
'renderer' => 'Cake\Error\ExceptionRenderer',
Expand All @@ -255,6 +243,13 @@ public function testHandleExceptionLogSkipping() {
$notFound = new Error\NotFoundException('Kaboom!');
$forbidden = new Error\ForbiddenException('Fooled you!');

$this->_logger->expects($this->once())
->method('write')
->with(
'error',
$this->stringContains('[Cake\Error\ForbiddenException] Fooled you!')
);

ob_start();
ErrorHandler::handleException($notFound);
$result = ob_get_clean();
Expand All @@ -264,10 +259,6 @@ public function testHandleExceptionLogSkipping() {
ErrorHandler::handleException($forbidden);
$result = ob_get_clean();
$this->assertRegExp('/Fooled you!/', $result, 'message missing.');

$log = file(LOGS . 'error.log');
$this->assertNotContains('[Cake\Error\NotFoundException] Kaboom!', $log[0], 'message should not be logged.');
$this->assertContains('[Cake\Error\ForbiddenException] Fooled you!', $log[0], 'message missing.');
}

/**
Expand Down Expand Up @@ -330,22 +321,25 @@ public function testHandleFatalErrorPage() {
* @return void
*/
public function testHandleFatalErrorLog() {
if (file_exists(LOGS . 'error.log')) {
unlink(LOGS . 'error.log');
}
Configure::write('Exception', [
'handler' => 'Cake\Error\ErrorHandler::handleException',
'renderer' => 'Cake\Error\ExceptionRenderer',
'log' => true
]);
$this->_logger->expects($this->at(0))
->method('write')
->with('error', $this->logicalAnd(
$this->stringContains(__FILE__ . ', line 341'),
$this->stringContains('Fatal Error (1)'),
$this->stringContains('Something wrong')
));
$this->_logger->expects($this->at(1))
->method('write')
->with('error', $this->stringContains('[Cake\Error\FatalErrorException] Something wrong'));

ob_start();
ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, __LINE__);
ob_clean();

$log = file(LOGS . 'error.log');
$this->assertContains(__FILE__, $log[0], 'missing filename');
$this->assertContains('[Cake\Error\FatalErrorException] Something wrong', $log[1], 'message missing.');
}

}
38 changes: 21 additions & 17 deletions lib/Cake/Test/TestCase/Network/Email/EmailTest.php
Expand Up @@ -19,7 +19,6 @@
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Log\Log;
use Cake\Log\LogInterface;
use Cake\Network\Email\Email;
use Cake\TestSuite\TestCase;
use Cake\Utility\File;
Expand Down Expand Up @@ -133,6 +132,7 @@ public function setUp() {
public function tearDown() {
parent::tearDown();
App::build();
Log::drop('email');
}

/**
Expand Down Expand Up @@ -1113,7 +1113,6 @@ public function testSendWithNoContentDispositionAttachments() {
* @return void
*/
public function testSendWithLog() {
$path = CAKE . 'Test/TestApp/tmp/';
$log = $this->getMock('Cake\Log\Engine\BaseLog', ['write'], [['scopes' => 'email']]);

$message = 'Logging This';
Expand All @@ -1129,7 +1128,7 @@ public function testSendWithLog() {
)
);

Log::engine('email', $log);
Log::config('email', ['engine' => $log]);

$this->CakeEmail->transport('Debug');
$this->CakeEmail->to('me@cakephp.org');
Expand All @@ -1145,25 +1144,30 @@ public function testSendWithLog() {
* @return void
*/
public function testSendWithLogAndScope() {
Configure::write('Log.email', array(
'engine' => 'File',
'path' => TMP,
'file' => 'cake_test_emails',
'scopes' => array('email')
));
Log::reset();
$message = 'Logging This';

$log = $this->getMock('Cake\Log\Engine\BaseLog', ['write'], ['scopes' => ['email']]);
$log->expects($this->once())
->method('write')
->with(
'debug',
$this->logicalAnd(
$this->stringContains($message),
$this->stringContains('cake@cakephp.org'),
$this->stringContains('me@cakephp.org')
)
);

Log::config('email', [
'engine' => $log,
]);

$this->CakeEmail->transport('Debug');
$this->CakeEmail->to('me@cakephp.org');
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->subject('My title');
$this->CakeEmail->config(array('log' => array('scope' => 'email')));
$result = $this->CakeEmail->send("Logging This");

$File = new File(TMP . 'cake_test_emails.log');
$log = $File->read();
$this->assertTrue(strpos($log, $result['headers']) !== false);
$this->assertTrue(strpos($log, $result['message']) !== false);
$File->delete();
$this->CakeEmail->send($message);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/Utility/DebuggerTest.php
Expand Up @@ -444,7 +444,7 @@ public function testExportVarZero() {
*/
public function testLog() {
$mock = $this->getMock('Cake\Log\Engine\BaseLog', ['write']);
Log::engine('test', $mock);
Log::config('test', ['engine' => $mock]);

$mock->expects($this->at(0))
->method('write')
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/View/ViewTest.php
Expand Up @@ -751,7 +751,7 @@ public function testElementCacheHelperNoCache() {
*/
public function testElementCache() {
Cache::drop('test_view');
Configure::write('Cache.test_view', [
Cache::config('test_view', [
'engine' => 'File',
'duration' => '+1 day',
'path' => CACHE . 'views/',
Expand Down

0 comments on commit 46a8a31

Please sign in to comment.