Debian Codegraph @

tests/phpunit/unit/includes/composer/PhpUnitSplitter/PhpUnitXmlManagerTest.php

raw symbols SCIP
1 <?php
2
3 declare( strict_types = 1 );
4
5 namespace MediaWiki\Tests\Unit\composer\PhpUnitSplitter;
6
7 use MediaWiki\Composer\PhpUnitSplitter\PhpUnitXmlManager;
8 use MediaWiki\Composer\PhpUnitSplitter\TestListMissingException;
9 use PHPUnit\Framework\TestCase;
10
11 /**
12 * @license GPL-2.0-or-later
13 * @covers \MediaWiki\Composer\PhpUnitSplitter\PhpUnitXmlManager
14 */
15 class PhpUnitXmlManagerTest extends TestCase {
16
17 private string $testDir;
18 private PhpUnitXmlManager $manager;
19
20 public function setUp(): void {
21 parent::setUp();
22 $this->testDir = implode( DIRECTORY_SEPARATOR, [ sys_get_temp_dir(), uniqid( 'PhpUnitTest' ) ] );
23 mkdir( $this->testDir );
24 $this->manager = new PhpUnitXmlManager( $this->testDir, 'tests-list.xml' );
25 $this->setupTestFolder();
26 }
27
28 private static function getSourcePhpUnitDistXml(): string {
29 return __DIR__ . DIRECTORY_SEPARATOR . implode(
30 DIRECTORY_SEPARATOR, [ '..', '..', '..', '..', '..', '..', 'phpunit.xml.dist' ]
31 );
32 }
33
34 private function setupTestFolder() {
35 copy(
36 self::getSourcePhpUnitDistXml(),
37 $this->testDir . DIRECTORY_SEPARATOR . 'phpunit.xml.dist'
38 );
39 mkdir( $this->testDir . DIRECTORY_SEPARATOR . "tests" );
40 foreach ( glob( __DIR__ . DIRECTORY_SEPARATOR . "*Test.php" ) as $file ) {
41 copy(
42 $file,
43 implode( DIRECTORY_SEPARATOR, [ $this->testDir, "tests", basename( $file ) ] )
44 );
45 }
46 }
47
48 private function tearDownTestFolder() {
49 foreach ( [ 'phpunit.xml', 'phpunit.xml.dist', 'tests-list.xml' ] as $file ) {
50 $path = $this->testDir . DIRECTORY_SEPARATOR . $file;
51 if ( file_exists( $path ) ) {
52 unlink( $path );
53 }
54 }
55 $testsFolder = $this->testDir . DIRECTORY_SEPARATOR . "tests";
56 foreach ( glob( $testsFolder . DIRECTORY_SEPARATOR . "*.php" ) as $file ) {
57 unlink( $file );
58 }
59 rmdir( $testsFolder );
60 rmdir( $this->testDir );
61 }
62
63 private function copyTestListIntoPlace() {
64 copy(
65 __DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'tests-list.xml',
66 $this->testDir . DIRECTORY_SEPARATOR . 'tests-list.xml'
67 );
68 }
69
70 public function tearDown(): void {
71 parent::tearDown();
72 $this->tearDownTestFolder();
73 }
74
75 public function testIsPrepared() {
76 $this->copyTestListIntoPlace();
77 $this->assertFalse( $this->manager->isPhpUnitXmlPrepared(), "Expected no PHPUnit Xml to be present" );
78 $this->manager->createPhpUnitXml( 4 );
79 $this->assertTrue( $this->manager->isPhpUnitXmlPrepared(), "Expected PHPUnit Xml to have been prepared" );
80 }
81
82 public function testFailsIfNoListIsPresent() {
83 $this->assertFalse( $this->manager->isPhpUnitXmlPrepared(), "Expected no PHPUnit Xml to be present" );
84 $this->expectException( TestListMissingException::class );
85 $this->manager->createPhpUnitXml( 4 );
86 }
87
88 public function testPhpUnitXmlDistNotPrepared() {
89 $this->assertFalse( $this->manager->isPhpUnitXmlPrepared(), "Expected no PHPUnit Xml to be present" );
90 copy( self::getSourcePhpUnitDistXml(), implode( DIRECTORY_SEPARATOR, [ $this->testDir, "phpunit.xml" ] ) );
91 $this->copyTestListIntoPlace();
92 $this->manager->createPhpUnitXml( 4 );
93 copy( self::getSourcePhpUnitDistXml(), implode( DIRECTORY_SEPARATOR, [ $this->testDir, "phpunit.xml" ] ) );
94 $this->assertFalse( $this->manager->isPhpUnitXmlPrepared(), "Expected phpunit.dist.xml to be treated as unprepared" );
95 }
96 }