Skip to content

Commit

Permalink
Run extension before loading the suite
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk committed Dec 10, 2024
1 parent 1c7b88a commit 9166833
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 26 deletions.
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@
"phpunit/php-timer": "^7.0.1",
"phpunit/phpunit": "^11.5.0",
"sebastian/environment": "^7.2.0",
"symfony/console": "^6.4.14 || ^7.1.7",
"symfony/process": "^6.4.14 || ^7.1.7"
"symfony/console": "^6.4.14 || ^7.2.0",
"symfony/process": "^6.4.14 || ^7.2.0"
},
"require-dev": {
"ext-pcov": "*",
"ext-posix": "*",
"doctrine/coding-standard": "^12.0.0",
"phpstan/phpstan": "^2",
"phpstan/phpstan-deprecation-rules": "^2",
"phpstan/phpstan-phpunit": "^2",
"phpstan/phpstan": "^2.0.3",
"phpstan/phpstan-deprecation-rules": "^2.0.1",
"phpstan/phpstan-phpunit": "^2.0.1",
"phpstan/phpstan-strict-rules": "^2",
"squizlabs/php_codesniffer": "^3.11.1",
"symfony/filesystem": "^6.4.13 || ^7.1.6"
"symfony/filesystem": "^6.4.13 || ^7.2.0"
},
"autoload": {
"psr-4": {
Expand Down
33 changes: 32 additions & 1 deletion src/WrapperRunner/SuiteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@

use Generator;
use ParaTest\Options;
use PHPUnit\Event\Facade as EventFacade;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestSuite;
use PHPUnit\Runner\Extension\ExtensionBootstrapper;
use PHPUnit\Runner\Extension\Facade as ExtensionFacade;
use PHPUnit\Runner\Extension\PharLoader;
use PHPUnit\Runner\PhptTestCase;
use PHPUnit\Runner\ResultCache\NullResultCache;
use PHPUnit\Runner\TestSuiteSorter;
use PHPUnit\TestRunner\TestResult\Facade as TestResultFacade;
use PHPUnit\TextUI\Command\Result;
use PHPUnit\TextUI\Command\WarmCodeCoverageCacheCommand;
use PHPUnit\TextUI\Configuration\CodeCoverageFilterRegistry;
Expand Down Expand Up @@ -50,9 +55,35 @@ public function __construct(
(new PhpHandler())->handle($this->options->configuration->php());

if ($this->options->configuration->hasBootstrap()) {
include_once $this->options->configuration->bootstrap();
$bootstrapFilename = $this->options->configuration->bootstrap();
include_once $bootstrapFilename;
EventFacade::emitter()->testRunnerBootstrapFinished($bootstrapFilename);
}

if (! $this->options->configuration->noExtensions()) {
if ($this->options->configuration->hasPharExtensionDirectory()) {
(new PharLoader())->loadPharExtensionsInDirectory(
$this->options->configuration->pharExtensionDirectory(),
);
}

$extensionFacade = new ExtensionFacade();
$extensionBootstrapper = new ExtensionBootstrapper(
$this->options->configuration,
$extensionFacade,
);

foreach ($this->options->configuration->extensionBootstrappers() as $bootstrapper) {
$extensionBootstrapper->bootstrap(
$bootstrapper['className'],
$bootstrapper['parameters'],
);
}
}

TestResultFacade::init();
EventFacade::instance()->seal();

$testSuite = (new TestSuiteBuilder())->build($this->options->configuration);

if ($this->options->configuration->executionOrder() === TestSuiteSorter::ORDER_RANDOMIZED) {
Expand Down
3 changes: 0 additions & 3 deletions src/WrapperRunner/WrapperRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use ParaTest\JUnit\Writer;
use ParaTest\Options;
use ParaTest\RunnerInterface;
use PHPUnit\Event\Facade as EventFacade;
use PHPUnit\Runner\CodeCoverage;
use PHPUnit\TestRunner\TestResult\Facade as TestResultFacade;
use PHPUnit\TestRunner\TestResult\TestResult;
Expand Down Expand Up @@ -100,8 +99,6 @@ public function run(): int
$directory = dirname(__DIR__);
assert($directory !== '');
ExcludeList::addDirectory($directory);
TestResultFacade::init();
EventFacade::instance()->seal();
$suiteLoader = new SuiteLoader(
$this->options,
$this->output,
Expand Down
12 changes: 11 additions & 1 deletion test/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use ParaTest\Options;
use ParaTest\RunnerInterface;
use ParaTest\WrapperRunner\WrapperRunner;
use PHPUnit\Event\Facade;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Output\BufferedOutput;
Expand Down Expand Up @@ -87,7 +89,15 @@ final protected function runRunner(): RunnerResult
unset($_SERVER[Options::ENV_KEY_UNIQUE_TOKEN]);
}

$exitCode = $runner->run();
$refProperty = new ReflectionProperty(Facade::class, 'instance');
$keep = $refProperty->getValue(new Facade());
$refProperty->setValue(new Facade(), null);
try {
$exitCode = $runner->run();
} finally {
$refProperty->setValue(new Facade(), $keep);
}

if ($shouldPutEnvForParatestTestingItSelf) {
putenv(Options::ENV_KEY_TOKEN . '=' . $prevToken);
putenv(Options::ENV_KEY_UNIQUE_TOKEN . '=' . $prevUniqueToken);
Expand Down
30 changes: 15 additions & 15 deletions test/Unit/WrapperRunner/WrapperRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -762,21 +762,6 @@ public function testBaselineIsRespected(): void
self::assertEquals(RunnerInterface::SUCCESS_EXIT, $runnerResult->exitCode);
}

/**
* ### WARNING ###
*
* This test MUST be the last of this file,
* otherwise the next one will always fail
*/
public function testProcessIsolation(): void
{
$this->bareOptions['path'] = $this->fixture('process_isolation' . DIRECTORY_SEPARATOR . 'FooTest.php');
$this->bareOptions['--process-isolation'] = true;

$runnerResult = $this->runRunner();
self::assertSame(RunnerInterface::SUCCESS_EXIT, $runnerResult->exitCode);
}

public function testExtensionMustRunBeforeDataProvider(): void
{
$this->bareOptions['--configuration'] = $this->fixture('extension_run_before_data_provider' . DIRECTORY_SEPARATOR . 'phpunit.xml');
Expand All @@ -798,6 +783,21 @@ public function testExtensionMustRunBeforeDataProvider(): void
self::assertEquals(RunnerInterface::SUCCESS_EXIT, $runnerResult->exitCode);
}

/**
* ### WARNING ###
*
* This test MUST be the last of this file,
* otherwise the next one will always fail
*/
public function testProcessIsolation(): void
{
$this->bareOptions['path'] = $this->fixture('process_isolation' . DIRECTORY_SEPARATOR . 'FooTest.php');
$this->bareOptions['--process-isolation'] = true;

$runnerResult = $this->runRunner();
self::assertSame(RunnerInterface::SUCCESS_EXIT, $runnerResult->exitCode);
}

private static function sorted(string $from): string
{
$from = explode(PHP_EOL, $from);
Expand Down

0 comments on commit 9166833

Please sign in to comment.