MediaWiki  1.33.0
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 }
MediaWikiLoggerPHPUnitTestListener\addError
addError(PHPUnit_Framework_Test $test, Exception $e, $time)
Definition: MediaWikiLoggerPHPUnitTestListener.php:42
MediaWiki\Logger\LogCapturingSpi
Wraps another spi to capture all logs generated.
Definition: LogCapturingSpi.php:13
MediaWikiLoggerPHPUnitTestListener\endTest
endTest(PHPUnit_Framework_Test $test, $time)
A test ended.
Definition: MediaWikiLoggerPHPUnitTestListener.php:70
MediaWikiLoggerPHPUnitTestListener\addSkippedTest
addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
Definition: MediaWikiLoggerPHPUnitTestListener.php:38
MediaWikiLoggerPHPUnitTestListener\formatLogs
formatLogs(array $logs)
Get string formatted logs generated during the last test to execute.
Definition: MediaWikiLoggerPHPUnitTestListener.php:83
php
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
MediaWikiLoggerPHPUnitTestListener\augmentTestWithLogs
augmentTestWithLogs(PHPUnit_Framework_Test $test)
Definition: MediaWikiLoggerPHPUnitTestListener.php:56
MediaWikiLoggerPHPUnitTestListener\startTest
startTest(PHPUnit_Framework_Test $test)
A test started.
Definition: MediaWikiLoggerPHPUnitTestListener.php:23
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
MediaWikiLoggerPHPUnitTestListener\addFailure
addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
Definition: MediaWikiLoggerPHPUnitTestListener.php:50
MediaWikiLoggerPHPUnitTestListener\addRiskyTest
addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
Definition: MediaWikiLoggerPHPUnitTestListener.php:30
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2162
MediaWikiLoggerPHPUnitTestListener\addWarning
addWarning(PHPUnit_Framework_Test $test, PHPUnit\Framework\Warning $e, $time)
Definition: MediaWikiLoggerPHPUnitTestListener.php:46
MediaWikiLoggerPHPUnitTestListener\$spi
Spi null $spi
Definition: MediaWikiLoggerPHPUnitTestListener.php:16
as
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
MediaWikiLoggerPHPUnitTestListener\addIncompleteTest
addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
Definition: MediaWikiLoggerPHPUnitTestListener.php:34
LoggerFactory
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
$time
see documentation in includes Linker php for Linker::makeImageLink & $time
Definition: hooks.txt:1802
MediaWiki\Logger\Spi
Service provider interface for \Psr\Log\LoggerInterface implementation libraries.
Definition: Spi.php:36
Spi
MediaWiki Logger Spi
Definition: logger.txt:49
MediaWikiLoggerPHPUnitTestListener
Replaces the logging SPI on each test run.
Definition: MediaWikiLoggerPHPUnitTestListener.php:12
MediaWikiLoggerPHPUnitTestListener\$originalSpi
Spi null $originalSpi
Definition: MediaWikiLoggerPHPUnitTestListener.php:14