Skip to content

Commit b9ee8a6

Browse files
Merge branch '11.3'
2 parents 5395456 + 1410b38 commit b9ee8a6

File tree

6 files changed

+302
-2
lines changed

6 files changed

+302
-2
lines changed

src/Logging/TestDox/TestResult/TestResultCollector.php

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
use PHPUnit\Event\UnknownSubscriberTypeException;
4242
use PHPUnit\Framework\TestStatus\TestStatus;
4343
use PHPUnit\Logging\TestDox\TestResult as TestDoxTestMethod;
44+
use PHPUnit\TextUI\Configuration\Source;
45+
use PHPUnit\TextUI\Configuration\SourceFilter;
4446
use ReflectionMethod;
4547

4648
/**
@@ -50,6 +52,8 @@
5052
*/
5153
final class TestResultCollector
5254
{
55+
private readonly Source $source;
56+
5357
/**
5458
* @var array<string, list<TestDoxTestMethod>>
5559
*/
@@ -62,8 +66,10 @@ final class TestResultCollector
6266
* @throws EventFacadeIsSealedException
6367
* @throws UnknownSubscriberTypeException
6468
*/
65-
public function __construct(Facade $facade)
69+
public function __construct(Facade $facade, Source $source)
6670
{
71+
$this->source = $source;
72+
6773
$this->registerSubscribers($facade);
6874
}
6975

@@ -214,6 +220,22 @@ public function testTriggeredDeprecation(DeprecationTriggered $event): void
214220
return;
215221
}
216222

223+
if ($event->ignoredByTest()) {
224+
return;
225+
}
226+
227+
if ($event->ignoredByBaseline()) {
228+
return;
229+
}
230+
231+
if (!$this->source->ignoreSuppressionOfDeprecations() && $event->wasSuppressed()) {
232+
return;
233+
}
234+
235+
if ($this->source->restrictDeprecations() && !(new SourceFilter)->includes($this->source, $event->file())) {
236+
return;
237+
}
238+
217239
$this->updateTestStatus(TestStatus::deprecation());
218240
}
219241

@@ -223,6 +245,18 @@ public function testTriggeredNotice(NoticeTriggered $event): void
223245
return;
224246
}
225247

248+
if ($event->ignoredByBaseline()) {
249+
return;
250+
}
251+
252+
if (!$this->source->ignoreSuppressionOfNotices() && $event->wasSuppressed()) {
253+
return;
254+
}
255+
256+
if ($this->source->restrictNotices() && !(new SourceFilter)->includes($this->source, $event->file())) {
257+
return;
258+
}
259+
226260
$this->updateTestStatus(TestStatus::notice());
227261
}
228262

@@ -232,6 +266,18 @@ public function testTriggeredWarning(WarningTriggered $event): void
232266
return;
233267
}
234268

269+
if ($event->ignoredByBaseline()) {
270+
return;
271+
}
272+
273+
if (!$this->source->ignoreSuppressionOfWarnings() && $event->wasSuppressed()) {
274+
return;
275+
}
276+
277+
if ($this->source->restrictWarnings() && !(new SourceFilter)->includes($this->source, $event->file())) {
278+
return;
279+
}
280+
235281
$this->updateTestStatus(TestStatus::warning());
236282
}
237283

@@ -241,6 +287,22 @@ public function testTriggeredPhpDeprecation(PhpDeprecationTriggered $event): voi
241287
return;
242288
}
243289

290+
if ($event->ignoredByTest()) {
291+
return;
292+
}
293+
294+
if ($event->ignoredByBaseline()) {
295+
return;
296+
}
297+
298+
if (!$this->source->ignoreSuppressionOfPhpDeprecations() && $event->wasSuppressed()) {
299+
return;
300+
}
301+
302+
if ($this->source->restrictDeprecations() && !(new SourceFilter)->includes($this->source, $event->file())) {
303+
return;
304+
}
305+
244306
$this->updateTestStatus(TestStatus::deprecation());
245307
}
246308

@@ -250,6 +312,18 @@ public function testTriggeredPhpNotice(PhpNoticeTriggered $event): void
250312
return;
251313
}
252314

