Skip to content

Commit

Permalink
Merge branch '10.5' into 11.3
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Sep 3, 2024
2 parents 4b40a6a + eae5514 commit 1410b38
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 2 deletions.
4 changes: 4 additions & 0 deletions ChangeLog-11.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes of the PHPUnit 11.3 release series are documented in this fi

## [11.3.2] - 2024-MM-DD

### Fixed

* [#5884](https://github.com/sebastianbergmann/phpunit/issues/5884): TestDox printer does not consider that issues can be suppressed by attribute, baseline, source location, or `@` operator

### Changed

* [#5931](https://github.com/sebastianbergmann/phpunit/pull/5931): `name` property on `<testsuites>` element in JUnit XML logfile
Expand Down
88 changes: 87 additions & 1 deletion src/Logging/TestDox/TestResult/TestResultCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
use PHPUnit\Event\UnknownSubscriberTypeException;
use PHPUnit\Framework\TestStatus\TestStatus;
use PHPUnit\Logging\TestDox\TestResult as TestDoxTestMethod;
use PHPUnit\TextUI\Configuration\Source;
use PHPUnit\TextUI\Configuration\SourceFilter;
use ReflectionMethod;

/**
Expand All @@ -50,6 +52,8 @@
*/
final class TestResultCollector
{
private readonly Source $source;

/**
* @var array<string, list<TestDoxTestMethod>>
*/
Expand All @@ -62,8 +66,10 @@ final class TestResultCollector
* @throws EventFacadeIsSealedException
* @throws UnknownSubscriberTypeException
*/
public function __construct(Facade $facade)
public function __construct(Facade $facade, Source $source)
{
$this->source = $source;

$this->registerSubscribers($facade);
}

Expand Down Expand Up @@ -214,6 +220,22 @@ public function testTriggeredDeprecation(DeprecationTriggered $event): void
return;
}

if ($event->ignoredByTest()) {
return;
}

if ($event->ignoredByBaseline()) {
return;
}

if (!$this->source->ignoreSuppressionOfDeprecations() && $event->wasSuppressed()) {
return;
}

if ($this->source->restrictDeprecations() && !(new SourceFilter)->includes($this->source, $event->file())) {
return;
}

$this->updateTestStatus(TestStatus::deprecation());
}

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

if ($event->ignoredByBaseline()) {
return;
}

if (!$this->source->ignoreSuppressionOfNotices() && $event->wasSuppressed()) {
return;
}

if ($this->source->restrictNotices() && !(new SourceFilter)->includes($this->source, $event->file())) {
return;
}

$this->updateTestStatus(TestStatus::notice());
}

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

if ($event->ignoredByBaseline()) {
return;
}

if (!$this->source->ignoreSuppressionOfWarnings() && $event->wasSuppressed()) {
return;
}

if ($this->source->restrictWarnings() && !(new SourceFilter)->includes($this->source, $event->file())) {
return;
}

$this->updateTestStatus(TestStatus::warning());
}

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

if ($event->ignoredByTest()) {
return;
}

if ($event->ignoredByBaseline()) {
return;
}

if (!$this->source->ignoreSuppressionOfPhpDeprecations() && $event->wasSuppressed()) {
return;
}

if ($this->source->restrictDeprecations() && !(new SourceFilter)->includes($this->source, $event->file())) {
return;
}

$this->updateTestStatus(TestStatus::deprecation());
}

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

if ($event->ignoredByBaseline()) {
return;
}

if (!$this->source->ignoreSuppressionOfPhpNotices() && $event->wasSuppressed()) {
return;
}

if ($this->source->restrictNotices() && !(new SourceFilter)->includes($this->source, $event->file())) {
return;
}

$this->updateTestStatus(TestStatus::notice());
}

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

if ($event->ignoredByBaseline()) {
return;
}

if (!$this->source->ignoreSuppressionOfPhpWarnings() && $event->wasSuppressed()) {
return;
}

if ($this->source->restrictWarnings() && !(new SourceFilter)->includes($this->source, $event->file())) {
return;
}

$this->updateTestStatus(TestStatus::warning());
}

Expand Down
5 changes: 4 additions & 1 deletion src/TextUI/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,10 @@ private function testDoxResultCollector(Configuration $configuration): ?TestDoxR
if ($configuration->hasLogfileTestdoxHtml() ||
$configuration->hasLogfileTestdoxText() ||
$configuration->outputIsTestDox()) {
return new TestDoxResultCollector(EventFacade::instance());
return new TestDoxResultCollector(
EventFacade::instance(),
$configuration->source(),
);
}

