Skip to content

Commit

Permalink
Add checks for required libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
epdenouden authored and sebastianbergmann committed Sep 8, 2018
1 parent 8f2b043 commit 8075b00
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/Framework/TestResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,7 @@ public function run(Test $test): void
try {
if (!$test instanceof WarningTestCase &&
$this->enforceTimeLimit &&
($this->defaultTimeLimit || $test->getSize() != \PHPUnit\Util\Test::UNKNOWN) &&
\extension_loaded('pcntl') && \class_exists(Invoker::class)) {
($this->defaultTimeLimit || $test->getSize() != \PHPUnit\Util\Test::UNKNOWN)) {
switch ($test->getSize()) {
case \PHPUnit\Util\Test::SMALL:
$_timeout = $this->timeoutForSmallTests;
Expand Down
11 changes: 11 additions & 0 deletions src/TextUI/TestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
use SebastianBergmann\CodeCoverage\Report\Xml\Facade as XmlReport;
use SebastianBergmann\Comparator\Comparator;
use SebastianBergmann\Environment\Runtime;
use SebastianBergmann\Invoker\Invoker;

/**
* A TestRunner for the Command Line Interface (CLI)
Expand Down Expand Up @@ -582,6 +583,16 @@ public function doRun(Test $suite, array $arguments = [], bool $exit = true): Te
$result->beStrictAboutOutputDuringTests($arguments['disallowTestOutput']);
$result->beStrictAboutTodoAnnotatedTests($arguments['disallowTodoAnnotatedTests']);
$result->beStrictAboutResourceUsageDuringSmallTests($arguments['beStrictAboutResourceUsageDuringSmallTests']);

if ($arguments['enforceTimeLimit'] === true) {
if (!\class_exists(Invoker::class)) {
$this->writeMessage('Error', 'Package phpunit/php-invoker is required for enforcing time limits');
}

if (!\extension_loaded('pcntl') || \strpos(\ini_get('disable_functions'), 'pcntl') !== false) {
$this->writeMessage('Error', 'PHP extension pcntl is required for enforcing time limits');
}
}
$result->enforceTimeLimit($arguments['enforceTimeLimit']);
$result->setDefaultTimeLimit($arguments['defaultTimeLimit']);
$result->setTimeoutForSmallTests($arguments['timeoutForSmallTests']);
Expand Down
20 changes: 20 additions & 0 deletions tests/Regression/GitHub/2085/Issue2085Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\TestCase;

class Issue2085Test extends TestCase
{
public function testShouldAbortSlowTestByEnforcingTimeLimit(): void
{
$this->assertTrue(true);
\sleep(2);
$this->assertTrue(true);
}
}
30 changes: 30 additions & 0 deletions tests/Regression/GitHub/2085/issue-2085-test.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
https://github.com/sebastianbergmann/phpunit/issues/2085
--SKIPIF--
<?php
if (false || !\class_exists(Invoker::class)) {
print "Package phpunit/php-invoker is required for enforcing time limits");
}
--FILE--
<?php
$_SERVER['argv'][1] = '--no-configuration';
$_SERVER['argv'][2] = '--enforce-time-limit';
$_SERVER['argv'][3] = '--default-time-limit=1';
$_SERVER['argv'][4] = __DIR__ . '/Issue2085Test.php';

require __DIR__ . '/../../../bootstrap.php';
PHPUnit\TextUI\Command::main();
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

R 1 / 1 (100%)

Time: %s, Memory: %s

There was 1 risky test:

1) Issue2085Test::testShouldAbortSlowTestByEnforcingTimeLimit
Execution aborted after 1 second

OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 1, Risky: 1.

0 comments on commit 8075b00

Please sign in to comment.