315+
if ($event->ignoredByBaseline()) {
316+
return;
317+
}
318+
319+
if (!$this->source->ignoreSuppressionOfPhpNotices() && $event->wasSuppressed()) {
320+
return;
321+
}
322+
323+
if ($this->source->restrictNotices() && !(new SourceFilter)->includes($this->source, $event->file())) {
324+
return;
325+
}
326+
253327
$this->updateTestStatus(TestStatus::notice());
254328
}
255329

@@ -259,6 +333,18 @@ public function testTriggeredPhpWarning(PhpWarningTriggered $event): void
259333
return;
260334
}
261335

336+
if ($event->ignoredByBaseline()) {
337+
return;
338+
}
339+
340+
if (!$this->source->ignoreSuppressionOfPhpWarnings() && $event->wasSuppressed()) {
341+
return;
342+
}
343+
344+
if ($this->source->restrictWarnings() && !(new SourceFilter)->includes($this->source, $event->file())) {
345+
return;
346+
}
347+
262348
$this->updateTestStatus(TestStatus::warning());
263349
}
264350

src/TextUI/Application.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,10 @@ private function testDoxResultCollector(Configuration $configuration): ?TestDoxR
673673
if ($configuration->hasLogfileTestdoxHtml() ||
674674
$configuration->hasLogfileTestdoxText() ||
675675
$configuration->outputIsTestDox()) {
676-
return new TestDoxResultCollector(EventFacade::instance());
676+
return new TestDoxResultCollector(
677+
EventFacade::instance(),
678+
$configuration->source(),
679+
);
677680
}
678681

