diff options
Diffstat (limited to 'tests/phpunit/unit/includes')
6 files changed, 275 insertions, 0 deletions
diff --git a/tests/phpunit/unit/includes/composer/PhpUnitSplitter/PhpUnitTestFileScannerTest.php b/tests/phpunit/unit/includes/composer/PhpUnitSplitter/PhpUnitTestFileScannerTest.php new file mode 100644 index 000000000000..b694f0a0b4d5 --- /dev/null +++ b/tests/phpunit/unit/includes/composer/PhpUnitSplitter/PhpUnitTestFileScannerTest.php @@ -0,0 +1,26 @@ +<?php + +declare( strict_types = 1 ); + +namespace MediaWiki\Tests\Unit\composer\PhpUnitSplitter; + +use MediaWiki\Composer\PhpUnitSplitter\PhpUnitTestFileScanner; +use PHPUnit\Framework\TestCase; + +/** + * @license GPL-2.0-or-later + * @covers \MediaWiki\Composer\PhpUnitSplitter\PhpUnitTestFileScanner + */ +class PhpUnitTestFileScannerTest extends TestCase { + + public function testScanForTestFiles() { + $scanner = new PhpUnitTestFileScanner( __DIR__ ); + $files = $scanner->scanForFiles(); + $expected = []; + foreach ( glob( __DIR__ . DIRECTORY_SEPARATOR . "*Test.php" ) as $testFile ) { + $expected[ basename( $testFile ) ] = [ $testFile ]; + } + $this->assertEquals( $expected, $files, "Expected PhpUnitSplitter test files to be found" ); + } + +} diff --git a/tests/phpunit/unit/includes/composer/PhpUnitSplitter/PhpUnitTestListProcessorTest.php b/tests/phpunit/unit/includes/composer/PhpUnitSplitter/PhpUnitTestListProcessorTest.php new file mode 100644 index 000000000000..04526c1b7e8a --- /dev/null +++ b/tests/phpunit/unit/includes/composer/PhpUnitSplitter/PhpUnitTestListProcessorTest.php @@ -0,0 +1,29 @@ +<?php + +declare( strict_types = 1 ); + +namespace MediaWiki\Tests\Unit\composer\PhpUnitSplitter; + +use MediaWiki\Composer\PhpUnitSplitter\PhpUnitTestListProcessor; +use PHPUnit\Framework\TestCase; + +/** + * @license GPL-2.0-or-later + * @covers \MediaWiki\Composer\PhpUnitSplitter\PhpUnitTestListProcessor + */ +class PhpUnitTestListProcessorTest extends TestCase { + + private const FIXTURE_FILE = __DIR__ . "/fixtures/tests-list.xml"; + + public function testGetTestClasses() { + $testList = new PhpUnitTestListProcessor( self::FIXTURE_FILE ); + $this->assertCount( + 5, $testList->getTestClasses(), "Expected classes to be loaded" + ); + $test5 = $testList->getTestClasses()[4]; + $this->assertEquals( + [ [ 'MediaWiki', 'Tests', 'Unit', 'composer', 'PhpUnitSplitter' ], 'TestSuiteBuilderTest' ], + [ $test5->getNamespace(), $test5->getClassName() ] + ); + } +} diff --git a/tests/phpunit/unit/includes/composer/PhpUnitSplitter/PhpUnitXmlManagerTest.php b/tests/phpunit/unit/includes/composer/PhpUnitSplitter/PhpUnitXmlManagerTest.php new file mode 100644 index 000000000000..af4c0d1a2d9d --- /dev/null +++ b/tests/phpunit/unit/includes/composer/PhpUnitSplitter/PhpUnitXmlManagerTest.php @@ -0,0 +1,96 @@ +<?php + +declare( strict_types = 1 ); + +namespace MediaWiki\Tests\Unit\composer\PhpUnitSplitter; + +use MediaWiki\Composer\PhpUnitSplitter\PhpUnitXmlManager; +use MediaWiki\Composer\PhpUnitSplitter\TestListMissingException; +use PHPUnit\Framework\TestCase; + +/** + * @license GPL-2.0-or-later + * @covers \MediaWiki\Composer\PhpUnitSplitter\PhpUnitXmlManager + */ +class PhpUnitXmlManagerTest extends TestCase { + + private string $testDir; + private PhpUnitXmlManager $manager; + + public function setUp(): void { + parent::setUp(); + $this->testDir = implode( DIRECTORY_SEPARATOR, [ sys_get_temp_dir(), uniqid( 'PhpUnitTest' ) ] ); + mkdir( $this->testDir ); + $this->manager = new PhpUnitXmlManager( $this->testDir ); + $this->setupTestFolder(); + } + + private static function getSourcePhpUnitDistXml(): string { + return __DIR__ . DIRECTORY_SEPARATOR . implode( + DIRECTORY_SEPARATOR, [ '..', '..', '..', '..', '..', '..', 'phpunit.xml.dist' ] + ); + } + + private function setupTestFolder() { + copy( + self::getSourcePhpUnitDistXml(), + $this->testDir . DIRECTORY_SEPARATOR . 'phpunit.xml.dist' + ); + mkdir( $this->testDir . DIRECTORY_SEPARATOR . "tests" ); + foreach ( glob( __DIR__ . DIRECTORY_SEPARATOR . "*Test.php" ) as $file ) { + copy( + $file, + implode( DIRECTORY_SEPARATOR, [ $this->testDir, "tests", basename( $file ) ] ) + ); + } + } + + private function tearDownTestFolder() { + foreach ( [ 'phpunit.xml', 'phpunit.xml.dist', 'tests-list.xml' ] as $file ) { + $path = $this->testDir . DIRECTORY_SEPARATOR . $file; + if ( file_exists( $path ) ) { + unlink( $path ); + } + } + $testsFolder = $this->testDir . DIRECTORY_SEPARATOR . "tests"; + foreach ( glob( $testsFolder . DIRECTORY_SEPARATOR . "*.php" ) as $file ) { + unlink( $file ); + } + rmdir( $testsFolder ); + rmdir( $this->testDir ); + } + + private function copyTestListIntoPlace() { + copy( + __DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'tests-list.xml', + $this->testDir . DIRECTORY_SEPARATOR . 'tests-list.xml' + ); + } + + public function tearDown(): void { + parent::tearDown(); + $this->tearDownTestFolder(); + } + + public function testIsPrepared() { + $this->copyTestListIntoPlace(); + $this->assertFalse( $this->manager->isPhpUnitXmlPrepared(), "Expected no PHPUnit Xml to be present" ); + $this->manager->createPhpUnitXml( 4 ); + $this->assertTrue( $this->manager->isPhpUnitXmlPrepared(), "Expected PHPUnit Xml to have been prepared" ); + } + + public function testFailsIfNoListIsPresent() { + $this->assertFalse( $this->manager->isPhpUnitXmlPrepared(), "Expected no PHPUnit Xml to be present" ); + $this->expectException( TestListMissingException::class ); + $this->manager->createPhpUnitXml( 4 ); + } + + public function testPhpUnitXmlDistNotPrepared() { + $this->assertFalse( $this->manager->isPhpUnitXmlPrepared(), "Expected no PHPUnit Xml to be present" ); + copy( self::getSourcePhpUnitDistXml(), implode( DIRECTORY_SEPARATOR, [ $this->testDir, "phpunit.xml" ] ) ); + $this->copyTestListIntoPlace(); + $this->manager->createPhpUnitXml( 4 ); + copy( self::getSourcePhpUnitDistXml(), implode( DIRECTORY_SEPARATOR, [ $this->testDir, "phpunit.xml" ] ) ); + $this->assertFalse( $this->manager->isPhpUnitXmlPrepared(), "Expected phpunit.dist.xml to be treated as unprepared" ); + } +} diff --git a/tests/phpunit/unit/includes/composer/PhpUnitSplitter/PhpUnitXmlTest.php b/tests/phpunit/unit/includes/composer/PhpUnitSplitter/PhpUnitXmlTest.php new file mode 100644 index 000000000000..6b30596e90bf --- /dev/null +++ b/tests/phpunit/unit/includes/composer/PhpUnitSplitter/PhpUnitXmlTest.php @@ -0,0 +1,52 @@ +<?php + +declare( strict_types = 1 ); + +namespace MediaWiki\Tests\Unit\composer\PhpUnitSplitter; + +use MediaWiki\Composer\PhpUnitSplitter\PhpUnitXml; +use PHPUnit\Framework\TestCase; + +/** + * @license GPL-2.0-or-later + * @covers \MediaWiki\Composer\PhpUnitSplitter\PhpUnitXml + */ +class PhpUnitXmlTest extends TestCase { + + private const BASIC_XML = '<?xml version="1.0" encoding="UTF-8"?> +<phpunit bootstrap="tests/phpunit/bootstrap.php"> +<testsuites> + <testsuite name="core:unit"> + <directory>tests/phpunit/unit</directory> + </testsuite> +</testsuites> +</phpunit>'; + + public function createFixtureFile( string $data ): string { + $filename = tempnam( sys_get_temp_dir(), "phpunit-test" ); + file_put_contents( $filename, $data ); + return $filename; + } + + public function testFixtureContainsNoSplitGroups() { + $phpUnitXmlFile = $this->createFixtureFile( self::BASIC_XML ); + $phpUnitXml = new PhpUnitXml( $phpUnitXmlFile ); + $this->assertFalse( $phpUnitXml->containsSplitGroups(), "No split groups expected in fixture" ); + unlink( $phpUnitXmlFile ); + } + + public function testAddSplitGroups() { + $phpUnitXmlFile = $this->createFixtureFile( self::BASIC_XML ); + $phpUnitXml = new PhpUnitXml( $phpUnitXmlFile ); + $phpUnitXml->addSplitGroups( [ + [ "file1.php", "file2.php" ], + [ "file3.php", "file4.php" ], + [ "file7.php", "file6.php" ], + [ "file9.php", "file8.php" ], + [ "file11.php", "file10.php" ], + [ "file13.php", "file12.php" ], + ] ); + $this->assertTrue( $phpUnitXml->containsSplitGroups(), "Expected groups to be added" ); + unlink( $phpUnitXmlFile ); + } +} diff --git a/tests/phpunit/unit/includes/composer/PhpUnitSplitter/TestSuiteBuilderTest.php b/tests/phpunit/unit/includes/composer/PhpUnitSplitter/TestSuiteBuilderTest.php new file mode 100644 index 000000000000..ce8a99504a6e --- /dev/null +++ b/tests/phpunit/unit/includes/composer/PhpUnitSplitter/TestSuiteBuilderTest.php @@ -0,0 +1,50 @@ +<?php + +declare( strict_types = 1 ); + +namespace MediaWiki\Tests\Unit\composer\PhpUnitSplitter; + +use MediaWiki\Composer\PhpUnitSplitter\TestDescriptor; +use MediaWiki\Composer\PhpUnitSplitter\TestSuiteBuilder; +use PHPUnit\Framework\TestCase; + +/** + * @license GPL-2.0-or-later + * @covers \MediaWiki\Composer\PhpUnitSplitter\TestSuiteBuilder + */ +class TestSuiteBuilderTest extends TestCase { + + public function testBuildSuites() { + $testList = [ + new TestDescriptor( "ATest", [ "MediaWiki" ], "MediaWiki/ATest.php" ), + new TestDescriptor( "BTest", [ "MediaWiki" ], "MediaWiki/BTest.php" ), + new TestDescriptor( "CTest", [ "MediaWiki" ], "MediaWiki/CTest.php" ), + new TestDescriptor( "DTest", [ "MediaWiki" ], "MediaWiki/DTest.php" ), + new TestDescriptor( "ETest", [ "MediaWiki" ], "MediaWiki/ETest.php" ), + ]; + $suites = ( new TestSuiteBuilder() )->buildSuites( $testList, 3 ); + $expected = [ + [ + "list" => [ + "MediaWiki/ATest.php", + "MediaWiki/DTest.php", + ], + "time" => 0 + ], + [ + "list" => [ + "MediaWiki/BTest.php", + "MediaWiki/ETest.php", + ], + "time" => 0 + ], + [ + "list" => [ + "MediaWiki/CTest.php", + ], + "time" => 0 + ] + ]; + $this->assertEquals( $expected, $suites, "Expected suites to be built correctly" ); + } +} diff --git a/tests/phpunit/unit/includes/composer/PhpUnitSplitter/fixtures/tests-list.xml b/tests/phpunit/unit/includes/composer/PhpUnitSplitter/fixtures/tests-list.xml new file mode 100644 index 000000000000..942fefe72e48 --- /dev/null +++ b/tests/phpunit/unit/includes/composer/PhpUnitSplitter/fixtures/tests-list.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="UTF-8"?> +<tests> + <testCaseClass name="MediaWiki\Tests\Unit\composer\PhpUnitSplitter\PhpUnitTestFileScannerTest"> + <testCaseMethod name="testSchemaChangesPassValidation" groups="default" dataSet=""patch-change_tag-rename-indexes.json""/> + </testCaseClass> + <testCaseClass name="MediaWiki\Tests\Unit\composer\PhpUnitSplitter\PhpUnitTestListProcessorTest"> + <testCaseMethod name="testPrefixes" groups="API,Database"/> + <testCaseMethod name="testValidCovers" groups="API,Database"/> + </testCaseClass> + <testCaseClass name="MediaWiki\Tests\Unit\composer\PhpUnitSplitter\PhpUnitXmlManagerTest"> + <testCaseMethod name="testConfigSchemaIsLoadable" groups="default"/> + <testCaseMethod name="testConfigSchemaDefaultsValidate" groups="default"/> + <testCaseMethod name="testCurrentSettingsValidate" groups="default"/> + <testCaseMethod name="testCurrentSettingsNotDeprecated" groups="default"/> + </testCaseClass> + <testCaseClass name="MediaWiki\Tests\Unit\composer\PhpUnitSplitter\PhpUnitXmlTest"> + <testCaseMethod name="testOnBeforePageDisplay" groups="Marius Hoch < hoo@online.de >,WikimediaBadges,__phpunit_covers_wikimediabadges\beforepagedisplayhookhandler"/> + </testCaseClass> + <testCaseClass name="MediaWiki\Tests\Unit\composer\PhpUnitSplitter\TestSuiteBuilderTest"> + <testCaseMethod name="testOnBeforePageDisplay" groups="Marius Hoch < hoo@online.de >,WikimediaBadges,__phpunit_covers_wikimediabadges\beforepagedisplayhookhandler"/> + </testCaseClass> +</tests> |