Skip to content

Commit 8075b00

Browse files
epdenoudensebastianbergmann
authored andcommitted
Add checks for required libraries
1 parent 8f2b043 commit 8075b00

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

src/Framework/TestResult.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,7 @@ public function run(Test $test): void
645645
try {
646646
if (!$test instanceof WarningTestCase &&
647647
$this->enforceTimeLimit &&
648-
($this->defaultTimeLimit || $test->getSize() != \PHPUnit\Util\Test::UNKNOWN) &&
649-
\extension_loaded('pcntl') && \class_exists(Invoker::class)) {
648+
($this->defaultTimeLimit || $test->getSize() != \PHPUnit\Util\Test::UNKNOWN)) {
650649
switch ($test->getSize()) {
651650
case \PHPUnit\Util\Test::SMALL:
652651
$_timeout = $this->timeoutForSmallTests;

src/TextUI/TestRunner.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
use SebastianBergmann\CodeCoverage\Report\Xml\Facade as XmlReport;
5555
use SebastianBergmann\Comparator\Comparator;
5656
use SebastianBergmann\Environment\Runtime;
57+
use SebastianBergmann\Invoker\Invoker;
5758

5859
/**
5960
* A TestRunner for the Command Line Interface (CLI)
@@ -582,6 +583,16 @@ public function doRun(Test $suite, array $arguments = [], bool $exit = true): Te
582583
$result->beStrictAboutOutputDuringTests($arguments['disallowTestOutput']);
583584
$result->beStrictAboutTodoAnnotatedTests($arguments['disallowTodoAnnotatedTests']);
584585
$result->beStrictAboutResourceUsageDuringSmallTests($arguments['beStrictAboutResourceUsageDuringSmallTests']);
586+
587+
if ($arguments['enforceTimeLimit'] === true) {
588+
if (!\class_exists(Invoker::class)) {
589+
$this->writeMessage('Error', 'Package phpunit/php-invoker is required for enforcing time limits');
590+
}
591+
592+
if (!\extension_loaded('pcntl') || \strpos(\ini_get('disable_functions'), 'pcntl') !== false) {
593+
$this->writeMessage('Error', 'PHP extension pcntl is required for enforcing time limits');
594+
}
595+
}
585596
$result->enforceTimeLimit($arguments['enforceTimeLimit']);
586597
$result->setDefaultTimeLimit($arguments['defaultTimeLimit']);
587598
$result->setTimeoutForSmallTests($arguments['timeoutForSmallTests']);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
use PHPUnit\Framework\TestCase;
11+
12+
class Issue2085Test extends TestCase
13+
{
14+
public function testShouldAbortSlowTestByEnforcingTimeLimit(): void
15+
{
16+
$this->assertTrue(true);
17+
\sleep(2);
18+
$this->assertTrue(true);
19+
}
20+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
https://github.com/sebastianbergmann/phpunit/issues/2085
3+
--SKIPIF--
4+
<?php
5+
if (false || !\class_exists(Invoker::class)) {
6+
print "Package phpunit/php-invoker is required for enforcing time limits");
7+
}
8+
--FILE--
9+
<?php
10+
$_SERVER['argv'][1] = '--no-configuration';
11+
$_SERVER['argv'][2] = '--enforce-time-limit';
12+
$_SERVER['argv'][3] = '--default-time-limit=1';
13+
$_SERVER['argv'][4] = __DIR__ . '/Issue2085Test.php';
14+
15+
require __DIR__ . '/../../../bootstrap.php';
16+
PHPUnit\TextUI\Command::main();
17+
--EXPECTF--
18+
PHPUnit %s by Sebastian Bergmann and contributors.
19+
20+
R 1 / 1 (100%)
21+
22+
Time: %s, Memory: %s
23+
24+
There was 1 risky test:
25+
26+
1) Issue2085Test::testShouldAbortSlowTestByEnforcingTimeLimit
27+
Execution aborted after 1 second
28+
29+
OK, but incomplete, skipped, or risky tests!
30+
Tests: 1, Assertions: 1, Risky: 1.

0 commit comments

Comments
 (0)