Skip to content

Commit

Permalink
Add issue trigger to DeprecationTriggered and PhpDeprecationTriggered…
Browse files Browse the repository at this point in the history
… events
  • Loading branch information
sebastianbergmann committed Mar 18, 2024
1 parent f9ca7c5 commit ffc0078
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 59 deletions.
7 changes: 5 additions & 2 deletions src/Event/Emitter/DispatchingEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use function assert;
use PHPUnit\Event\Code\ClassMethod;
use PHPUnit\Event\Code\ComparisonFailure;
use PHPUnit\Event\Code\IssueTrigger\IssueTrigger;
use PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException;
use PHPUnit\Event\Code\TestMethod;
use PHPUnit\Event\Code\TestMethodBuilder;
Expand Down Expand Up @@ -746,7 +747,7 @@ public function testTriggeredPhpunitDeprecation(?Code\Test $test, string $messag
* @throws InvalidArgumentException
* @throws UnknownEventTypeException
*/
public function testTriggeredPhpDeprecation(Code\Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest): void
public function testTriggeredPhpDeprecation(Code\Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest, IssueTrigger $trigger): void
{
$this->dispatcher->dispatch(
new Test\PhpDeprecationTriggered(
Expand All @@ -758,6 +759,7 @@ public function testTriggeredPhpDeprecation(Code\Test $test, string $message, st
$suppressed,
$ignoredByBaseline,
$ignoredByTest,
$trigger,
),
);
}
Expand All @@ -770,7 +772,7 @@ public function testTriggeredPhpDeprecation(Code\Test $test, string $message, st
* @throws InvalidArgumentException
* @throws UnknownEventTypeException
*/
public function testTriggeredDeprecation(Code\Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest): void
public function testTriggeredDeprecation(Code\Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest, IssueTrigger $trigger): void
{
$this->dispatcher->dispatch(
new Test\DeprecationTriggered(
Expand All @@ -782,6 +784,7 @@ public function testTriggeredDeprecation(Code\Test $test, string $message, strin
$suppressed,
$ignoredByBaseline,
$ignoredByTest,
$trigger,
),
);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Event/Emitter/Emitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use PHPUnit\Event\Code\ClassMethod;
use PHPUnit\Event\Code\ComparisonFailure;
use PHPUnit\Event\Code\IssueTrigger\IssueTrigger;
use PHPUnit\Event\Code\Throwable;
use PHPUnit\Event\TestSuite\TestSuite;
use PHPUnit\TextUI\Configuration\Configuration;
Expand Down Expand Up @@ -178,14 +179,14 @@ public function testTriggeredPhpunitDeprecation(?Code\Test $test, string $messag
* @psalm-param non-empty-string $file
* @psalm-param positive-int $line
*/
public function testTriggeredPhpDeprecation(Code\Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest): void;
public function testTriggeredPhpDeprecation(Code\Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest, IssueTrigger $trigger): void;

/**
* @psalm-param non-empty-string $message
* @psalm-param non-empty-string $file
* @psalm-param positive-int $line
*/
public function testTriggeredDeprecation(Code\Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest): void;
public function testTriggeredDeprecation(Code\Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest, IssueTrigger $trigger): void;

/**
* @psalm-param non-empty-string $message
Expand Down
12 changes: 10 additions & 2 deletions src/Event/Events/Test/Issue/DeprecationTriggered.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use const PHP_EOL;
use function implode;
use function sprintf;
use PHPUnit\Event\Code\IssueTrigger\IssueTrigger;
use PHPUnit\Event\Code\Test;
use PHPUnit\Event\Event;
use PHPUnit\Event\Telemetry;
Expand Down Expand Up @@ -43,13 +44,14 @@
private bool $suppressed;
private bool $ignoredByBaseline;
private bool $ignoredByTest;
private IssueTrigger $trigger;

/**
* @psalm-param non-empty-string $message
* @psalm-param non-empty-string $file
* @psalm-param positive-int $line
*/
public function __construct(Telemetry\Info $telemetryInfo, Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest)
public function __construct(Telemetry\Info $telemetryInfo, Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest, IssueTrigger $trigger)
{
$this->telemetryInfo = $telemetryInfo;
$this->test = $test;
Expand All @@ -59,6 +61,7 @@ public function __construct(Telemetry\Info $telemetryInfo, Test $test, string $m
$this->suppressed = $suppressed;
$this->ignoredByBaseline = $ignoredByBaseline;
$this->ignoredByTest = $ignoredByTest;
$this->trigger = $trigger;
}

public function telemetryInfo(): Telemetry\Info
Expand Down Expand Up @@ -110,6 +113,11 @@ public function ignoredByTest(): bool
return $this->ignoredByTest;
}

public function trigger(): IssueTrigger
{
return $this->trigger;
}

public function asString(): string
{
$message = $this->message;
Expand All @@ -118,7 +126,7 @@ public function asString(): string
$message = PHP_EOL . $message;
}

$details = [$this->test->id()];
$details = [$this->test->id(), $this->trigger->asString()];

if ($this->suppressed) {
$details[] = 'suppressed using operator';
Expand Down
12 changes: 10 additions & 2 deletions src/Event/Events/Test/Issue/PhpDeprecationTriggered.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use const PHP_EOL;
use function implode;
use function sprintf;
use PHPUnit\Event\Code\IssueTrigger\IssueTrigger;
use PHPUnit\Event\Code\Test;
use PHPUnit\Event\Event;
use PHPUnit\Event\Telemetry;
Expand Down Expand Up @@ -43,13 +44,14 @@
private bool $suppressed;
private bool $ignoredByBaseline;
private bool $ignoredByTest;
private IssueTrigger $trigger;

/**
* @psalm-param non-empty-string $message
* @psalm-param non-empty-string $file
* @psalm-param positive-int $line
*/
public function __construct(Telemetry\Info $telemetryInfo, Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest)
public function __construct(Telemetry\Info $telemetryInfo, Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest, IssueTrigger $trigger)
{
$this->telemetryInfo = $telemetryInfo;
$this->test = $test;
Expand All @@ -59,6 +61,7 @@ public function __construct(Telemetry\Info $telemetryInfo, Test $test, string $m
$this->suppressed = $suppressed;
$this->ignoredByBaseline = $ignoredByBaseline;
$this->ignoredByTest = $ignoredByTest;
$this->trigger = $trigger;
}

public function telemetryInfo(): Telemetry\Info
Expand Down Expand Up @@ -110,6 +113,11 @@ public function ignoredByTest(): bool
return $this->ignoredByTest;
}

public function trigger(): IssueTrigger
{
return $this->trigger;
}

public function asString(): string
{
$message = $this->message;
Expand All @@ -118,7 +126,7 @@ public function asString(): string
$message = PHP_EOL . $message;
}

$details = [$this->test->id()];
$details = [$this->test->id(), $this->trigger->asString()];

if ($this->suppressed) {
$details[] = 'suppressed using operator';
Expand Down
71 changes: 39 additions & 32 deletions src/Runner/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
use function restore_error_handler;
use function set_error_handler;
use PHPUnit\Event;
use PHPUnit\Event\Code\IssueTrigger\IssueTrigger;
use PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException;
use PHPUnit\Event\Code\TestMethod;
use PHPUnit\Runner\Baseline\Baseline;
use PHPUnit\Runner\Baseline\Issue;
use PHPUnit\TextUI\Configuration\Registry;
Expand Down Expand Up @@ -80,38 +82,6 @@ public function __invoke(int $errorNumber, string $errorString, string $errorFil
$ignoredByBaseline = $this->ignoredByBaseline($errorFile, $errorLine, $errorString);
$ignoredByTest = $test->metadata()->isIgnoreDeprecations()->isNotEmpty();

$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);

assert(isset($trace[1]['file']));
assert(isset($trace[2]['file']));

$triggeredInFirstPartyCode = false;
$triggerCalledFromFirstPartyCode = false;

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

if ($trace[2]['file'] === $test->file() ||
$this->sourceFilter->includes($this->source, $trace[2]['file'])) {
$triggerCalledFromFirstPartyCode = true;
}

$self = false;
$direct = false;
$indirect = false;

if ($triggerCalledFromFirstPartyCode) {
if ($triggeredInFirstPartyCode) {
$self = true;
} else {
$direct = true;
}
} else {
$indirect = true;
}

switch ($errorNumber) {
case E_NOTICE:
case E_STRICT:
Expand Down Expand Up @@ -171,6 +141,7 @@ public function __invoke(int $errorNumber, string $errorString, string $errorFil
$suppressed,
$ignoredByBaseline,
$ignoredByTest,
$this->trigger($test),
);

break;
Expand All @@ -184,6 +155,7 @@ public function __invoke(int $errorNumber, string $errorString, string $errorFil
$suppressed,
$ignoredByBaseline,
$ignoredByTest,
$this->trigger($test),
);

break;
Expand Down Expand Up @@ -258,4 +230,39 @@ private function ignoredByBaseline(string $file, int $line, string $description)

return $this->baseline->has(Issue::from($file, $line, null, $description));
}

private function trigger(TestMethod $test): IssueTrigger
{
if (!$this->source->notEmpty()) {
return IssueTrigger::unknown();
}

$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 4);

assert(isset($trace[2]['file']));
assert(isset($trace[3]['file']));

$triggeredInFirstPartyCode = false;
$triggerCalledFromFirstPartyCode = false;

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

if ($trace[3]['file'] === $test->file() ||
$this->sourceFilter->includes($this->source, $trace[3]['file'])) {
$triggerCalledFromFirstPartyCode = true;
}

if ($triggerCalledFromFirstPartyCode) {
if ($triggeredInFirstPartyCode) {
return IssueTrigger::self();
}

return IssueTrigger::direct();
}

return IssueTrigger::indirect();
}
}
2 changes: 1 addition & 1 deletion tests/end-to-end/cli/fail-on/fail-on-deprecation.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Test Runner Execution Started (2 tests)
Test Suite Started (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest, 2 tests)
Test Preparation Started (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne)
Test Prepared (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne)
Test Triggered Deprecation (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne)
Test Triggered Deprecation (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne, unknown whether this issue was triggered in first-party or third-party code)
message
Test Passed (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne)
Test Finished (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne)
Expand Down
2 changes: 1 addition & 1 deletion tests/end-to-end/cli/stop-on/stop-on-deprecation.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Test Runner Execution Started (2 tests)
Test Suite Started (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest, 2 tests)
Test Preparation Started (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne)
Test Prepared (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne)
Test Triggered Deprecation (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne)
Test Triggered Deprecation (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne, unknown whether this issue was triggered in first-party or third-party code)
message
Test Passed (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne)
Test Finished (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ Test Runner Execution Started (4 tests)
Test Suite Started (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest, 4 tests)
Test Preparation Started (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOne)
Test Prepared (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOne)
Test Triggered Deprecation (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOne, ignored by test)
Test Triggered Deprecation (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOne, unknown whether this issue was triggered in first-party or third-party code, ignored by test)
message
Test Passed (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOne)
Test Finished (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOne)
Test Preparation Started (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwo)
Test Prepared (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwo)
Test Triggered Deprecation (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwo)
Test Triggered Deprecation (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwo, unknown whether this issue was triggered in first-party or third-party code)
message
Test Passed (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwo)
Test Finished (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwo)
Test Preparation Started (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOneErrorGetLast)
Test Prepared (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOneErrorGetLast)
Test Triggered Deprecation (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOneErrorGetLast, ignored by test)
Test Triggered Deprecation (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOneErrorGetLast, unknown whether this issue was triggered in first-party or third-party code, ignored by test)
message
Test Passed (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOneErrorGetLast)
Test Finished (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOneErrorGetLast)
Test Preparation Started (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwoErrorGetLast)
Test Prepared (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwoErrorGetLast)
Test Triggered Deprecation (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwoErrorGetLast)
Test Triggered Deprecation (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwoErrorGetLast, unknown whether this issue was triggered in first-party or third-party code)
message
Test Passed (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwoErrorGetLast)
Test Finished (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwoErrorGetLast)
Expand Down
4 changes: 2 additions & 2 deletions tests/end-to-end/event/php-deprecated.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ Test Runner Execution Started (1 test)
Test Suite Started (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest, 1 test)
Test Preparation Started (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest::testDeprecatedPhpFeature)
Test Prepared (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest::testDeprecatedPhpFeature)
Test Triggered PHP Deprecation (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest::testDeprecatedPhpFeature)
Test Triggered PHP Deprecation (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest::testDeprecatedPhpFeature, unknown whether this issue was triggered in first-party or third-party code)
strlen(): Passing null to parameter #1 ($string) of type string is deprecated
Test Triggered PHP Deprecation (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest::testDeprecatedPhpFeature, suppressed using operator)
Test Triggered PHP Deprecation (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest::testDeprecatedPhpFeature, unknown whether this issue was triggered in first-party or third-party code, suppressed using operator)
strlen(): Passing null to parameter #1 ($string) of type string is deprecated
Test Passed (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest::testDeprecatedPhpFeature)
Test Finished (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest::testDeprecatedPhpFeature)
Expand Down
6 changes: 3 additions & 3 deletions tests/end-to-end/event/user-deprecated.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ Test Runner Execution Started (2 tests)
Test Suite Started (PHPUnit\TestFixture\Event\DeprecatedFeatureTest, 2 tests)
Test Preparation Started (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedFeature)
Test Prepared (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedFeature)
Test Triggered Deprecation (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedFeature)
Test Triggered Deprecation (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedFeature, unknown whether this issue was triggered in first-party or third-party code)
message
Test Triggered Deprecation (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedFeature, suppressed using operator)
Test Triggered Deprecation (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedFeature, unknown whether this issue was triggered in first-party or third-party code, suppressed using operator)
message
Test Passed (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedFeature)
Test Finished (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedFeature)
Test Preparation Started (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedSuppressedErrorGetLast)
Test Prepared (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedSuppressedErrorGetLast)
Test Triggered Deprecation (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedSuppressedErrorGetLast, suppressed using operator)
Test Triggered Deprecation (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedSuppressedErrorGetLast, unknown whether this issue was triggered in first-party or third-party code, suppressed using operator)
message
Test Passed (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedSuppressedErrorGetLast)
Test Finished (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedSuppressedErrorGetLast)
Expand Down
Loading

0 comments on commit ffc0078

Please sign in to comment.