Merge "database: Add extra sanity check to selectFieldValues()"
[lhc/web/wiklou.git] / tests / phpunit / includes / TestLogger.php
1 <?php
2 /**
3 * Testing logger
4 *
5 * Copyright (C) 2015 Brad Jorsch <bjorsch@wikimedia.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @author Brad Jorsch <bjorsch@wikimedia.org>
24 */
25
26 use Psr\Log\LogLevel;
27
28 /**
29 * A logger that may be configured to either buffer logs or to print them to
30 * the output where PHPUnit will complain about them.
31 *
32 * @since 1.27
33 */
34 class TestLogger extends \Psr\Log\AbstractLogger {
35 private $collect = false;
36 private $buffer = [];
37 private $filter = null;
38
39 /**
40 * @param bool $collect Whether to collect logs
41 * @param callable $filter Filter logs before collecting/printing. Signature is
42 * string|null function ( string $message, string $level );
43 */
44 public function __construct( $collect = false, $filter = null ) {
45 $this->collect = $collect;
46 $this->filter = $filter;
47 }
48
49 /**
50 * Set the "collect" flag
51 * @param bool $collect
52 */
53 public function setCollect( $collect ) {
54 $this->collect = $collect;
55 }
56
57 /**
58 * Return the collected logs
59 * @return array Array of array( string $level, string $message )
60 */
61 public function getBuffer() {
62 return $this->buffer;
63 }
64
65 /**
66 * Clear the collected log buffer
67 */
68 public function clearBuffer() {
69 $this->buffer = [];
70 }
71
72 public function log( $level, $message, array $context = [] ) {
73 $message = trim( $message );
74
75 if ( $this->filter ) {
76 $message = call_user_func( $this->filter, $message, $level );
77 if ( $message === null ) {
78 return;
79 }
80 }
81
82 if ( $this->collect ) {
83 $this->buffer[] = [ $level, $message ];
84 } else {
85 switch ( $level ) {
86 case LogLevel::DEBUG:
87 case LogLevel::INFO:
88 case LogLevel::NOTICE:
89 trigger_error( "LOG[$level]: $message", E_USER_NOTICE );
90 break;
91
92 case LogLevel::WARNING:
93 trigger_error( "LOG[$level]: $message", E_USER_WARNING );
94 break;
95
96 case LogLevel::ERROR:
97 case LogLevel::CRITICAL:
98 case LogLevel::ALERT:
99 case LogLevel::EMERGENCY:
100 trigger_error( "LOG[$level]: $message", E_USER_ERROR );
101 break;
102 }
103 }
104 }
105 }