From 48bb51b5187f10688e8d1885c548106e8b1dfdcb Mon Sep 17 00:00:00 2001 From: Ewout Pieter den Ouden Date: Wed, 18 Jul 2018 16:50:57 +0200 Subject: [PATCH] Basic config infrastructure to support #2085 --- phpunit.xsd | 1 + src/Framework/TestResult.php | 21 +++++++++++++++++++++ src/TextUI/Command.php | 6 ++++++ src/TextUI/TestRunner.php | 6 ++++++ src/Util/Configuration.php | 8 ++++++++ tests/_files/configuration.xml | 1 + tests/_files/configuration_xinclude.xml | 1 + tests/unit/Util/ConfigurationTest.php | 1 + 8 files changed, 45 insertions(+) diff --git a/phpunit.xsd b/phpunit.xsd index 7eaa65eda1d..68ea3604d5d 100644 --- a/phpunit.xsd +++ b/phpunit.xsd @@ -260,6 +260,7 @@ + diff --git a/src/Framework/TestResult.php b/src/Framework/TestResult.php index 4a5b1634ed0..5e63cc42344 100644 --- a/src/Framework/TestResult.php +++ b/src/Framework/TestResult.php @@ -139,6 +139,11 @@ class TestResult implements Countable */ protected $beStrictAboutResourceUsageDuringSmallTests = false; + /** + * @var int + */ + protected $defaultTimeLimit = 1; + /** * @var bool */ @@ -1061,6 +1066,14 @@ public function wasSuccessful(): bool return empty($this->errors) && empty($this->failures) && empty($this->warnings); } + /** + * Sets the default timeout for tests + */ + public function setDefaultTimeLimit(int $timeout): void + { + $this->defaultTimeLimit = $timeout; + } + /** * Sets the timeout for small tests. */ @@ -1085,6 +1098,14 @@ public function setTimeoutForLargeTests(int $timeout): void $this->timeoutForLargeTests = $timeout; } + /** + * Returns the set timeout for large tests. + */ + public function getDefaultTimeoutTests(): int + { + return $this->defaultTimeLimit; + } + /** * Returns the set timeout for large tests. */ diff --git a/src/TextUI/Command.php b/src/TextUI/Command.php index 24c96c5bc96..b4b7334e099 100644 --- a/src/TextUI/Command.php +++ b/src/TextUI/Command.php @@ -84,6 +84,7 @@ class Command 'disallow-test-output' => null, 'disallow-resource-usage' => null, 'disallow-todo-tests' => null, + 'default-time-limit=' => null, 'enforce-time-limit' => null, 'exclude-group=' => null, 'filter=' => null, @@ -692,6 +693,11 @@ protected function handleArguments(array $argv): void break; + case '--default-time-limit': + $this->arguments['defaultTimeLimit'] = (int) $option[1]; + + break; + case '--enforce-time-limit': $this->arguments['enforceTimeLimit'] = true; diff --git a/src/TextUI/TestRunner.php b/src/TextUI/TestRunner.php index 5563879f912..73e7206185d 100644 --- a/src/TextUI/TestRunner.php +++ b/src/TextUI/TestRunner.php @@ -583,6 +583,7 @@ public function doRun(Test $suite, array $arguments = [], bool $exit = true): Te $result->beStrictAboutTodoAnnotatedTests($arguments['disallowTodoAnnotatedTests']); $result->beStrictAboutResourceUsageDuringSmallTests($arguments['beStrictAboutResourceUsageDuringSmallTests']); $result->enforceTimeLimit($arguments['enforceTimeLimit']); + $result->setDefaultTimeLimit($arguments['defaultTimeLimit']); $result->setTimeoutForSmallTests($arguments['timeoutForSmallTests']); $result->setTimeoutForMediumTests($arguments['timeoutForMediumTests']); $result->setTimeoutForLargeTests($arguments['timeoutForLargeTests']); @@ -945,6 +946,10 @@ protected function handleConfiguration(array &$arguments): void $arguments['disallowTestOutput'] = $phpunitConfiguration['disallowTestOutput']; } + if (isset($phpunitConfiguration['defaultTimeLimit']) && !isset($arguments['defaultTimeLimit'])) { + $arguments['defaultTimeLimit'] = $phpunitConfiguration['defaultTimeLimit']; + } + if (isset($phpunitConfiguration['enforceTimeLimit']) && !isset($arguments['enforceTimeLimit'])) { $arguments['enforceTimeLimit'] = $phpunitConfiguration['enforceTimeLimit']; } @@ -1182,6 +1187,7 @@ protected function handleConfiguration(array &$arguments): void $arguments['crap4jThreshold'] = $arguments['crap4jThreshold'] ?? 30; $arguments['disallowTestOutput'] = $arguments['disallowTestOutput'] ?? false; $arguments['disallowTodoAnnotatedTests'] = $arguments['disallowTodoAnnotatedTests'] ?? false; + $arguments['defaultTimeLimit'] = $arguments['defaultTimeLimit'] ?? 1; $arguments['enforceTimeLimit'] = $arguments['enforceTimeLimit'] ?? false; $arguments['excludeGroups'] = $arguments['excludeGroups'] ?? []; $arguments['failOnRisky'] = $arguments['failOnRisky'] ?? false; diff --git a/src/Util/Configuration.php b/src/Util/Configuration.php index ad70571c888..8845dbc97c9 100644 --- a/src/Util/Configuration.php +++ b/src/Util/Configuration.php @@ -59,6 +59,7 @@ * beStrictAboutResourceUsageDuringSmallTests="false" * beStrictAboutTestsThatDoNotTestAnything="false" * beStrictAboutTodoAnnotatedTests="false" + * defaultTimeLimit="1" * enforceTimeLimit="false" * ignoreDeprecatedCodeUnitsFromCodeCoverage="false" * timeoutForSmallTests="1" @@ -855,6 +856,13 @@ public function getPHPUnitConfiguration(): array ); } + if ($root->hasAttribute('defaultTimeLimit')) { + $result['defaultTimeLimit'] = $this->getInteger( + (string) $root->getAttribute('defaultTimeLimit'), + 1 + ); + } + if ($root->hasAttribute('enforceTimeLimit')) { $result['enforceTimeLimit'] = $this->getBoolean( (string) $root->getAttribute('enforceTimeLimit'), diff --git a/tests/_files/configuration.xml b/tests/_files/configuration.xml index 36c91caccc5..5786d920cdd 100644 --- a/tests/_files/configuration.xml +++ b/tests/_files/configuration.xml @@ -25,6 +25,7 @@ beStrictAboutTestsThatDoNotTestAnything="false" beStrictAboutTodoAnnotatedTests="false" beStrictAboutCoversAnnotation="false" + defaultTimeLimit="123" enforceTimeLimit="false" ignoreDeprecatedCodeUnitsFromCodeCoverage="false" timeoutForSmallTests="1" diff --git a/tests/_files/configuration_xinclude.xml b/tests/_files/configuration_xinclude.xml index 536d461351b..19087d36921 100644 --- a/tests/_files/configuration_xinclude.xml +++ b/tests/_files/configuration_xinclude.xml @@ -26,6 +26,7 @@ beStrictAboutTestsThatDoNotTestAnything="false" beStrictAboutTodoAnnotatedTests="false" beStrictAboutCoversAnnotation="false" + defaultTimeLimit="123" enforceTimeLimit="false" ignoreDeprecatedCodeUnitsFromCodeCoverage="false" timeoutForSmallTests="1" diff --git a/tests/unit/Util/ConfigurationTest.php b/tests/unit/Util/ConfigurationTest.php index 92bffa2d5e8..95d8652ebff 100644 --- a/tests/unit/Util/ConfigurationTest.php +++ b/tests/unit/Util/ConfigurationTest.php @@ -471,6 +471,7 @@ public function testPHPUnitConfigurationIsReadCorrectly(): void 'reportUselessTests' => false, 'strictCoverage' => false, 'disallowTestOutput' => false, + 'defaultTimeLimit' => 123, 'enforceTimeLimit' => false, 'extensionsDirectory' => '/tmp', 'printerClass' => 'PHPUnit\TextUI\ResultPrinter',