2019-06-17 21:15:25 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Testing
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
2019-06-26 02:33:14 +00:00
|
|
|
/**
|
|
|
|
|
* Base class for unit tests.
|
|
|
|
|
*
|
|
|
|
|
* Extend this class if you are testing classes which use dependency injection and do not access
|
|
|
|
|
* global functions, variables, services or a storage backend.
|
2019-08-18 18:19:05 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.34
|
2019-06-26 02:33:14 +00:00
|
|
|
*/
|
2019-06-17 21:15:25 +00:00
|
|
|
abstract class MediaWikiUnitTestCase extends TestCase {
|
|
|
|
|
use PHPUnit4And6Compat;
|
|
|
|
|
use MediaWikiCoversValidator;
|
2019-08-18 18:19:05 +00:00
|
|
|
use MediaWikiTestCaseTrait;
|
2019-06-30 13:23:53 +00:00
|
|
|
|
2019-07-08 13:25:31 +00:00
|
|
|
private $unitGlobals = [];
|
|
|
|
|
|
|
|
|
|
protected function setUp() {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
$reflection = new ReflectionClass( $this );
|
2019-07-17 17:52:18 +00:00
|
|
|
$dirSeparator = DIRECTORY_SEPARATOR;
|
2019-09-01 15:44:14 +00:00
|
|
|
if ( stripos( $reflection->getFilename(), "${dirSeparator}unit${dirSeparator}" ) === false ) {
|
2019-08-18 18:19:05 +00:00
|
|
|
$this->fail( 'This unit test needs to be in "tests/phpunit/unit"!' );
|
2019-07-08 13:25:31 +00:00
|
|
|
}
|
|
|
|
|
$this->unitGlobals = $GLOBALS;
|
|
|
|
|
unset( $GLOBALS );
|
|
|
|
|
$GLOBALS = [];
|
|
|
|
|
// Add back the minimal set of globals needed for unit tests to run for core +
|
|
|
|
|
// extensions/skins.
|
2019-07-10 17:20:00 +00:00
|
|
|
foreach ( $this->unitGlobals['wgPhpUnitBootstrapGlobals'] ?? [] as $key => $value ) {
|
|
|
|
|
$GLOBALS[ $key ] = $this->unitGlobals[ $key ];
|
2019-07-08 13:25:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function tearDown() {
|
|
|
|
|
$GLOBALS = $this->unitGlobals;
|
|
|
|
|
parent::tearDown();
|
|
|
|
|
}
|
2019-08-18 18:19:05 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a temporary hook handler which will be reset by tearDown.
|
|
|
|
|
* This replaces other handlers for the same hook.
|
|
|
|
|
* @param string $hookName Hook name
|
|
|
|
|
* @param mixed $handler Value suitable for a hook handler
|
|
|
|
|
* @since 1.34
|
|
|
|
|
*/
|
|
|
|
|
protected function setTemporaryHook( $hookName, $handler ) {
|
|
|
|
|
// This will be reset by tearDown() when it restores globals. We don't want to use
|
|
|
|
|
// Hooks::register()/clear() because they won't replace other handlers for the same hook,
|
|
|
|
|
// which doesn't match behavior of MediaWikiIntegrationTestCase.
|
|
|
|
|
global $wgHooks;
|
|
|
|
|
$wgHooks[$hookName] = [ $handler ];
|
|
|
|
|
}
|
2019-08-21 15:51:10 +00:00
|
|
|
|
|
|
|
|
protected function getMockMessage( $text, ...$params ) {
|
|
|
|
|
if ( isset( $params[0] ) && is_array( $params[0] ) ) {
|
|
|
|
|
$params = $params[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$msg = $this->getMockBuilder( Message::class )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->setMethods( [] )
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$msg->method( 'toString' )->willReturn( $text );
|
|
|
|
|
$msg->method( '__toString' )->willReturn( $text );
|
|
|
|
|
$msg->method( 'text' )->willReturn( $text );
|
|
|
|
|
$msg->method( 'parse' )->willReturn( $text );
|
|
|
|
|
$msg->method( 'plain' )->willReturn( $text );
|
|
|
|
|
$msg->method( 'parseAsBlock' )->willReturn( $text );
|
|
|
|
|
$msg->method( 'escaped' )->willReturn( $text );
|
|
|
|
|
|
|
|
|
|
$msg->method( 'title' )->willReturn( $msg );
|
|
|
|
|
$msg->method( 'inLanguage' )->willReturn( $msg );
|
|
|
|
|
$msg->method( 'inContentLanguage' )->willReturn( $msg );
|
|
|
|
|
$msg->method( 'useDatabase' )->willReturn( $msg );
|
|
|
|
|
$msg->method( 'setContext' )->willReturn( $msg );
|
|
|
|
|
|
|
|
|
|
$msg->method( 'exists' )->willReturn( true );
|
|
|
|
|
$msg->method( 'content' )->willReturn( new MessageContent( $msg ) );
|
|
|
|
|
|
|
|
|
|
return $msg;
|
|
|
|
|
}
|
2019-06-17 21:15:25 +00:00
|
|
|
}
|