Skip to content

Commit

Permalink
Add PHPUnit's Extension support (#726)
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk authored Feb 7, 2023
1 parent 5346ac0 commit 76baf4c
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"phpunit/php-code-coverage": "^10.0",
"phpunit/php-file-iterator": "^4.0",
"phpunit/php-timer": "^6.0",
"phpunit/phpunit": "^10.0.4",
"phpunit/phpunit": "^10.0.5",
"sebastian/environment": "^6.0",
"symfony/console": "^6.2.5",
"symfony/process": "^6.2.5"
Expand Down
23 changes: 23 additions & 0 deletions src/WrapperRunner/ApplicationForWrapperWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use PHPUnit\Logging\TeamCity\TeamCityLogger;
use PHPUnit\Logging\TestDox\TestResultCollector;
use PHPUnit\Runner\CodeCoverage;
use PHPUnit\Runner\Extension\ExtensionBootstrapper;
use PHPUnit\Runner\Extension\Facade as ExtensionFacade;
use PHPUnit\Runner\Extension\PharLoader;
use PHPUnit\Runner\TestSuiteLoader;
use PHPUnit\Runner\TestSuiteSorter;
use PHPUnit\TestRunner\TestResult\Facade as TestResultFacade;
Expand Down Expand Up @@ -86,6 +89,26 @@ private function bootstrap(): void
EventFacade::emitter()->testRunnerBootstrapFinished($bootstrapFilename);
}

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

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

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

CodeCoverage::init($this->configuration);

if ($this->configuration->hasLogfileJunit()) {
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/extensions/FooTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace ParaTest\Tests\fixtures\extensions;

use PHPUnit\Framework\TestCase;

/** @internal */
final class FooTest extends TestCase
{
public function testExtensionValue(): void
{
$this->assertSame('success', MyExtension::$value);
}
}
27 changes: 27 additions & 0 deletions test/fixtures/extensions/MyExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace ParaTest\Tests\fixtures\extensions;

use PHPUnit\Event\TestRunner\EventFacadeSealed;
use PHPUnit\Event\TestRunner\EventFacadeSealedSubscriber;
use PHPUnit\Runner\Extension\Extension;
use PHPUnit\Runner\Extension\Facade;
use PHPUnit\Runner\Extension\ParameterCollection;
use PHPUnit\TextUI\Configuration\Configuration;

final class MyExtension implements Extension, EventFacadeSealedSubscriber
{
public static string $value = 'fail';

public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
{
$facade->registerSubscriber($this);
}

public function notify(EventFacadeSealed $event): void
{
self::$value = 'success';
}
}
14 changes: 14 additions & 0 deletions test/fixtures/extensions/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../vendor/phpunit/phpunit/phpunit.xsd"
>
<testsuites>
<testsuite name="Extensions Test">
<file>./FooTest.php</file>
</testsuite>
</testsuites>
<extensions>
<bootstrap class="\ParaTest\Tests\fixtures\extensions\MyExtension"/>
</extensions>
</phpunit>

0 comments on commit 76baf4c

Please sign in to comment.