wiki.techinc.nl/tests/phpunit/MediaWikiUnitTestCase.php
Amir Sarabadani d23af35764 Unset all globals unneeded for unit tests, assert correct directory
* Unset globals to avoid tests that look like unit tests but actually rely on
  globals
* move some tests out of unit directory so that the test suite will pass.
* Assert that tests which extend MediaWikiUnitTestCase are in a directory with
  "/unit/" in its path name

Depends-On: I67b37b1bde94eaa3d4298d9bd98ac57995ce93b9
Depends-On: I90921679518ee95fe393f8b1bbd9134daf0ba032
Bug: T87781
Change-Id: I16691fc8ac063705ba0c2bc63b96c4534ca8660b
2019-07-09 14:09:29 -04:00

56 lines
1.8 KiB
PHP

<?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;
/**
* 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.
*/
abstract class MediaWikiUnitTestCase extends TestCase {
use PHPUnit4And6Compat;
use MediaWikiCoversValidator;
private $unitGlobals = [];
protected function setUp() {
parent::setUp();
$reflection = new ReflectionClass( $this );
if ( strpos( $reflection->getFilename(), '/unit/' ) === false ) {
$this->fail( 'This unit test needs to be in "tests/phpunit/unit" !' );
}
$this->unitGlobals = $GLOBALS;
unset( $GLOBALS );
$GLOBALS = [];
// Add back the minimal set of globals needed for unit tests to run for core +
// extensions/skins.
foreach ( [ 'wgAutoloadClasses', 'wgAutoloadLocalClasses', 'IP' ] as $requiredGlobal ) {
$GLOBALS[$requiredGlobal] = $this->unitGlobals[ $requiredGlobal ];
}
}
protected function tearDown() {
$GLOBALS = $this->unitGlobals;
parent::tearDown();
}
}