MediaWiki  master
MediaWikiLoggerPHPUnitTestListener.php
Go to the documentation of this file.
1 <?php
2 
6 
12 class MediaWikiLoggerPHPUnitTestListener extends PHPUnit_Framework_BaseTestListener {
14  private $originalSpi;
16  private $spi;
17 
23  public function startTest( PHPUnit_Framework_Test $test ) {
24  $this->lastTestLogs = null;
25  $this->originalSpi = LoggerFactory::getProvider();
26  $this->spi = new LogCapturingSpi( $this->originalSpi );
27  LoggerFactory::registerProvider( $this->spi );
28  }
29 
30  public function addRiskyTest( PHPUnit_Framework_Test $test, Exception $e, $time ) {
31  $this->augmentTestWithLogs( $test );
32  }
33 
34  public function addIncompleteTest( PHPUnit_Framework_Test $test, Exception $e, $time ) {
35  $this->augmentTestWithLogs( $test );
36  }
37 
38  public function addSkippedTest( PHPUnit_Framework_Test $test, Exception $e, $time ) {
39  $this->augmentTestWithLogs( $test );
40  }
41 
42  public function addError( PHPUnit_Framework_Test $test, Exception $e, $time ) {
43  $this->augmentTestWithLogs( $test );
44  }
45 
46  public function addWarning( PHPUnit_Framework_Test $test, PHPUnit\Framework\Warning $e, $time ) {
47  $this->augmentTestWithLogs( $test );
48  }
49 
50  public function addFailure( PHPUnit_Framework_Test $test,
51  PHPUnit_Framework_AssertionFailedError $e, $time
52  ) {
53  $this->augmentTestWithLogs( $test );
54  }
55 
56  private function augmentTestWithLogs( PHPUnit_Framework_Test $test ) {
57  if ( $this->spi ) {
58  $logs = $this->spi->getLogs();
59  $formatted = $this->formatLogs( $logs );
60  $test->_formattedMediaWikiLogs = $formatted;
61  }
62  }
63 
70  public function endTest( PHPUnit_Framework_Test $test, $time ) {
71  LoggerFactory::registerProvider( $this->originalSpi );
72  $this->originalSpi = null;
73  $this->spi = null;
74  }
75 
83  private function formatLogs( array $logs ) {
84  $message = [];
85  foreach ( $logs as $log ) {
86  if ( $log['channel'] === 'PHPUnitCommand' ) {
87  // Don't print the log of PHPUnit events while running PHPUnit,
88  // because PHPUnit is already printing those already.
89  continue;
90  }
91  $message[] = sprintf(
92  '[%s] [%s] %s %s',
93  $log['channel'],
94  $log['level'],
95  $log['message'],
96  json_encode( $log['context'] )
97  );
98  }
99  return implode( "\n", $message );
100  }
101 }
addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2146
MediaWiki Logger Spi
Definition: logger.txt:49
endTest(PHPUnit_Framework_Test $test, $time)
A test ended.
formatLogs(array $logs)
Get string formatted logs generated during the last test to execute.
see documentation in includes Linker php for Linker::makeImageLink & $time
Definition: hooks.txt:1780
addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
addWarning(PHPUnit_Framework_Test $test, PHPUnit\Framework\Warning $e, $time)
startTest(PHPUnit_Framework_Test $test)
A test started.
addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
Replaces the logging SPI on each test run.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
MediaWiki Logger LoggerFactory implements a PSR [0] compatible message logging system Named Psr Log LoggerInterface instances can be obtained from the MediaWiki Logger LoggerFactory::getInstance() static method. MediaWiki\Logger\LoggerFactory expects a class implementing the MediaWiki\Logger\Spi interface to act as a factory for new Psr\Log\LoggerInterface instances. The "Spi" in MediaWiki\Logger\Spi stands for "service provider interface". An SPI is an API intended to be implemented or extended by a third party. This software design pattern is intended to enable framework extension and replaceable components. It is specifically used in the MediaWiki\Logger\LoggerFactory service to allow alternate PSR-3 logging implementations to be easily integrated with MediaWiki. The service provider interface allows the backend logging library to be implemented in multiple ways. The $wgMWLoggerDefaultSpi global provides the classname of the default MediaWiki\Logger\Spi implementation to be loaded at runtime. This can either be the name of a class implementing the MediaWiki\Logger\Spi with a zero argument const ructor or a callable that will return an MediaWiki\Logger\Spi instance. Alternately the MediaWiki\Logger\LoggerFactory MediaWiki Logger LoggerFactory
Definition: logger.txt:5
addError(PHPUnit_Framework_Test $test, Exception $e, $time)
Wraps another spi to capture all logs generated.