colly_wyx
2018-05-18 50a53f7db63c5729b0ddbc93367117cf35ccf03c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
 * PhpUnderControl_PhalApiLoggerFile_Test
 *
 * 针对 ../../PhalApi/Logger/File.php PhalApi_Logger_File 类的PHPUnit单元测试
 *
 * @author: dogstar 20141217
 */
 
require_once dirname(__FILE__) . '/../test_env.php';
 
if (!class_exists('PhalApi_Logger_File')) {
    require dirname(__FILE__) . '/../../PhalApi/Logger/File.php';
}
 
class PhpUnderControl_PhalApiLoggerFile_Test extends PHPUnit_Framework_TestCase
{
    public $coreLoggerFile;
 
    protected function setUp()
    {
        parent::setUp();
 
        $cmd = sprintf('rm %s -rf', dirname(__FILE__) . '/Runtime');
        shell_exec($cmd);
 
        $this->coreLoggerFile = new PhalApi_Logger_File(dirname(__FILE__) . '/Runtime',
            PhalApi_Logger::LOG_LEVEL_DEBUG | PhalApi_Logger::LOG_LEVEL_INFO | PhalApi_Logger::LOG_LEVEL_ERROR);
    }
 
    protected function tearDown()
    {
        $cmd = sprintf('rm %s -rf', dirname(__FILE__) . '/Runtime');
        shell_exec($cmd);
    }
 
 
    /**
     * @group testLog
     */ 
    public function testLog()
    {
        $this->coreLoggerFile->log('debug', 'debug from log', '');
        $this->coreLoggerFile->log('task', 'something test for task', array('from' => 'phpunit'));
 
        $this->assertLogExists('debug from log');
        $this->assertLogExists('something test for task');
    }
 
    public function testDebug()
    {
        $this->coreLoggerFile->debug('something debug here', array('name' => 'phpunit'));
 
        $this->coreLoggerFile->debug("This 
            should not be 
            multi line");
 
        $this->assertLogExists('something debug here');
        $this->assertLogExists('This');
        $this->assertLogExists('should not be \n');
        $this->assertLogExists('multi');
    }
 
    public function testInfo()
    {
        $this->coreLoggerFile->info('something info here', 'phpunit');
        $this->coreLoggerFile->info('something info here', 2014);
        $this->coreLoggerFile->info('something info here', true);
 
        $this->assertLogExists('something info here');
        $this->assertLogExists('phpunit');
        $this->assertLogExists('2014');
        $this->assertLogExists('1');
    }
 
    public function testError()
    {
        $this->coreLoggerFile->error('WTF!');
 
        $this->assertLogExists('WTF!');
    }
 
    protected function assertLogExists($content)
    {
        $logFile = implode(DIRECTORY_SEPARATOR, array(
            dirname(__FILE__) . '/Runtime',
            'log',
            date('Ym', time()),
            date('Ymd', time()) . '.log'
        ));
 
        $this->assertContains($content, file_get_contents($logFile));
    }
}