Skip to content

Commit

Permalink
[PhpUnitBridge] Use verbose deprecation output for quiet types only w…
Browse files Browse the repository at this point in the history
…hen it reaches the threshold
  • Loading branch information
ogizanagi committed Dec 27, 2022
1 parent 9379abd commit 2232d32
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 5 deletions.
10 changes: 5 additions & 5 deletions DeprecationErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function shutdown()
// store failing status
$isFailing = !$configuration->tolerates($this->deprecationGroups);

$this->displayDeprecations($groups, $configuration, $isFailing);
$this->displayDeprecations($groups, $configuration);

$this->resetDeprecationGroups();

Expand All @@ -213,7 +213,7 @@ public function shutdown()
}

$isFailingAtShutdown = !$configuration->tolerates($this->deprecationGroups);
$this->displayDeprecations($groups, $configuration, $isFailingAtShutdown);
$this->displayDeprecations($groups, $configuration);

if ($configuration->isGeneratingBaseline()) {
$configuration->writeBaseline();
Expand Down Expand Up @@ -289,11 +289,10 @@ private static function colorize($str, $red)
/**
* @param string[] $groups
* @param Configuration $configuration
* @param bool $isFailing
*
* @throws \InvalidArgumentException
*/
private function displayDeprecations($groups, $configuration, $isFailing)
private function displayDeprecations($groups, $configuration)
{
$cmp = function ($a, $b) {
return $b->count() - $a->count();
Expand All @@ -320,7 +319,8 @@ private function displayDeprecations($groups, $configuration, $isFailing)
fwrite($handle, "\n".self::colorize($deprecationGroupMessage, 'legacy' !== $group && 'indirect' !== $group)."\n");
}

if ('legacy' !== $group && !$configuration->verboseOutput($group) && !$isFailing) {
// Skip the verbose output if the group is quiet and not failing according to its threshold:
if ('legacy' !== $group && !$configuration->verboseOutput($group) && $configuration->toleratesForGroup($group, $this->deprecationGroups)) {
continue;
}
$notices = $this->deprecationGroups[$group]->notices();
Expand Down
26 changes: 26 additions & 0 deletions DeprecationErrorHandler/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,32 @@ public function tolerates(array $deprecationGroups)
return true;
}

/**
* @param array<string,DeprecationGroup> $deprecationGroups
*
* @return bool true if the threshold is not reached for the deprecation type nor for the total
*/
public function toleratesForGroup(string $groupName, array $deprecationGroups): bool
{
$grandTotal = 0;

foreach ($deprecationGroups as $type => $group) {
if ('legacy' !== $type) {
$grandTotal += $group->count();
}
}

if ($grandTotal > $this->thresholds['total']) {
return false;
}

if (\in_array($groupName, ['self', 'direct', 'indirect'], true) && $deprecationGroups[$groupName]->count() > $this->thresholds[$groupName]) {
return false;
}

return true;
}

/**
* @return bool
*/
Expand Down
97 changes: 97 additions & 0 deletions Tests/DeprecationErrorHandler/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,103 @@ public function testOutputIsNotVerboseInWeakMode()
$this->assertFalse($configuration->verboseOutput('other'));
}

/**
* @dataProvider provideDataForToleratesForGroup
*/
public function testToleratesForIndividualGroups(string $deprecationsHelper, array $deprecationsPerType, array $expected)
{
$configuration = Configuration::fromUrlEncodedString($deprecationsHelper);

$groups = $this->buildGroups($deprecationsPerType);

foreach ($expected as $groupName => $tolerates) {
$this->assertSame($tolerates, $configuration->toleratesForGroup($groupName, $groups), sprintf('Deprecation type "%s" is %s', $groupName, $tolerates ? 'tolerated' : 'not tolerated'));
}
}

public function provideDataForToleratesForGroup() {

yield 'total threshold not reached' => ['max[total]=1', [
'unsilenced' => 0,
'self' => 0,
'legacy' => 1, // Legacy group is ignored in total threshold
'other' => 0,
'direct' => 1,
'indirect' => 0,
], [
'unsilenced' => true,
'self' => true,
'legacy' => true,
'other' => true,
'direct' => true,
'indirect' => true,
]];

yield 'total threshold reached' => ['max[total]=1', [
'unsilenced' => 0,
'self' => 0,
'legacy' => 1,
'other' => 0,
'direct' => 1,
'indirect' => 1,
], [
'unsilenced' => false,
'self' => false,
'legacy' => false,
'other' => false,
'direct' => false,
'indirect' => false,
]];

yield 'direct threshold reached' => ['max[total]=99&max[direct]=0', [
'unsilenced' => 0,
'self' => 0,
'legacy' => 1,
'other' => 0,
'direct' => 1,
'indirect' => 1,
], [
'unsilenced' => true,
'self' => true,
'legacy' => true,
'other' => true,
'direct' => false,
'indirect' => true,
]];

yield 'indirect & self threshold reached' => ['max[total]=99&max[direct]=0&max[self]=0', [
'unsilenced' => 0,
'self' => 1,
'legacy' => 1,
'other' => 1,
'direct' => 1,
'indirect' => 1,
], [
'unsilenced' => true,
'self' => false,
'legacy' => true,
'other' => true,
'direct' => false,
'indirect' => true,
]];

yield 'indirect & self threshold not reached' => ['max[total]=99&max[direct]=2&max[self]=2', [
'unsilenced' => 0,
'self' => 1,
'legacy' => 1,
'other' => 1,
'direct' => 1,
'indirect' => 1,
], [
'unsilenced' => true,
'self' => true,
'legacy' => true,
'other' => true,
'direct' => true,
'indirect' => true,
]];
}

private function buildGroups($counts)
{
$groups = [];
Expand Down
36 changes: 36 additions & 0 deletions Tests/DeprecationErrorHandler/partially_quiet2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
Test DeprecationErrorHandler quiet on everything but self/direct deprecations
--FILE--
<?php

$k = 'SYMFONY_DEPRECATIONS_HELPER';
putenv($k.'='.$_SERVER[$k] = $_ENV[$k] = 'max[self]=0&max[direct]=0&quiet[]=unsilenced&quiet[]=indirect&quiet[]=other');
putenv('ANSICON');
putenv('ConEmuANSI');
putenv('TERM');

$vendor = __DIR__;
while (!file_exists($vendor.'/vendor')) {
$vendor = dirname($vendor);
}
define('PHPUNIT_COMPOSER_INSTALL', $vendor.'/vendor/autoload.php');
require PHPUNIT_COMPOSER_INSTALL;
require_once __DIR__.'/../../bootstrap.php';
require __DIR__.'/fake_vendor/autoload.php';
require __DIR__.'/fake_vendor/acme/lib/deprecation_riddled.php';
require __DIR__.'/fake_vendor/acme/outdated-lib/outdated_file.php';

?>
--EXPECTF--
Unsilenced deprecation notices (3)

Remaining direct deprecation notices (2)

1x: root deprecation

1x: silenced bar deprecation
1x in FooTestCase::testNonLegacyBar

Remaining indirect deprecation notices (1)

Legacy deprecation notices (2)

0 comments on commit 2232d32

Please sign in to comment.