Skip to content

Commit 5afa2bf

Browse files
committed
Support disabling fetching by ENV variable
1 parent 622260b commit 5afa2bf

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

src/PHPUnit/Listeners/NaughtyTestListener.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class NaughtyTestListener extends BaseTestListener
2121
/** @var string Config key enabling execution of MetricFetcher before & after running all tests (bool) */
2222
const CONFIG_KEY_LEVEL_GLOBAL = 'executeOnGlobalLevel';
2323

24+
const DISABLE_ENV_KEY = 'DISABLE_NAUGHTY_TEST_DETECTOR';
25+
2426
private $defaultOptions = [
2527
self::CONFIG_KEY_LEVEL_TEST => false,
2628
self::CONFIG_KEY_LEVEL_SUITE => true,
@@ -60,7 +62,8 @@ public function __construct(
6062
array $constructorArgs = [],
6163
array $options = []
6264
) {
63-
if ($metricFetcherClass !== null) {
65+
$disabledByEnv = in_array(getenv(self::DISABLE_ENV_KEY), ['1', 'true', 'TRUE'], true);
66+
if (!$disabledByEnv && $metricFetcherClass !== null) {
6467
$this->metricFetcher = new $metricFetcherClass(...$constructorArgs);
6568
}
6669
$this->flags = array_merge($this->defaultOptions, $options);

tests/PHPUnit/Listeners/NaughtyTestListenerTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,27 @@ public function testAllLevels()
205205
], $output);
206206
}
207207

208+
public function testDisabledByEnv()
209+
{
210+
// setup env
211+
putenv('DISABLE_NAUGHTY_TEST_DETECTOR=1');
212+
213+
$testListener = new NaughtyTestListener('NonExistingClassName', [], [
214+
NaughtyTestListener::CONFIG_KEY_LEVEL_GLOBAL => true,
215+
NaughtyTestListener::CONFIG_KEY_LEVEL_SUITE => true,
216+
NaughtyTestListener::CONFIG_KEY_LEVEL_TEST => true,
217+
]);
218+
219+
$this->startOutputCapture();
220+
$this->runTestSuites($testListener, [1, 1]);
221+
$output = $this->finishOutputCapture();
222+
223+
static::assertSame('', $output);
224+
225+
// revert env
226+
putenv('DISABLE_NAUGHTY_TEST_DETECTOR=');
227+
}
228+
208229
private function createTestSuiteMock($name)
209230
{
210231
$mock = $this->getMock(TestSuite::class);

0 commit comments

Comments
 (0)