Skip to content
Permalink
Browse files

Fix failing tests.

Make tests for ConsoleLog much simpler.
  • Loading branch information...
markstory committed Sep 26, 2012
1 parent 07f8e5a commit cb8aad81bb87923a7d1e782ca438701bd843050b
@@ -11,8 +11,8 @@
* @since CakePHP(tm) v 2.2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Log\Engine;
use Cake\Console\ConsoleOutput;
use Cake\Utility\Hash;
use Cake\Error;
@@ -66,6 +66,10 @@ public function tearDown() {
* @return void
*/
public function testTranslationCaching() {
$this->skipIf(
!Cache::engine('_cake_core_'),
'Missing _cake_core_ cache config, cannot test caching.'
);
Configure::write('Config.language', 'cache_test_po');
// reset internally stored entries
@@ -74,8 +78,6 @@ public function testTranslationCaching() {
Cache::clear(false, '_cake_core_');
$lang = Configure::read('Config.language');
Cache::config('_cake_core_', Cache::config('default'));
// make some calls to translate using different domains
$this->assertEquals('Dom 1 Foo', I18n::translate('dom1.foo', false, 'dom1'));
$this->assertEquals('Dom 1 Bar', I18n::translate('dom1.bar', false, 'dom1'));
@@ -12,129 +12,73 @@
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Log.Engine
* @since CakePHP(tm) v 1.3
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Log\Engine;
use Cake\Console\ConsoleOutput;
use Cake\Log\Engine\ConsoleLog;
use Cake\Log\Log;
use Cake\TestSuite\TestCase;
class TestConsoleLog extends ConsoleLog {
}
class TestCakeLog extends Log {
public static function replace($key, &$engine) {
static::$_Collection->{$key} = $engine;
}
}
/**
* ConsoleLogTest class
*
* @package Cake.Test.Case.Log.Engine
*/
class ConsoleLogTest extends TestCase {
public function setUp() {
parent::setUp();
Log::config('debug', array(
'engine' => 'Cake\Log\Engine\FileLog',
'types' => array('notice', 'info', 'debug'),
'file' => 'debug',
));
Log::config('error', array(
'engine' => 'Cake\Log\Engine\FileLog',
'types' => array('error', 'warning'),
'file' => 'error',
));
}
public function tearDown() {
parent::tearDown();
if (file_exists(LOGS . 'error.log')) {
unlink(LOGS . 'error.log');
}
if (file_exists(LOGS . 'debug.log')) {
unlink(LOGS . 'debug.log');
}
}
/**
* Test writing to ConsoleOutput
*/
public function testConsoleOutputWrites() {
TestCakeLog::config('test_console_log', array(
'engine' => __NAMESPACE__ . '\TestConsoleLog',
));
$mock = $this->getMock(__NAMESPACE__ . '\TestConsoleLog', array('write'), array(
array('types' => 'error'),
));
TestCakeLog::replace('test_console_log', $mock);
$message = 'Test error message';
$mock->expects($this->once())
->method('write');
TestCakeLog::write(LOG_ERR, $message);
}
$output = $this->getMock('Cake\Console\ConsoleOutput');
/**
* Test logging to both ConsoleLog and FileLog
*/
public function testCombinedLogWriting() {
TestCakeLog::config('test_console_log', array(
'engine' => __NAMESPACE__ . '\TestConsoleLog',
));
$mock = $this->getMock(__NAMESPACE__ . '\TestConsoleLog', array('write'), array(
array('types' => 'error'),
));
TestCakeLog::replace('test_console_log', $mock);
$output->expects($this->at(0))
->method('outputAs');
// log to both file and console
$message = 'Test error message';
$mock->expects($this->once())
->method('write');
TestCakeLog::write(LOG_ERR, $message);
$this->assertTrue(file_exists(LOGS . 'error.log'), 'error.log missing');
$logOutput = file_get_contents(LOGS . 'error.log');
$this->assertContains($message, $logOutput);
$message = '<error>' . date('Y-m-d H:i:s') . " Error: oh noes\n</error>";
$output->expects($this->at(1))
->method('write')
->with($message);
// TestConsoleLog is only interested in `error` type
$message = 'Test info message';
$mock->expects($this->never())
->method('write');
TestCakeLog::write(LOG_INFO, $message);
$log = new ConsoleLog([
'stream' => $output
]);
$log->write('error', 'oh noes');
}
// checks that output is correctly written in the correct logfile
$this->assertTrue(file_exists(LOGS . 'error.log'), 'error.log missing');
$this->assertTrue(file_exists(LOGS . 'debug.log'), 'debug.log missing');
$logOutput = file_get_contents(LOGS . 'error.log');
$this->assertNotContains($message, $logOutput);
$logOutput = file_get_contents(LOGS . 'debug.log');
$this->assertContains($message, $logOutput);
public function testWriteToFileStream() {
$filename = tempnam(sys_get_temp_dir(), 'cake_log_test');
$log = new ConsoleLog([
'stream' => $filename
]);
$log->write('error', 'oh noes');
$fh = fopen($filename, 'r');
$line = fgets($fh);
$this->assertContains('Error: oh noes', $line);
}
/**
* test default value of stream 'outputAs'
*/
public function testDefaultOutputAs() {
TestCakeLog::config('test_console_log', array(
'engine' => __NAMESPACE__ . '\TestConsoleLog',
));
if (DS == '\\' && !(bool)env('ANSICON')) {
$expected = ConsoleOutput::PLAIN;
} else {
$expected = ConsoleOutput::COLOR;
}
$stream = TestCakeLog::stream('test_console_log');
$config = $stream->config();
$output = $this->getMock('Cake\Console\ConsoleOutput');
$output->expects($this->at(0))
->method('outputAs')
->with($expected);
$log = new ConsoleLog([
'stream' => $output,
]);
$config = $log->config();
$this->assertEquals($expected, $config['outputAs']);
}

0 comments on commit cb8aad8

Please sign in to comment.
You can’t perform that action at this time.