X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2FTestLogger.php;h=21d1bf2504e80875024547f20b1222af661fe0b7;hb=01c3bf3431e9754b79e4a4a31fa74ce9e6616514;hp=7099c3aceaf1160719c203c8d4b630cb7fb620ee;hpb=4b63ca7113ee48b8c33ad19abc5b89d452b3590e;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/TestLogger.php b/tests/phpunit/includes/TestLogger.php index 7099c3acea..21d1bf2504 100644 --- a/tests/phpunit/includes/TestLogger.php +++ b/tests/phpunit/includes/TestLogger.php @@ -2,7 +2,7 @@ /** * Testing logger * - * Copyright (C) 2015 Brad Jorsch + * Copyright (C) 2015 Wikimedia Foundation and contributors * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,7 +20,6 @@ * http://www.gnu.org/copyleft/gpl.html * * @file - * @author Brad Jorsch */ use Psr\Log\LogLevel; @@ -33,30 +32,49 @@ use Psr\Log\LogLevel; */ class TestLogger extends \Psr\Log\AbstractLogger { private $collect = false; - private $buffer = array(); + private $collectContext = false; + private $buffer = []; private $filter = null; /** - * @param bool $collect Whether to collect logs + * @param bool $collect Whether to collect logs. @see setCollect() * @param callable $filter Filter logs before collecting/printing. Signature is - * string|null function ( string $message, string $level ); + * string|null function ( string $message, string $level, array $context ); + * @param bool $collectContext Whether to keep the context passed to log. + * @since 1.29 @see setCollectContext() */ - public function __construct( $collect = false, $filter = null ) { + public function __construct( $collect = false, $filter = null, $collectContext = false ) { $this->collect = $collect; + $this->collectContext = $collectContext; $this->filter = $filter; } /** * Set the "collect" flag * @param bool $collect + * @return TestLogger $this */ public function setCollect( $collect ) { $this->collect = $collect; + return $this; + } + + /** + * Set the collectContext flag + * + * @param bool $collectContext + * @since 1.29 + * @return TestLogger $this + */ + public function setCollectContext( $collectContext ) { + $this->collectContext = $collectContext; + return $this; } /** * Return the collected logs - * @return array Array of array( string $level, string $message ) + * @return array Array of array( string $level, string $message ), or + * array( string $level, string $message, array $context ) if $collectContext was true. */ public function getBuffer() { return $this->buffer; @@ -66,21 +84,25 @@ class TestLogger extends \Psr\Log\AbstractLogger { * Clear the collected log buffer */ public function clearBuffer() { - $this->buffer = array(); + $this->buffer = []; } - public function log( $level, $message, array $context = array() ) { + public function log( $level, $message, array $context = [] ) { $message = trim( $message ); if ( $this->filter ) { - $message = call_user_func( $this->filter, $message, $level ); + $message = call_user_func( $this->filter, $message, $level, $context ); if ( $message === null ) { return; } } if ( $this->collect ) { - $this->buffer[] = array( $level, $message ); + if ( $this->collectContext ) { + $this->buffer[] = [ $level, $message, $context ]; + } else { + $this->buffer[] = [ $level, $message ]; + } } else { switch ( $level ) { case LogLevel::DEBUG: