File: PhpUnitXmlManagerTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (96 lines) | stat: -rw-r--r-- 3,288 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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, 'tests-list.xml' );
		$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" );
	}
}