Skip to content

Commit

Permalink
Closes #6028
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Nov 25, 2024
1 parent c9cfb9e commit 3cefce3
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 4 deletions.
6 changes: 6 additions & 0 deletions ChangeLog-11.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes of the PHPUnit 11.4 release series are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.

## [11.4.4] - 2024-MM-DD

### Fixed

* [#6028](https://github.com/sebastianbergmann/phpunit/issues/6028): `ignoreIndirectDeprecations=true` suppresses deprecations triggered in test code

## [11.4.3] - 2024-10-28

### Changed
Expand Down
15 changes: 15 additions & 0 deletions src/Event/Value/Test/Issue/IssueTrigger.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
*/
abstract class IssueTrigger
{
public static function test(): TestTrigger
{
return new TestTrigger;
}

public static function self(): SelfTrigger
{
return new SelfTrigger;
Expand All @@ -40,6 +45,16 @@ final private function __construct()
{
}

/**
* Your test code triggers an issue.
*
* @phpstan-assert-if-true TestTrigger $this
*/
public function isTest(): bool
{
return false;
}

/**
* Your own code triggers an issue in your own code.
*
Expand Down
39 changes: 39 additions & 0 deletions src/Event/Value/Test/Issue/TestTrigger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\Event\Code\IssueTrigger;

/**
* @immutable
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*/
final class TestTrigger extends IssueTrigger
{
/**
* Your test code triggers an issue.
*/
public function isTest(): true
{
return true;
}

/**
* Your own code triggers an issue in your own code.
*/
public function isSelf(): true
{
return true;
}

public function asString(): string
{
return 'issue triggered by test code';
}
}
12 changes: 8 additions & 4 deletions src/Runner/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,14 @@ private function trigger(TestMethod $test, bool $filterTrigger): IssueTrigger
$triggeredInFirstPartyCode = false;
$triggerCalledFromFirstPartyCode = false;

if (isset($trace[0]['file']) &&
($trace[0]['file'] === $test->file() ||
$this->sourceFilter->includes($this->source, $trace[0]['file']))) {
$triggeredInFirstPartyCode = true;
if (isset($trace[0]['file'])) {
if ($trace[0]['file'] === $test->file()) {
return IssueTrigger::test();
}

if ($this->sourceFilter->includes($this->source, $trace[0]['file'])) {
$triggeredInFirstPartyCode = true;
}
}

if (isset($trace[1]['file']) &&
Expand Down
34 changes: 34 additions & 0 deletions tests/end-to-end/regression/6028.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
https://github.com/sebastianbergmann/phpunit/issues/6028
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--display-deprecations';
$_SERVER['argv'][] = '--configuration';
$_SERVER['argv'][] = __DIR__ . '/6028';

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

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

Runtime: %s
Configuration: %sphpunit.xml

D 1 / 1 (100%)

Time: %s, Memory: %s

1 test triggered 1 deprecation:

1) %sIssue6028Test.php:20
message

Triggered by:

* PHPUnit\TestFixture\Issue6028\Issue6028Test::testOne
%sIssue6028Test.php:18

OK, but there were issues!
Tests: 1, Assertions: 1, Deprecations: 1.
16 changes: 16 additions & 0 deletions tests/end-to-end/regression/6028/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../phpunit.xsd"
>
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>

<source ignoreIndirectDeprecations="true" restrictNotices="true" restrictWarnings="true">
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
Empty file.
24 changes: 24 additions & 0 deletions tests/end-to-end/regression/6028/tests/Issue6028Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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\Issue6028;

use const E_USER_DEPRECATED;
use function trigger_error;
use PHPUnit\Framework\TestCase;

final class Issue6028Test extends TestCase
{
public function testOne(): void
{
trigger_error('message', E_USER_DEPRECATED);

$this->assertTrue(true);
}
}
16 changes: 16 additions & 0 deletions tests/unit/Event/Value/Test/IssueTriggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,24 @@
#[Small]
final class IssueTriggerTest extends TestCase
{
public function testCanBeTest(): void
{
$trigger = IssueTrigger::test();

$this->assertTrue($trigger->isTest());
$this->assertTrue($trigger->isSelf());
$this->assertFalse($trigger->isDirect());
$this->assertFalse($trigger->isIndirect());
$this->assertFalse($trigger->isUnknown());
$this->assertSame('issue triggered by test code', $trigger->asString());
}

public function testCanBeSelf(): void
{
$trigger = IssueTrigger::self();

$this->assertTrue($trigger->isSelf());
$this->assertFalse($trigger->isTest());
$this->assertFalse($trigger->isDirect());
$this->assertFalse($trigger->isIndirect());
$this->assertFalse($trigger->isUnknown());
Expand All @@ -36,6 +49,7 @@ public function testCanBeDirect(): void
$trigger = IssueTrigger::direct();

$this->assertTrue($trigger->isDirect());
$this->assertFalse($trigger->isTest());
$this->assertFalse($trigger->isSelf());
$this->assertFalse($trigger->isIndirect());
$this->assertFalse($trigger->isUnknown());
Expand All @@ -47,6 +61,7 @@ public function testCanBeIndirect(): void
$trigger = IssueTrigger::indirect();

$this->assertTrue($trigger->isIndirect());
$this->assertFalse($trigger->isTest());
$this->assertFalse($trigger->isSelf());
$this->assertFalse($trigger->isDirect());
$this->assertFalse($trigger->isUnknown());
Expand All @@ -57,6 +72,7 @@ public function testCanBeUnknown(): void
{
$trigger = IssueTrigger::unknown();

$this->assertFalse($trigger->isTest());
$this->assertFalse($trigger->isSelf());
$this->assertFalse($trigger->isDirect());
$this->assertFalse($trigger->isIndirect());
Expand Down

0 comments on commit 3cefce3

Please sign in to comment.