From 006d5c9ad530ab27a45eefbe14dc87e5687c6b03 Mon Sep 17 00:00:00 2001 From: Filippo Tessarotto Date: Wed, 5 Aug 2020 07:38:20 +0200 Subject: [PATCH] Use native PHPUnit Configuration class (#496) * Use native PHPUnit Configuration class * Fix composer require checker validation --- .../lint-xml-configuration.sh | 2 +- composer.json | 15 +- psalm.xml.dist | 5 + src/Console/Testers/PHPUnit.php | 53 ++-- src/Runners/PHPUnit/BaseRunner.php | 12 +- src/Runners/PHPUnit/Configuration.php | 253 ------------------ src/Runners/PHPUnit/ExecutableTest.php | 5 + src/Runners/PHPUnit/Options.php | 12 +- src/Runners/PHPUnit/ResultPrinter.php | 2 +- src/Runners/PHPUnit/SuiteLoader.php | 105 ++++++-- src/Runners/PHPUnit/TestMethod.php | 2 + src/Runners/PHPUnit/Worker/WrapperWorker.php | 2 +- test/Unit/Console/Testers/PHPUnitTest.php | 9 +- .../Runners/PHPUnit/ConfigurationTest.php | 136 ---------- test/Unit/Runners/PHPUnit/OptionsTest.php | 10 +- test/Unit/Runners/PHPUnit/RunnerTest.php | 4 +- test/Unit/Runners/PHPUnit/SuiteLoaderTest.php | 42 +-- ...-excluded-including-excluding-same-dir.xml | 18 -- .../phpunit-files-dirs-mix-nested.xml | 24 -- 19 files changed, 162 insertions(+), 549 deletions(-) delete mode 100644 src/Runners/PHPUnit/Configuration.php delete mode 100644 test/Unit/Runners/PHPUnit/ConfigurationTest.php delete mode 100644 test/fixtures/phpunit-excluded-including-excluding-same-dir.xml delete mode 100644 test/fixtures/phpunit-files-dirs-mix-nested.xml diff --git a/.github/lint-xml-configuration/lint-xml-configuration.sh b/.github/lint-xml-configuration/lint-xml-configuration.sh index f96380aa..4a31c45a 100644 --- a/.github/lint-xml-configuration/lint-xml-configuration.sh +++ b/.github/lint-xml-configuration/lint-xml-configuration.sh @@ -5,4 +5,4 @@ set -ex xmllint --noout --schema vendor/phpunit/phpunit/phpunit.xsd phpunit.xml.dist xmllint --noout --schema vendor/squizlabs/php_codesniffer/phpcs.xsd phpcs.xml.dist xmllint --noout --schema vendor/vimeo/psalm/config.xsd psalm.xml.dist -find test/ -name "phpunit*.xml*" -not -name "phpunit-files-dirs-mix-nested.xml" -print0 | xargs -0 xmllint --noout --schema vendor/phpunit/phpunit/phpunit.xsd \ No newline at end of file +find test/ -name "phpunit*.xml*" -print0 | xargs -0 xmllint --noout --schema vendor/phpunit/phpunit/phpunit.xsd \ No newline at end of file diff --git a/composer.json b/composer.json index d46fbbd1..5f13b48a 100644 --- a/composer.json +++ b/composer.json @@ -27,22 +27,23 @@ "ext-simplexml": "*", "brianium/habitat": "^1.0", "phpunit/php-code-coverage": "^8.0", + "phpunit/php-file-iterator": "^3.0", "phpunit/php-timer": "^5.0", "phpunit/phpunit": "^9.2", - "symfony/console": "^4.4 || ^5.0", - "symfony/process": "^4.4 || ^5.0" + "symfony/console": "^4.4 || ^5.1", + "symfony/process": "^4.4 || ^5.1" }, "require-dev": { "doctrine/coding-standard": "^8.1", "ekino/phpstan-banned-code": "^0.3.1", - "ergebnis/phpstan-rules": "^0.15.0", - "phpstan/phpstan": "^0.12.33", + "ergebnis/phpstan-rules": "^0.15.1", + "phpstan/phpstan": "^0.12.35", "phpstan/phpstan-deprecation-rules": "^0.12.5", - "phpstan/phpstan-phpunit": "^0.12.12", + "phpstan/phpstan-phpunit": "^0.12.15", "phpstan/phpstan-strict-rules": "^0.12.4", - "squizlabs/php_codesniffer": "^3.5", + "squizlabs/php_codesniffer": "^3.5.5", "thecodingmachine/phpstan-strict-rules": "^0.12.0", - "vimeo/psalm": "^3.12" + "vimeo/psalm": "^3.13.1" }, "autoload": { "psr-4": { diff --git a/psalm.xml.dist b/psalm.xml.dist index 342c860b..89cb7ff9 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -13,4 +13,9 @@ + + + + + diff --git a/src/Console/Testers/PHPUnit.php b/src/Console/Testers/PHPUnit.php index f091c19d..d4efeebb 100644 --- a/src/Console/Testers/PHPUnit.php +++ b/src/Console/Testers/PHPUnit.php @@ -5,11 +5,12 @@ namespace ParaTest\Console\Testers; use InvalidArgumentException; -use ParaTest\Runners\PHPUnit\Configuration; use ParaTest\Runners\PHPUnit\Options; use ParaTest\Runners\PHPUnit\Runner; use ParaTest\Runners\PHPUnit\RunnerInterface; use ParaTest\Util\Str; +use PHPUnit\TextUI\Configuration\Configuration; +use PHPUnit\TextUI\Configuration\Registry; use RuntimeException; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\ArrayInput; @@ -27,12 +28,11 @@ use function getcwd; use function is_string; use function is_subclass_of; +use function realpath; use function sprintf; use function sys_get_temp_dir; use function tempnam; -use const DIRECTORY_SEPARATOR; - /** * Creates the interface for PHPUnit testing */ @@ -111,16 +111,7 @@ public function execute(InputInterface $input, OutputInterface $output): int $bareOptions = $this->getRunnerOptions($input); $this->requireBootstrap($this->getBootstrapFile($input, $bareOptions)); - $options = new Options($bareOptions); - if ( - isset($options->filtered['configuration']) && - ! file_exists($path = $options->filtered['configuration']->getPath()) - ) { - $output->writeln(sprintf('Could not read "%s".', $path)); - - return 1; - } - + $options = new Options($bareOptions); $runnerClass = $this->getRunnerClass($input); $runner = new $runnerClass($options, $output); @@ -147,27 +138,22 @@ private function hasPath(InputInterface $input): bool */ private function hasConfig(InputInterface $input): bool { - return $this->getConfig($input) !== false; + return $this->getConfig($input) !== null; } - /** - * @return Configuration|bool - */ - private function getConfig(InputInterface $input) + private function getConfig(InputInterface $input): ?Configuration { - $cwd = getcwd() . DIRECTORY_SEPARATOR; - - if ($input->getOption('configuration')) { - $configFilename = $input->getOption('configuration'); - } elseif (file_exists($cwd . 'phpunit.xml.dist')) { - $configFilename = $cwd . 'phpunit.xml.dist'; - } elseif (file_exists($cwd . 'phpunit.xml')) { - $configFilename = $cwd . 'phpunit.xml'; + if (is_string($path = $input->getOption('configuration')) && file_exists($path)) { + $configFilename = $path; + } elseif (file_exists($path = 'phpunit.xml')) { + $configFilename = $path; + } elseif (file_exists($path = 'phpunit.xml.dist')) { + $configFilename = $path; } else { - return false; + return null; } - return new Configuration($configFilename); + return Registry::getInstance()->get(realpath($configFilename)); } /** @@ -272,14 +258,17 @@ private function getBootstrapFile(InputInterface $input, array $options): string return $options['bootstrap']; } - if (! $this->hasConfig($input)) { + $config = $this->getConfig($input); + if ($config === null) { return ''; } - $config = $this->getConfig($input); - $bootstrap = $config->getBootstrap(); + $phpunitConfig = $config->phpunit(); + if (! $phpunitConfig->hasBootstrap()) { + return ''; + } - return $bootstrap !== '' ? $config->getConfigDir() . $bootstrap : ''; + return $phpunitConfig->bootstrap(); } /** diff --git a/src/Runners/PHPUnit/BaseRunner.php b/src/Runners/PHPUnit/BaseRunner.php index a32a5ed3..0b19934f 100644 --- a/src/Runners/PHPUnit/BaseRunner.php +++ b/src/Runners/PHPUnit/BaseRunner.php @@ -174,17 +174,17 @@ private function overrideEnvironmentVariables(): void return; } - $variables = $this->options->filtered['configuration']->getEnvironmentVariables(); + $variables = $this->options->filtered['configuration']->php()->envVariables(); - foreach ($variables as $key => $value) { - $localEnvValue = getenv($key, true); + foreach ($variables as $variable) { + $localEnvValue = getenv($variable->name(), true); if ($localEnvValue === false) { - $localEnvValue = $value; + $localEnvValue = $variable->value(); } - putenv(sprintf('%s=%s', $key, $localEnvValue)); + putenv(sprintf('%s=%s', $variable->name(), $localEnvValue)); - $_ENV[$key] = $localEnvValue; + $_ENV[$variable->name()] = $localEnvValue; } } diff --git a/src/Runners/PHPUnit/Configuration.php b/src/Runners/PHPUnit/Configuration.php deleted file mode 100644 index a4932de8..00000000 --- a/src/Runners/PHPUnit/Configuration.php +++ /dev/null @@ -1,253 +0,0 @@ -path = $path; - if (! file_exists($path)) { - return; - } - - $this->xml = simplexml_load_string(file_get_contents($path)); - } - - /** - * Converting the configuration to a string - * returns the configuration path. - */ - public function __toString(): string - { - return $this->path; - } - - /** - * Get the bootstrap PHPUnit configuration attribute. - * - * @return string The bootstrap attribute or empty string if not set - */ - public function getBootstrap(): string - { - if ($this->xml) { - return (string) $this->xml->attributes()->bootstrap; - } - - return ''; - } - - /** - * Returns the path to the phpunit configuration - * file. - */ - public function getPath(): string - { - return $this->path; - } - - /** - * Return the contents of the nodes - * contained in a PHPUnit configuration. - * - * @return SuitePath[][]|null - */ - public function getSuites(): ?array - { - if (! $this->xml) { - return null; - } - - $suites = []; - $nodes = $this->xml->xpath('//testsuites/testsuite'); - - foreach ($nodes as $node) { - $suites = array_merge_recursive($suites, $this->getSuiteByName((string) $node['name'])); - } - - return $suites; - } - - public function hasSuites(): bool - { - $suitesName = $this->getSuitesName(); - - return $suitesName !== null && count($suitesName) > 0; - } - - /** - * @return string[]|null - */ - public function getSuitesName(): ?array - { - if (! $this->xml) { - return null; - } - - $nodes = $this->xml->xpath('//testsuites/testsuite'); - $names = []; - foreach ($nodes as $node) { - $names[] = (string) $node['name']; - } - - return $names; - } - - /** - * Return the contents of the nodes - * contained in a PHPUnit configuration. - * - * @return SuitePath[][]|null - */ - public function getSuiteByName(string $suiteName): ?array - { - $nodes = $this->xml->xpath(sprintf('//testsuite[@name="%s"]', $suiteName)); - - $suites = []; - $excludedPaths = []; - foreach ($nodes as $node) { - foreach ($this->availableNodes as $nodeName) { - foreach ($node->{$nodeName} as $nodeContent) { - switch ($nodeName) { - case 'exclude': - foreach ($this->getSuitePaths((string) $nodeContent) as $excludedPath) { - $excludedPaths[$excludedPath] = $excludedPath; - } - - break; - case 'testsuite': - $suites = array_merge_recursive($suites, $this->getSuiteByName((string) $nodeContent)); - break; - case 'directory': - // Replicate behaviour of PHPUnit - // if a directory is included and excluded at the same time, then it is considered included - foreach ($this->getSuitePaths((string) $nodeContent) as $dir) { - if (! array_key_exists($dir, $excludedPaths)) { - continue; - } - - unset($excludedPaths[$dir]); - } - - // no break on purpose - default: - foreach ($this->getSuitePaths((string) $nodeContent) as $path) { - $suites[(string) $node['name']][] = new SuitePath( - $path, - $excludedPaths, - (string) $nodeContent->attributes()->suffix - ); - } - - break; - } - } - } - } - - return $suites; - } - - /** - * Return the path of the directory - * that contains the phpunit configuration. - */ - public function getConfigDir(): string - { - return dirname($this->path) . DIRECTORY_SEPARATOR; - } - - /** - * Returns a suite paths relative to the config file. - * - * @return array|string[] - */ - public function getSuitePaths(string $path): array - { - $real = realpath($this->getConfigDir() . $path); - - if ($real !== false) { - return [$real]; - } - - if ($this->isGlobRequired($path)) { - $paths = []; - foreach (glob($this->getConfigDir() . $path, GLOB_ONLYDIR) as $globPath) { - if (($globPath = realpath($globPath)) === false) { - continue; - } - - $paths[] = $globPath; - } - - return $paths; - } - - throw new RuntimeException("Suite path $path could not be found"); - } - - /** - * Get override environment variables from phpunit config file. - * - * @return array - */ - public function getEnvironmentVariables(): array - { - if (! isset($this->xml->php->env)) { - return []; - } - - $variables = []; - - foreach ($this->xml->php->env as $env) { - $variables[(string) $env['name']] = (string) $env['value']; - } - - return $variables; - } - - /** - * Returns true if path needs globbing (like a /path/*-to/string). - */ - public function isGlobRequired(string $path): bool - { - return strpos($path, '*') !== false; - } -} diff --git a/src/Runners/PHPUnit/ExecutableTest.php b/src/Runners/PHPUnit/ExecutableTest.php index a9b310f5..ea2abb27 100644 --- a/src/Runners/PHPUnit/ExecutableTest.php +++ b/src/Runners/PHPUnit/ExecutableTest.php @@ -4,6 +4,7 @@ namespace ParaTest\Runners\PHPUnit; +use PHPUnit\TextUI\Configuration\Configuration; use RuntimeException; use Symfony\Component\Process\PhpExecutableFinder; use Symfony\Component\Process\Process; @@ -239,6 +240,10 @@ final public function commandArguments(string $binary, array $options = [], ?arr continue; } + if ($value instanceof Configuration) { + $value = $value->filename(); + } + $arguments[] = $value; } diff --git a/src/Runners/PHPUnit/Options.php b/src/Runners/PHPUnit/Options.php index 645083d6..8e237143 100644 --- a/src/Runners/PHPUnit/Options.php +++ b/src/Runners/PHPUnit/Options.php @@ -4,6 +4,8 @@ namespace ParaTest\Runners\PHPUnit; +use PHPUnit\TextUI\Configuration\Configuration; +use PHPUnit\TextUI\Configuration\Registry; use RuntimeException; use Symfony\Component\Process\Process; @@ -47,7 +49,7 @@ * @property-read string $runner * @property-read bool $noTestTokens * @property-read bool $colors - * @property-read string|string[] $testsuite + * @property-read string[] $testsuite * @property-read int|null $maxBatchSize * @property-read string $filter * @property-read string[] $groups @@ -119,7 +121,7 @@ final class Options /** * Filters which tests to run. * - * @var string|string[] + * @var string[] */ private $testsuite; @@ -258,7 +260,7 @@ public function __isset(string $var): bool * Returns a collection of ParaTest's default * option values. * - * @return array + * @return array */ private static function defaults(): array { @@ -271,7 +273,7 @@ private static function defaults(): array 'runner' => 'Runner', 'no-test-tokens' => false, 'colors' => false, - 'testsuite' => '', + 'testsuite' => [], 'max-batch-size' => 0, 'filter' => null, 'parallel-suite' => false, @@ -348,7 +350,7 @@ private function filterOptions(array $options): array 'coverage-test-limit' => $this->coverageTestLimit, ]); if (($configuration = $this->getConfigurationPath($filtered)) !== null) { - $filtered['configuration'] = new Configuration($configuration); + $filtered['configuration'] = Registry::getInstance()->get($configuration); } return $filtered; diff --git a/src/Runners/PHPUnit/ResultPrinter.php b/src/Runners/PHPUnit/ResultPrinter.php index d73df5e8..78c0ddd6 100644 --- a/src/Runners/PHPUnit/ResultPrinter.php +++ b/src/Runners/PHPUnit/ResultPrinter.php @@ -141,7 +141,7 @@ public function start(Options $options): void if (isset($options->filtered['configuration'])) { $this->output->write(sprintf( "Configuration read from %s\n\n", - $options->filtered['configuration']->getPath() + $options->filtered['configuration']->filename() )); } diff --git a/src/Runners/PHPUnit/SuiteLoader.php b/src/Runners/PHPUnit/SuiteLoader.php index 151db819..3b3a8a12 100644 --- a/src/Runners/PHPUnit/SuiteLoader.php +++ b/src/Runners/PHPUnit/SuiteLoader.php @@ -8,20 +8,28 @@ use ParaTest\Parser\ParsedClass; use ParaTest\Parser\ParsedFunction; use ParaTest\Parser\Parser; +use PHPUnit\TextUI\Configuration\Configuration; +use PHPUnit\TextUI\Configuration\TestSuite; use ReflectionClass; use RuntimeException; +use SebastianBergmann\FileIterator\Facade; use function array_intersect; +use function array_map; use function array_merge; use function array_unique; use function assert; use function count; +use function in_array; use function is_array; use function is_int; use function preg_match; use function preg_match_all; use function sprintf; use function substr; +use function version_compare; + +use const PHP_VERSION; final class SuiteLoader { @@ -56,7 +64,11 @@ public function __construct(?Options $options = null) { $this->options = $options; - $this->configuration = $this->options->filtered['configuration'] ?? new Configuration(''); + if (! isset($this->options->filtered['configuration'])) { + return; + } + + $this->configuration = $this->options->filtered['configuration']; } /** @@ -104,27 +116,39 @@ public function load(string $path = ''): void } elseif ( isset($this->options->parallelSuite) && $this->options->parallelSuite + && $this->configuration !== null + && ! $this->configuration->testSuite()->isEmpty() + ) { + $this->suitesName = array_map(static function (TestSuite $testSuite): string { + return $testSuite->name(); + }, $this->configuration->testSuite()->asArray()); + } elseif ( + $this->configuration !== null + && ! $this->configuration->testSuite()->isEmpty() ) { - $this->suitesName = $this->configuration->getSuitesName(); - } elseif ($this->configuration->hasSuites()) { - if (is_array($this->options->testsuite) && count($this->options->testsuite) > 0) { - $suites = []; - foreach ($this->options->testsuite as $testsuite) { - $suites = array_merge($suites, $this->configuration->getSuiteByName($testsuite)); + $testSuiteCollection = $this->configuration->testSuite()->asArray(); + if (count($this->options->testsuite) > 0) { + $suitesName = array_map(static function (TestSuite $testSuite): string { + return $testSuite->name(); + }, $testSuiteCollection); + foreach ($this->options->testsuite as $testSuiteName) { + if (! in_array($testSuiteName, $suitesName, true)) { + throw new RuntimeException("Suite path $testSuiteName could not be found"); + } } - } else { - $suites = $this->configuration->getSuites(); - } - foreach ($suites as $suite) { - foreach ($suite as $suitePath) { - $testFileLoader = new TestFileLoader($this->options); - $this->files = array_merge( - $this->files, - $testFileLoader->loadSuitePath($suitePath) - ); + foreach ($testSuiteCollection as $index => $testSuite) { + if (in_array($testSuite->name(), $this->options->testsuite, true)) { + continue; + } + + unset($testSuiteCollection[$index]); } } + + foreach ($testSuiteCollection as $testSuite) { + $this->loadFilesFromTestSuite($testSuite); + } } if (count($this->files) === 0 && ! is_array($this->suitesName)) { @@ -144,7 +168,7 @@ private function initSuites(): void { if (is_array($this->suitesName)) { foreach ($this->suitesName as $suiteName) { - $this->loadedSuites[$suiteName] = $this->createFullSuite($suiteName, $this->configuration->getPath()); + $this->loadedSuites[$suiteName] = $this->createFullSuite($suiteName, $this->configuration->filename()); } } else { foreach ($this->files as $path) { @@ -404,4 +428,49 @@ private function createFullSuite(string $suiteName, string $configPath): FullSui { return new FullSuite($suiteName, $configPath); } + + /** + * @see \PHPUnit\TextUI\Configuration\TestSuiteMapper::map + */ + private function loadFilesFromTestSuite(TestSuite $testSuiteCollection): void + { + foreach ($testSuiteCollection->directories() as $directory) { + if ( + ! version_compare( + PHP_VERSION, + $directory->phpVersion(), + $directory->phpVersionOperator()->asString() + ) + ) { + continue; + } + + $exclude = []; + + foreach ($testSuiteCollection->exclude()->asArray() as $file) { + $exclude[] = $file->path(); + } + + $this->files = array_merge($this->files, (new Facade())->getFilesAsArray( + $directory->path(), + $directory->suffix(), + $directory->prefix(), + $exclude + )); + } + + foreach ($testSuiteCollection->files() as $file) { + if ( + ! version_compare( + PHP_VERSION, + $file->phpVersion(), + $file->phpVersionOperator()->asString() + ) + ) { + continue; + } + + $this->files[] = $file->path(); + } + } } diff --git a/src/Runners/PHPUnit/TestMethod.php b/src/Runners/PHPUnit/TestMethod.php index 61e51f5e..8003ac6a 100644 --- a/src/Runners/PHPUnit/TestMethod.php +++ b/src/Runners/PHPUnit/TestMethod.php @@ -4,6 +4,8 @@ namespace ParaTest\Runners\PHPUnit; +use PHPUnit\TextUI\Configuration\Configuration; + use function array_reduce; use function count; use function implode; diff --git a/src/Runners/PHPUnit/Worker/WrapperWorker.php b/src/Runners/PHPUnit/Worker/WrapperWorker.php index ea5f8bda..2ca5bef5 100644 --- a/src/Runners/PHPUnit/Worker/WrapperWorker.php +++ b/src/Runners/PHPUnit/Worker/WrapperWorker.php @@ -4,10 +4,10 @@ namespace ParaTest\Runners\PHPUnit\Worker; -use ParaTest\Runners\PHPUnit\Configuration; use ParaTest\Runners\PHPUnit\ExecutableTest; use ParaTest\Runners\PHPUnit\Options; use ParaTest\Runners\PHPUnit\ResultPrinter; +use PHPUnit\TextUI\Configuration\Configuration; use RuntimeException; use function fgets; diff --git a/test/Unit/Console/Testers/PHPUnitTest.php b/test/Unit/Console/Testers/PHPUnitTest.php index 457bad50..800e91ef 100644 --- a/test/Unit/Console/Testers/PHPUnitTest.php +++ b/test/Unit/Console/Testers/PHPUnitTest.php @@ -9,6 +9,7 @@ use ParaTest\Console\Testers\PHPUnit; use ParaTest\Runners\PHPUnit\EmptyRunnerStub; use ParaTest\Tests\TestBase; +use PHPUnit\Framework\Exception; use RuntimeException; use Symfony\Component\Console\Application; use Symfony\Component\Console\Command\HelpCommand; @@ -17,6 +18,7 @@ use Symfony\Component\Console\Input\InputDefinition; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\BufferedOutput; +use Symfony\Component\Console\Output\NullOutput; use function chdir; use function getcwd; @@ -127,11 +129,12 @@ public function testMessagePrintedWhenInvalidConfigFileSupplied(): void $command = new ParaTestCommand($tester); $input = new ArgvInput([], $command->getDefinition()); $input->setOption('configuration', 'nope.xml'); - $output = new BufferedOutput(); + $output = new NullOutput(); - $tester->execute($input, $output); + static::expectException(Exception::class); + static::expectDeprecationMessage('Could not read "nope.xml"'); - static::assertStringContainsString('Could not read "nope.xml"', $output->fetch()); + $tester->execute($input, $output); } public function testDisplayHelpWithoutConfigNorPath(): void diff --git a/test/Unit/Runners/PHPUnit/ConfigurationTest.php b/test/Unit/Runners/PHPUnit/ConfigurationTest.php deleted file mode 100644 index a8709db7..00000000 --- a/test/Unit/Runners/PHPUnit/ConfigurationTest.php +++ /dev/null @@ -1,136 +0,0 @@ -path = realpath(PARATEST_ROOT . '/phpunit.xml.dist'); - $this->config = new Configuration($this->path); - } - - public function testToStringReturnsPath(): void - { - static::assertEquals($this->path, (string) $this->config); - } - - /** - * @return SuitePath[][] - */ - public function testGetSuitesShouldReturnCorrectNumberOfSuites(): array - { - $suites = $this->config->getSuites(); - static::assertCount(2, $suites); - - return $suites; - } - - public function testHasSuites(): void - { - $actual = $this->config->hasSuites(); - static::assertTrue($actual); - } - - /** - * @param SuitePath[][] $suites - * - * @return SuitePath[][] - * - * @depends testGetSuitesShouldReturnCorrectNumberOfSuites - */ - public function testSuitesContainSuiteNameAtKey(array $suites): array - { - static::assertArrayHasKey('ParaTest Unit Tests', $suites); - static::assertArrayHasKey('ParaTest Functional Tests', $suites); - - return $suites; - } - - /** - * @param SuitePath[][] $suites - * - * @depends testSuitesContainSuiteNameAtKey - */ - public function testSuitesContainPathAsValue(array $suites): void - { - $basePath = getcwd() . DS; - $unitSuite = $suites['ParaTest Unit Tests']; - static::assertCount(1, $unitSuite); - $unitSuitePath = $unitSuite[0]; - static::assertEquals($basePath . 'test' . DS . 'Unit', $unitSuitePath->getPath()); - $functionalSuite = $suites['ParaTest Functional Tests']; - static::assertCount(1, $functionalSuite); - $functionalSuitePath = $functionalSuite[0]; - static::assertEquals($basePath . 'test' . DS . 'Functional', $functionalSuitePath->getPath()); - } - - public function testGetEnvironmentVariables(): void - { - static::assertCount(4, $this->config->getEnvironmentVariables()); - static::assertArrayHasKey('APP_ENV', $this->config->getEnvironmentVariables()); - static::assertArrayHasKey('CACHE_DRIVER', $this->config->getEnvironmentVariables()); - static::assertArrayHasKey('DB_CONNECTION', $this->config->getEnvironmentVariables()); - static::assertArrayHasKey('DB_DATABASE', $this->config->getEnvironmentVariables()); - - $config = new Configuration(realpath(__DIR__ . '/phpunit-ConfigurationTest.xml')); - static::assertCount(0, $config->getEnvironmentVariables()); - } - - public function testGlobbingSupport(): void - { - $basePath = getcwd() . DS; - $configuration = new Configuration($this->fixture('phpunit-globbing.xml')); - /** @var SuitePath[][] $suites */ - $suites = $configuration->getSuites(); - static::assertEquals( - $basePath . 'test' . DS . 'fixtures' . DS . 'globbing-support-tests' . DS . 'some-dir', - $suites['ParaTest Fixtures'][0]->getPath() - ); - static::assertEquals( - $basePath . 'test' . DS . 'fixtures' . DS . 'globbing-support-tests' . DS . 'some-dir2', - $suites['ParaTest Fixtures'][1]->getPath() - ); - } - - public function testLoadConfigEvenIfLibXmlEntityLoaderIsDisabled(): void - { - $before = libxml_disable_entity_loader(); - - try { - $this->config = new Configuration($this->path); - } finally { - libxml_disable_entity_loader($before); - } - - self::assertSame($this->path, $this->config->getPath()); - } - - public function testLoadedEnvironmentVariablesWillNotBeOverwritten(): void - { - putenv('DB_CONNECTION=mysql'); - putenv('DB_DATABASE=localhost'); - - $config = new Configuration(realpath(__DIR__ . '/phpunit-ConfigurationTest.xml')); - - static::assertSame('mysql', getenv('DB_CONNECTION')); - static::assertSame('localhost', getenv('DB_DATABASE')); - } -} diff --git a/test/Unit/Runners/PHPUnit/OptionsTest.php b/test/Unit/Runners/PHPUnit/OptionsTest.php index 896d548b..f6727b4a 100644 --- a/test/Unit/Runners/PHPUnit/OptionsTest.php +++ b/test/Unit/Runners/PHPUnit/OptionsTest.php @@ -118,14 +118,6 @@ public function testConfigurationShouldReturnSpecifiedConfigurationIfFileExists( $this->assertConfigurationFileFiltered('phpunit-myconfig.xml', getcwd(), 'phpunit-myconfig.xml'); } - public function testConfigurationShouldBeSetEvenIfFileDoesNotExist(): void - { - $this->unfiltered['path'] = getcwd(); - $this->unfiltered['configuration'] = '/path/to/config'; - $options = new Options($this->unfiltered); - static::assertEquals('/path/to/config', $options->filtered['configuration']->getPath()); - } - public function testConfigurationKeyIsNotPresentIfNoConfigGiven(): void { $this->unfiltered['path'] = getcwd(); @@ -181,7 +173,7 @@ private function assertConfigurationFileFiltered( $options = new Options($this->unfiltered); static::assertEquals( __DIR__ . DS . 'generated-configs' . DS . $configFileName, - $options->filtered['configuration']->getPath() + $options->filtered['configuration']->filename() ); } } diff --git a/test/Unit/Runners/PHPUnit/RunnerTest.php b/test/Unit/Runners/PHPUnit/RunnerTest.php index 398b35dd..0f4a9ae4 100644 --- a/test/Unit/Runners/PHPUnit/RunnerTest.php +++ b/test/Unit/Runners/PHPUnit/RunnerTest.php @@ -5,11 +5,11 @@ namespace ParaTest\Tests\Unit\Runners\PHPUnit; use ParaTest\Logging\LogInterpreter; -use ParaTest\Runners\PHPUnit\Configuration; use ParaTest\Runners\PHPUnit\Options; use ParaTest\Runners\PHPUnit\ResultPrinter; use ParaTest\Runners\PHPUnit\Runner; use ParaTest\Tests\TestBase; +use PHPUnit\TextUI\Configuration\Loader; use Symfony\Component\Console\Output\BufferedOutput; use function getcwd; @@ -41,7 +41,7 @@ public function testConstructor(): void static::assertEquals(-1, $this->getObjectValue($runner, 'exitcode')); static::assertTrue($options->functional); //filter out processes and path and phpunit - $config = new Configuration(getcwd() . DS . 'phpunit.xml.dist'); + $config = (new Loader())->load(getcwd() . DS . 'phpunit.xml.dist'); static::assertEquals(['bootstrap' => 'hello', 'configuration' => $config], $options->filtered); static::assertInstanceOf(LogInterpreter::class, $this->getObjectValue($runner, 'interpreter')); static::assertInstanceOf(ResultPrinter::class, $this->getObjectValue($runner, 'printer')); diff --git a/test/Unit/Runners/PHPUnit/SuiteLoaderTest.php b/test/Unit/Runners/PHPUnit/SuiteLoaderTest.php index be847e3e..541ba5af 100644 --- a/test/Unit/Runners/PHPUnit/SuiteLoaderTest.php +++ b/test/Unit/Runners/PHPUnit/SuiteLoaderTest.php @@ -16,6 +16,7 @@ use function array_shift; use function count; use function strstr; +use function uniqid; final class SuiteLoaderTest extends TestBase { @@ -90,20 +91,6 @@ public function testLoadTestsuiteFilesFromDirFromConfigWhileRespectingExcludeTag static::assertCount($expected, $files); } - public function testLoadTestsuiteFilesFromConfigWhileIncludingAndExcludingTheSameDirectory(): void - { - $options = new Options([ - 'configuration' => $this->fixture('phpunit-excluded-including-excluding-same-dir.xml'), - 'testsuite' => ['ParaTest Fixtures'], - ]); - $loader = new SuiteLoader($options); - $loader->load(); - $files = $this->getObjectValue($loader, 'files'); - - $expected = 1; - static::assertCount($expected, $files); - } - public function testLoadTestsuiteFilesFromConfig(): void { $options = new Options([ @@ -160,21 +147,6 @@ public function testLoadTestsuiteWithFilesDirsMixed(): void static::assertCount($expected, $files); } - public function testLoadTestsuiteWithNestedSuite(): void - { - $options = new Options([ - 'configuration' => $this->fixture('phpunit-files-dirs-mix-nested.xml'), - 'testsuite' => ['ParaTest Fixtures'], - ]); - $loader = new SuiteLoader($options); - $loader->load(); - $files = $this->getObjectValue($loader, 'files'); - - $expected = count($this->findTests(FIXTURES . DS . 'passing-tests')) + - count($this->findTests(FIXTURES . DS . 'failing-tests')) + 1; - static::assertCount($expected, $files); - } - public function testLoadTestsuiteWithDuplicateFilesDirMixed(): void { $options = new Options([ @@ -185,7 +157,7 @@ public function testLoadTestsuiteWithDuplicateFilesDirMixed(): void $loader->load(); $files = $this->getObjectValue($loader, 'files'); - $expected = count($this->findTests(FIXTURES . DS . 'passing-tests')) + 1; + $expected = count($this->findTests(FIXTURES . DS . 'passing-tests')) + 2; static::assertCount($expected, $files); } @@ -214,11 +186,15 @@ public function testLoadSuiteFromConfigWithMultipleDirs(): void public function testLoadSuiteFromConfigWithBadSuitePath(): void { + $options = new Options([ + 'configuration' => $this->fixture('phpunit-non-existent-testsuite-dir.xml'), + 'testsuite' => [uniqid()], + ]); + $loader = new SuiteLoader($options); + $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('Suite path ./nope/ could not be found'); + $this->expectExceptionMessageMatches('/Suite path \w+ could not be found/'); - $options = new Options(['configuration' => $this->fixture('phpunit-non-existent-testsuite-dir.xml')]); - $loader = new SuiteLoader($options); $loader->load(); } diff --git a/test/fixtures/phpunit-excluded-including-excluding-same-dir.xml b/test/fixtures/phpunit-excluded-including-excluding-same-dir.xml deleted file mode 100644 index 0ebe64a6..00000000 --- a/test/fixtures/phpunit-excluded-including-excluding-same-dir.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - ./excluded-tests/included - ./excluded-tests/included - - - diff --git a/test/fixtures/phpunit-files-dirs-mix-nested.xml b/test/fixtures/phpunit-files-dirs-mix-nested.xml deleted file mode 100644 index 568bcb6b..00000000 --- a/test/fixtures/phpunit-files-dirs-mix-nested.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - ./failing-tests/ - ./passing-tests/TestOfUnits.php - ./passing-tests/GroupsTest.php - Nested Suite - - - ./passing-tests/ - ./passing-tests/GroupsTest.php - - -