File: Log.php

package info (click to toggle)
php-horde-test 2.6.4%2Bdebian0-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 556 kB
  • sloc: xml: 2,328; php: 730; makefile: 18
file content (127 lines) | stat: -rw-r--r-- 3,852 bytes parent folder | download | duplicates (3)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
 * Provides utilities to test for log output.
 *
 * PHP version 5
 *
 * @category Horde
 * @package  Test
 * @author   Gunnar Wrobel <wrobel@pardus.de>
 * @license  http://www.horde.org/licenses/lgpl21 LGPL
 * @link     http://www.horde.org/components/Horde_Test
 */

/**
 * Provides utilities to test for log output.
 *
 * Copyright 2011-2017 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 *
 * @category Horde
 * @package  Test
 * @author   Gunnar Wrobel <wrobel@pardus.de>
 * @license  http://www.horde.org/licenses/lgpl21 LGPL
 * @link     http://www.horde.org/components/Horde_Test
 */
class Horde_Test_Log extends Horde_Test_Case
{
    /**
     * The log handler.
     *
     * @var Horde_Log_Handler_Base
     */
    private $_logHandler;

    /**
     * Returns a log handler.
     *
     * @return Horde_Log_Logger
     */
    public function getLogger()
    {
        if (!class_exists('Horde_Log_Logger')) {
            $this->markTestSkipped('The "Horde_Log" package is missing!');
        }
        $this->_logHandler = new Horde_Log_Handler_Mock();
        return new Horde_Log_Logger($this->_logHandler);
    }

    /**
     * Asserts that the log contains the given number of messages.
     *
     * You *MUST* fetch the logger via $this->getLogger() before using this
     * method. This will store a reference to an internal mock log handler that
     * will later be used to analyze the log events.
     *
     * @param int $count The expected number of messages.
     *
     * @return Horde_Log_Logger
     */
    public function assertLogCount($count)
    {
        $this->assertEquals(count($this->_logHandler->events), $count);
    }

    /**
     * Asserts that the log contains at least one message matching the provided string.
     *
     * You *MUST* fetch the logger via $this->getLogger() before using this
     * method. This will store a reference to an internal mock log handler that
     * will later be used to analyze the log events.
     *
     * @param string $message The expected log message.
     *
     * @return Horde_Log_Logger
     */
    public function assertLogContains($message)
    {
        $messages = array();
        $found = false;
        foreach ($this->_logHandler->events as $event) {
            if (strstr($event['message'], $message) !== false) {
                $found = true;
                break;
            }
            $messages[] = $event['message'];
        }
        $this->assertTrue($found, sprintf("Did not find \"%s\" in [\n%s\n]", $message, join("\n", $messages)));
    }

    /**
     * Asserts that the log contains at least one message matching the provided regular_expression.
     *
     * You *MUST* fetch the logger via $this->getLogger() before using this
     * method. This will store a reference to an internal mock log handler that
     * will later be used to analyze the log events.
     *
     * @param string $regular_expression The expected regular expression.
     *
     * @return Horde_Log_Logger
     */
    public function assertLogRegExp($regular_expression)
    {
        $messages = array();
        $found = false;
        foreach ($this->_logHandler->events as $event) {
            if (preg_match($regular_expression, $event['message'], $matches) !== false) {
                $found = true;
                break;
            }
            $messages[] = $event['message'];
        }
        $this->assertTrue($found, sprintf("Did not find \"%s\" in [\n%s\n]", $message, join("\n", $messages)));
    }

    /**
     * Utility function to return the array of logged events.
     *
     * @return array
     */
    public function getLogOutput()
    {
        return $this->_logHandler->events;
    }

}