Skip to content

Commit

Permalink
support for phpunit 10
Browse files Browse the repository at this point in the history
  • Loading branch information
Ondřej Ešler committed May 24, 2023
1 parent e67c0e6 commit dcf48ba
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
vendor
/vendor
composer.lock
composer.phar
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ A simple plug-in which allows to you interrupt running PHPUnit tests **gracefull
```bash
composer require --dev esler/phpunit-graceful-interrupt
```

#### For PHPUnit >= 10
```xml
Add extension to your `phpunit.xml`
<phpunit>
<extensions>
<bootstrap class="Esler\PHPUnit\GracefulInterruptExtension" />
</extensions>
</phpunit>
```

#### For PHPUnit < 10
Add listener to your `phpunit.xml`
```xml
<phpunit>
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"require": {
"ext-pcntl": "*",
"phpunit/phpunit": "^7.0||^8.0||^9.0"
"phpunit/phpunit": "^7.0||^8.0||^9.0||^10.0"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 5 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<phpunit>
<listeners>
<!-- <listeners>
<listener class="Esler\PHPUnit\Listener\GracefulInterruptListener" file="./src/PHPUnit/Listener/GracefulInterruptListener.php" />
</listeners>
</listeners> -->
<extensions>
<bootstrap class="Esler\PHPUnit\GracefulInterruptExtension" />
</extensions>
<testsuites>
<testsuite name="Fake testsuite">
<directory>./tests/unit/</directory>
Expand Down
33 changes: 33 additions & 0 deletions src/PHPUnit/GracefulInterruptExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace Esler\PHPUnit;

use BadMethodCallException;
use PHPUnit\Runner\Extension\Extension;
use PHPUnit\Runner\Extension\Facade;
use PHPUnit\Runner\Extension\ParameterCollection;
use PHPUnit\TextUI\Configuration\Configuration;

/**
* This class defines an extension for PHPUnit.
*
* It allows interrupt running test gracefully. By hitting Ctrl+\ (SIGQUIT)
* you will let know to PHPUnit
*
* @author Ondrej Esler <[email protected]>
* @license MIT
*/
final class GracefulInterruptExtension implements Extension
{
public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
{
if (!function_exists('pcntl_signal')) {
throw new BadMethodCallException('PCNTL is disabled');
}

$facade->registerSubscriber($subscriber = new GracefulInterruptSubscriber());

pcntl_signal(SIGQUIT, static function () use ($subscriber) {
$subscriber->interrupted = true;
});
}
}
29 changes: 29 additions & 0 deletions src/PHPUnit/GracefulInterruptSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace Esler\PHPUnit;

use PHPUnit\Event\Test\PreparedSubscriber;
use PHPUnit\Event\Test\Prepared;
use PHPUnit\Framework\Assert;

/**
* This class defines an extension for PHPUnit.
*
* It allows interrupt running test gracefully. By hitting Ctrl+\ (SIGQUIT)
* you will let know to PHPUnit
*
* @author Ondrej Esler <[email protected]>
* @license MIT
*/
final class GracefulInterruptSubscriber implements PreparedSubscriber
{
public bool $interrupted = false;

public function notify(Prepared $event): void
{
pcntl_signal_dispatch();

if ($this->interrupted) {
Assert::markTestSkipped('Skipped by ' . $this::class);
}
}
}
3 changes: 3 additions & 0 deletions src/PHPUnit/Listener/GracefulInterruptListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class GracefulInterruptListener implements TestListener
{
use TestListenerDefaultImplementation;

/** @var Test */
private $test;

/**
* Constructor
*/
Expand Down

0 comments on commit dcf48ba

Please sign in to comment.