return null;
Expand Down
41 changes: 41 additions & 0 deletions tests/end-to-end/regression/5884.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
https://github.com/sebastianbergmann/phpunit/issues/5884
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--configuration';
$_SERVER['argv'][] = __DIR__ . '/5884';
$_SERVER['argv'][] = '--no-progress';
$_SERVER['argv'][] = '--testdox';

require_once __DIR__ . '/../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Runtime: %s
Configuration: %s

Time: %s, Memory: %s

Foo (PHPUnit\TestFixture\Issue5884\Foo)
Expect user deprecation message n o t ignoring deprecations
Expect user deprecation message a n d ignoring deprecations
Pcre has utf 8 support
Stream to non writable file with p h p unit error handler
Stream to non writable file without p h p unit error handler
Stream to invalid file

1 test triggered 1 deprecation:

1) %sFooTest.php:33
foo

Triggered by:

* PHPUnit\TestFixture\Issue5884\FooTest::testExpectUserDeprecationMessageNOTIgnoringDeprecations
%sFooTest.php:29

OK, but there were issues!
Tests: 6, Assertions: 7, Deprecations: 1.
21 changes: 21 additions & 0 deletions tests/end-to-end/regression/5884/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../phpunit.xsd"
bootstrap="src/Foo.php"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnIncompleteTests="true"
displayDetailsOnSkippedTests="true"
failOnWarning="true"
failOnNotice="true"
failOnDeprecation="true"
>
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
42 changes: 42 additions & 0 deletions tests/end-to-end/regression/5884/src/Foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture\Issue5884;

use function error_get_last;
use function fopen;
use function is_array;
use function preg_match;
use Exception;

final class Foo
{
public static function pcreHasUtf8Support()
{
// This regex deliberately has a compile error to demonstrate the issue.
return (bool) @preg_match('/^.[/u', 'a');
}

public static function openFile($filename): void
{
// Silenced the PHP native warning in favour of throwing an exception.
$download = @fopen($filename, 'wb');

if ($download === false) {
$error = error_get_last();

if (!is_array($error)) {
// Shouldn't be possible, but can happen in test situations.
$error = ['message' => 'Failed to open stream'];
}

throw new Exception($error['message']);
}
}
}
107 changes: 107 additions & 0 deletions tests/end-to-end/regression/5884/tests/FooTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture\Issue5884;

use const E_USER_DEPRECATED;
use function chmod;
use function file_get_contents;
use function file_put_contents;
use function sys_get_temp_dir;
use function tempnam;
use function trigger_error;
use function unlink;
use Exception;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\Attributes\RequiresPhpunit;
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
use PHPUnit\Framework\TestCase;

final class FooTest extends TestCase
{
#[RequiresPhpunit('^11.0')]
public function testExpectUserDeprecationMessageNOTIgnoringDeprecations(): void
{
$this->expectUserDeprecationMessage('foo');

trigger_error('foo', E_USER_DEPRECATED);
}

#[RequiresPhpunit('^11.0')]
#[IgnoreDeprecations]
public function testExpectUserDeprecationMessageANDIgnoringDeprecations(): void
{
$this->expectUserDeprecationMessage('foo');

trigger_error('foo', E_USER_DEPRECATED);
}

public function testPcreHasUtf8Support(): void
{
$this->assertIsBool(Foo::pcreHasUtf8Support());
}

public function testStreamToNonWritableFileWithPHPUnitErrorHandler(): void
{
// Create an unwritable file.
$filename = tempnam(sys_get_temp_dir(), 'RLT');

if (file_put_contents($filename, 'foo')) {
chmod($filename, 0o444);
}

try {
Foo::openFile($filename);
} catch (Exception $e) {
// This "Failed to open stream" exception is expected.
}

// Now verify the original file is unchanged.
$contents = file_get_contents($filename);
$this->assertSame('foo', $contents);

chmod($filename, 0o755);
unlink($filename);
}

#[WithoutErrorHandler]
public function testStreamToNonWritableFileWithoutPHPUnitErrorHandler(): void
{
// Create an unwritable file.
$filename = tempnam(sys_get_temp_dir(), 'RLT');

if (file_put_contents($filename, 'foo')) {
chmod($filename, 0o444);
}

try {
Foo::openFile($filename);
} catch (Exception $e) {
// This "Failed to open stream" exception is expected.
}

// Now verify the original file is unchanged.
$contents = file_get_contents($filename);
$this->assertSame('foo', $contents);

chmod($filename, 0o755);
unlink($filename);
}

public function testStreamToInvalidFile(): void
{
$filename = tempnam(sys_get_temp_dir(), 'RLT') . '/missing/directory';

$this->expectException(Exception::class);
// First character (F) can be upper or lowercase depending on PHP version.
$this->expectExceptionMessage('ailed to open stream');

Foo::openFile($filename);
}
}

0 comments on commit 1410b38

Please sign in to comment.