679682
return null;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
https://github.com/sebastianbergmann/phpunit/issues/5884
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--configuration';
7+
$_SERVER['argv'][] = __DIR__ . '/5884';
8+
$_SERVER['argv'][] = '--no-progress';
9+
$_SERVER['argv'][] = '--testdox';
10+
11+
require_once __DIR__ . '/../../bootstrap.php';
12+
13+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
14+
--EXPECTF--
15+
PHPUnit %s by Sebastian Bergmann and contributors.
16+
17+
Runtime: %s
18+
Configuration: %s
19+
20+
Time: %s, Memory: %s
21+
22+
Foo (PHPUnit\TestFixture\Issue5884\Foo)
23+
⚠ Expect user deprecation message n o t ignoring deprecations
24+
✔ Expect user deprecation message a n d ignoring deprecations
25+
✔ Pcre has utf 8 support
26+
✔ Stream to non writable file with p h p unit error handler
27+
✔ Stream to non writable file without p h p unit error handler
28+
✔ Stream to invalid file
29+
30+
1 test triggered 1 deprecation:
31+
32+
1) %sFooTest.php:33
33+
foo
34+
35+
Triggered by:
36+
37+
* PHPUnit\TestFixture\Issue5884\FooTest::testExpectUserDeprecationMessageNOTIgnoringDeprecations
38+
%sFooTest.php:29
39+
40+
OK, but there were issues!
41+
Tests: 6, Assertions: 7, Deprecations: 1.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="../../../../phpunit.xsd"
5+
bootstrap="src/Foo.php"
6+
displayDetailsOnTestsThatTriggerErrors="true"
7+
displayDetailsOnTestsThatTriggerWarnings="true"
8+
displayDetailsOnTestsThatTriggerNotices="true"
9+
displayDetailsOnTestsThatTriggerDeprecations="true"
10+
displayDetailsOnIncompleteTests="true"
11+
displayDetailsOnSkippedTests="true"
12+
failOnWarning="true"
13+
failOnNotice="true"
14+
failOnDeprecation="true"
15+
>
16+
<testsuites>
17+
<testsuite name="default">
18+
<directory>tests</directory>
19+
</testsuite>
20+
</testsuites>
21+
</phpunit>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture\Issue5884;
11+
12+
use function error_get_last;
13+
use function fopen;
14+
use function is_array;
15+
use function preg_match;
16+
use Exception;
17+
18+
final class Foo
19+
{
20+
public static function pcreHasUtf8Support()
21+
{
22+
// This regex deliberately has a compile error to demonstrate the issue.
23+
return (bool) @preg_match('/^.[/u', 'a');
24+
}
25+
26+
public static function openFile($filename): void
27+
{
28+
// Silenced the PHP native warning in favour of throwing an exception.
29+
$download = @fopen($filename, 'wb');
30+
31+
if ($download === false) {
32+
$error = error_get_last();
33+
34+
if (!is_array($error)) {
35+
// Shouldn't be possible, but can happen in test situations.
36+
$error = ['message' => 'Failed to open stream'];
37+
}
38+
39+
throw new Exception($error['message']);
40+
}
41+
}
42+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture\Issue5884;
11+
12+
use const E_USER_DEPRECATED;
13+
use function chmod;
14+
use function file_get_contents;
15+
use function file_put_contents;
16+
use function sys_get_temp_dir;
17+
use function tempnam;
18+
use function trigger_error;
19+
use function unlink;
20+
use Exception;
21+
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
22+
use PHPUnit\Framework\Attributes\RequiresPhpunit;
23+
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
24+
use PHPUnit\Framework\TestCase;
25+
26+
final class FooTest extends TestCase
27+
{
28+
#[RequiresPhpunit('^11.0')]
29+
public function testExpectUserDeprecationMessageNOTIgnoringDeprecations(): void
30+
{
31+
$this->expectUserDeprecationMessage('foo');
32+
33+
trigger_error('foo', E_USER_DEPRECATED);
34+
}
35+
36+
#[RequiresPhpunit('^11.0')]
37+
#[IgnoreDeprecations]
38+
public function testExpectUserDeprecationMessageANDIgnoringDeprecations(): void
39+
{
40+
$this->expectUserDeprecationMessage('foo');
41+
42+
trigger_error('foo', E_USER_DEPRECATED);
43+
}
44+
45+
public function testPcreHasUtf8Support(): void
46+
{
47+
$this->assertIsBool(Foo::pcreHasUtf8Support());
48+
}
49+
50+
public function testStreamToNonWritableFileWithPHPUnitErrorHandler(): void
51+
{
52+
// Create an unwritable file.
53+
$filename = tempnam(sys_get_temp_dir(), 'RLT');
54+
55+
if (file_put_contents($filename, 'foo')) {
56+
chmod($filename, 0o444);
57+
}
58+
59+
try {
60+
Foo::openFile($filename);
61+
} catch (Exception $e) {
62+
// This "Failed to open stream" exception is expected.
63+
}
64+
65+
// Now verify the original file is unchanged.
66+
$contents = file_get_contents($filename);
67+
$this->assertSame('foo', $contents);
68+
69+
chmod($filename, 0o755);
70+
unlink($filename);
71+
}
72+
73+
#[WithoutErrorHandler]
74+
public function testStreamToNonWritableFileWithoutPHPUnitErrorHandler(): void
75+
{
76+
// Create an unwritable file.
77+
$filename = tempnam(sys_get_temp_dir(), 'RLT');
78+
79+
if (file_put_contents($filename, 'foo')) {
80+
chmod($filename, 0o444);
81+
}
82+
83+
try {
84+
Foo::openFile($filename);
85+
} catch (Exception $e) {
86+
// This "Failed to open stream" exception is expected.
87+
}
88+
89+
// Now verify the original file is unchanged.
90+
$contents = file_get_contents($filename);
91+
$this->assertSame('foo', $contents);
92+
93+
chmod($filename, 0o755);
94+
unlink($filename);
95+
}
96+
97+
public function testStreamToInvalidFile(): void
98+
{
99+
$filename = tempnam(sys_get_temp_dir(), 'RLT') . '/missing/directory';
100+
101+
$this->expectException(Exception::class);
102+
// First character (F) can be upper or lowercase depending on PHP version.
103+
$this->expectExceptionMessage('ailed to open stream');
104+
105+
Foo::openFile($filename);
106+
}
107+
}

0 commit comments

Comments
 (0)