8000 [PhpUnitBridge] Use verbose deprecation output for quiet types only w… · symfony/symfony@8f3b28e · GitHub
[go: up one dir, main page]

Skip to content

Commit 8f3b28e

Browse files
committed
[PhpUnitBridge] Use verbose deprecation output for quiet types only when it reaches the threshold
1 parent e7ec8a6 commit 8f3b28e

File tree

4 files changed

+164
-5
lines changed

4 files changed

+164
-5
lines changed

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public function shutdown()
200200
// store failing status
201201
$isFailing = !$configuration->tolerates($this->deprecationGroups);
202202

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

205205
$this->resetDeprecationGroups();
206206

@@ -213,7 +213,7 @@ public function shutdown()
213213
}
214214

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

218218
if ($configuration->isGeneratingBaseline()) {
219219
$configuration->writeBaseline();
@@ -289,11 +289,10 @@ private static function colorize($str, $red)
289289
/**
290290
* @param string[] $groups
291291
* @param Configuration $configuration
292-
* @param bool $isFailing
293292
*
294293
* @throws \InvalidArgumentException
295294
*/
296-
private function displayDeprecations($groups, $configuration, $isFailing)
295+
private function displayDeprecations($groups, $configuration)
297296
{
298297
$cmp = function ($a, $b) {
299298
return $b->count() - $a->count();
@@ -320,7 +319,8 @@ private function displayDeprecations($groups, $configuration, $isFailing)
320319
fwrite($handle, "\n".self::colorize($deprecationGroupMessage, 'legacy' !== $group && 'indirect' !== $group)."\n");
321320
}
322321

323-
if ('legacy' !== $group && !$configuration->verboseOutput($group) && !$isFailing) {
322+
// Skip the verbose output if the group is quiet and not failing according to its threshold:
323+
if ('legacy' !== $group && !$configuration->verboseOutput($group) && $configuration->toleratesForGroup($group, $this->deprecationGroups)) {
324324
continue;
325325
}
326326
$notices = $this->deprecationGroups[$group]->notices();

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler/Configuration.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,32 @@ public function tolerates(array $deprecationGroups)
165165
return true;
166166
}
167167

168+
/**
169+
* @param array<string,DeprecationGroup> $deprecationGroups
170+
*
171+
* @return bool true if the threshold is not reached for the deprecation type nor for the total
172+
*/
173+
public function toleratesForGroup(string $groupName, array $deprecationGroups): bool
174+
{
175+
$grandTotal = 0;
176+
177+
foreach ($deprecationGroups as $type => $group) {
178+
if ('legacy' !== $type) {
179+
$grandTotal += $group->count();
180+
}
181+
}
182+
183+
if ($grandTotal > $this->thresholds['total']) {
184+
return false;
185+
}
186+
187+
if (\in_array($groupName, ['self', 'direct', 'indirect'], true) && $deprecationGroups[$groupName]->count() > $this->thresholds[$groupName]) {
188+
return false;
189+
}
190+
191+
return true;
192+
}
193+
168194
/**
169195
* @return bool
170196
*/

src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/ConfigurationTest.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,103 @@ public function testOutputIsNotVerboseInWeakMode()
234234
$this->assertFalse($configuration->verboseOutput('other'));
235235
}
236236

237+
/**
238+
* @dataProvider provideDataForToleratesForGroup
239+
*/
240+
public function testToleratesForIndividualGroups(string $deprecationsHelper, array $deprecationsPerType, array $expected)
241+
{
242+
$configuration = Configuration::fromUrlEncodedString($deprecationsHelper);
243+
244+
$groups = $this->buildGroups($deprecationsPerType);
245+
246+
foreach ($expected as $groupName => $tolerates) {
247+
$this->assertSame($tolerates, $configuration->toleratesForGroup($groupName, $groups), sprintf('Deprecation type "%s" is %s', $groupName, $tolerates ? 'tolerated' : 'not tolerated'));
248+
}
249+
}
250+
251+
public function provideDataForToleratesForGroup() {
252+
253+
yield 'total threshold not reached' => ['max[total]=1', [
254+
'unsilenced' => 0,
255+
'self' => 0,
256+
'legacy' => 1, // Legacy group is ignored in total threshold
257+
'other' => 0,
258+
'direct' => 1,
259+
'indirect' => 0,
260+
], [
261+
'unsilenced' => true,
262+
'self' => true,
263+
'legacy' => true,
264+
'other' => true,
265+
'direct' => true,
266+
'indirect' => true,
267+
]];
268+
269+
yield 'total threshold reached' => ['max[total]=1', [
270+
'unsilenced' => 0,
271+
'self' => 0,
272+
'legacy' => 1,
273+
'other' => 0,
274+
'direct' => 1,
275+
'indirect' => 1,
276+
], [
277+
'unsilenced' => false,
278+
'self' => false,
279+
'legacy' => false,
280+
'other' => false,
281+
'direct' => false,
282+
'indirect' => false,
283+
]];
284+
285+
yield 'direct threshold reached' => ['max[total]=99&max[direct]=0', [
286+
'unsilenced' => 0,
287+
'self' => 0,
288+
'legacy' => 1,
289+
'other' => 0,
290+
'direct' => 1,
291+
'indirect' => 1,
292+
], [
293+
'unsilenced' => true,
294+
'self' => true,
295+
'legacy' => true,
296+
'other' => true,
297+
'direct' => false,
298+
'indirect' => true,
299+
]];
300+
301+
yield 'indirect & self threshold reached' => ['max[total]=99&max[direct]=0&max[self]=0', [
302+
'unsilenced' => 0,
303+
'self' => 1,
304+
'legacy' => 1,
305+
'other' => 1,
306+
'direct' => 1,
307+
'indirect' => 1,
308+
], [
309+
'unsilenced' => true,
310+
'self' => false,
311+
'legacy' => true,
312+
'other' => true,
313+
'direct' => false,
314+
'indirect' => true,
315+
]];
316+
317+
yield 'indirect & self threshold not reached' => ['max[total]=99&max[direct]=2&max[self]=2', [
318+
'unsilenced' => 0,
319+
'self' => 1,
320+
'legacy' => 1,
321+
'other' => 1,
322+
'direct' => 1,
323+
'indirect' => 1,
324+
], [
325+
'unsilenced' => true,
326+
'self' => true,
327+
'legacy' => true,
328+
'other' => true,
329+
'direct' => true,
330+
'indirect' => true,
331+
]];
332+
}
333+
237334
private function buildGroups($counts)
238335
{
239336
$groups = [];
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
Test DeprecationErrorHandler quiet on everything but self/direct deprecations
3+
--FILE--
4+
<?php
5+
6+
$k = 'SYMFONY_DEPRECATIONS_HELPER';
7+
putenv($k.'='.$_SERVER[$k] = $_ENV[$k] = 'max[self]=0&max[direct]=0&quiet[]=unsilenced&quiet[]=indirect&quiet[]=other');
8+
putenv('ANSICON');
9+
putenv('ConEmuANSI');
10+
putenv('TERM');
11+
12+
$vendor = __DIR__;
13+
while (!file_exists($vendor.'/vendor')) {
14+
$vendor = dirname($vendor);
15+
}
16+
define('PHPUNIT_COMPOSER_INSTALL', $vendor.'/vendor/autoload.php');
17+
require PHPUNIT_COMPOSER_INSTALL;
18+
require_once __DIR__.'/../../bootstrap.php';
19+
require __DIR__.'/fake_vendor/autoload.php';
20+
require __DIR__.'/fake_vendor/acme/lib/deprecation_riddled.php';
21+
require __DIR__.'/fake_vendor/acme/outdated-lib/outdated_file.php';
22+
23+
?>
24+
--EXPECTF--
25+
Unsilenced deprecation notices (3)
26+
27+
Remaining direct deprecation notices (2)
28+
29+
1x: root deprecation
30+
31+
1x: silenced bar deprecation
32+
1x in FooTestCase::testNonLegacyBar
33+
34+
Remaining indirect deprecation notices (1)
35+
36+
Legacy deprecation notices (2)

0 commit comments

Comments
 (0)
0