1: <?php
2: /**
3: * Provides utilities to test for log output.
4: *
5: * PHP version 5
6: *
7: * @category Horde
8: * @package Test
9: * @author Gunnar Wrobel <wrobel@pardus.de>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL
11: * @link http://www.horde.org/components/Horde_Test
12: */
13:
14: /**
15: * Provides utilities to test for log output.
16: *
17: * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you
20: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
21: *
22: * @since Horde_Test 1.1.0
23: *
24: * @category Horde
25: * @package Test
26: * @author Gunnar Wrobel <wrobel@pardus.de>
27: * @license http://www.horde.org/licenses/lgpl21 LGPL
28: * @link http://www.horde.org/components/Horde_Test
29: */
30: class Horde_Test_Log extends Horde_Test_Case
31: {
32: /**
33: * The log handler.
34: *
35: * @var Horde_Log_Handler_Base
36: */
37: private $_logHandler;
38:
39: /**
40: * Returns a log handler.
41: *
42: * @return Horde_Log_Logger
43: */
44: public function getLogger()
45: {
46: if (!class_exists('Horde_Log_Logger')) {
47: $this->markTestSkipped('The "Horde_Log" package is missing!');
48: }
49: $this->_logHandler = new Horde_Log_Handler_Mock();
50: return new Horde_Log_Logger($this->_logHandler);
51: }
52:
53: /**
54: * Asserts that the log contains the given number of messages.
55: *
56: * You *MUST* fetch the logger via $this->getLogger() before using this
57: * method. This will store a reference to an internal mock log handler that
58: * will later be used to analyze the log events.
59: *
60: * @param int $count The expected number of messages.
61: *
62: * @return Horde_Log_Logger
63: */
64: public function assertLogCount($count)
65: {
66: $this->assertEquals(count($this->_logHandler->events), $count);
67: }
68:
69: /**
70: * Asserts that the log contains at least one message matching the provided string.
71: *
72: * You *MUST* fetch the logger via $this->getLogger() before using this
73: * method. This will store a reference to an internal mock log handler that
74: * will later be used to analyze the log events.
75: *
76: * @param string $message The expected log message.
77: *
78: * @return Horde_Log_Logger
79: */
80: public function assertLogContains($message)
81: {
82: $messages = array();
83: $found = false;
84: foreach ($this->_logHandler->events as $event) {
85: if (strstr($event['message'], $message) !== false) {
86: $found = true;
87: break;
88: }
89: $messages[] = $event['message'];
90: }
91: $this->assertTrue($found, sprintf("Did not find \"%s\" in [\n%s\n]", $message, join("\n", $messages)));
92: }
93:
94: /**
95: * Asserts that the log contains at least one message matching the provided regular_expression.
96: *
97: * You *MUST* fetch the logger via $this->getLogger() before using this
98: * method. This will store a reference to an internal mock log handler that
99: * will later be used to analyze the log events.
100: *
101: * @param string $regular_expression The expected regular expression.
102: *
103: * @return Horde_Log_Logger
104: */
105: public function assertLogRegExp($regular_expression)
106: {
107: $messages = array();
108: $found = false;
109: foreach ($this->_logHandler->events as $event) {
110: if (preg_match($regular_expression, $event['message'], $matches) !== false) {
111: $found = true;
112: break;
113: }
114: $messages[] = $event['message'];
115: }
116: $this->assertTrue($found, sprintf("Did not find \"%s\" in [\n%s\n]", $message, join("\n", $messages)));
117: }
118: }
119: