Skip to content

Commit e957339

Browse files
Add issue trigger to DeprecationTriggered and PhpDeprecationTriggered events
1 parent e07e972 commit e957339

File tree

12 files changed

+100
-59
lines changed

12 files changed

+100
-59
lines changed

src/Event/Emitter/DispatchingEmitter.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use function assert;
1313
use PHPUnit\Event\Code\ClassMethod;
1414
use PHPUnit\Event\Code\ComparisonFailure;
15+
use PHPUnit\Event\Code\IssueTrigger\IssueTrigger;
1516
use PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException;
1617
use PHPUnit\Event\Code\TestMethod;
1718
use PHPUnit\Event\Code\TestMethodBuilder;
@@ -746,7 +747,7 @@ public function testTriggeredPhpunitDeprecation(?Code\Test $test, string $messag
746747
* @throws InvalidArgumentException
747748
* @throws UnknownEventTypeException
748749
*/
749-
public function testTriggeredPhpDeprecation(Code\Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest): void
750+
public function testTriggeredPhpDeprecation(Code\Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest, IssueTrigger $trigger): void
750751
{
751752
$this->dispatcher->dispatch(
752753
new Test\PhpDeprecationTriggered(
@@ -758,6 +759,7 @@ public function testTriggeredPhpDeprecation(Code\Test $test, string $message, st
758759
$suppressed,
759760
$ignoredByBaseline,
760761
$ignoredByTest,
762+
$trigger,
761763
),
762764
);
763765
}
@@ -770,7 +772,7 @@ public function testTriggeredPhpDeprecation(Code\Test $test, string $message, st
770772
* @throws InvalidArgumentException
771773
* @throws UnknownEventTypeException
772774
*/
773-
public function testTriggeredDeprecation(Code\Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest): void
775+
public function testTriggeredDeprecation(Code\Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest, IssueTrigger $trigger): void
774776
{
775777
$this->dispatcher->dispatch(
776778
new Test\DeprecationTriggered(
@@ -782,6 +784,7 @@ public function testTriggeredDeprecation(Code\Test $test, string $message, strin
782784
$suppressed,
783785
$ignoredByBaseline,
784786
$ignoredByTest,
787+
$trigger,
785788
),
786789
);
787790
}

src/Event/Emitter/Emitter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PHPUnit\Event\Code\ClassMethod;
1313
use PHPUnit\Event\Code\ComparisonFailure;
14+
use PHPUnit\Event\Code\IssueTrigger\IssueTrigger;
1415
use PHPUnit\Event\Code\Throwable;
1516
use PHPUnit\Event\TestSuite\TestSuite;
1617
use PHPUnit\TextUI\Configuration\Configuration;
@@ -178,14 +179,14 @@ public function testTriggeredPhpunitDeprecation(?Code\Test $test, string $messag
178179
* @psalm-param non-empty-string $file
179180
* @psalm-param positive-int $line
180181
*/
181-
public function testTriggeredPhpDeprecation(Code\Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest): void;
182+
public function testTriggeredPhpDeprecation(Code\Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest, IssueTrigger $trigger): void;
182183

183184
/**
184185
* @psalm-param non-empty-string $message
185186
* @psalm-param non-empty-string $file
186187
* @psalm-param positive-int $line
187188
*/
188-
public function testTriggeredDeprecation(Code\Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest): void;
189+
public function testTriggeredDeprecation(Code\Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest, IssueTrigger $trigger): void;
189190

190191
/**
191192
* @psalm-param non-empty-string $message

src/Event/Events/Test/Issue/DeprecationTriggered.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use const PHP_EOL;
1313
use function implode;
1414
use function sprintf;
15+
use PHPUnit\Event\Code\IssueTrigger\IssueTrigger;
1516
use PHPUnit\Event\Code\Test;
1617
use PHPUnit\Event\Event;
1718
use PHPUnit\Event\Telemetry;
@@ -43,13 +44,14 @@
4344
private bool $suppressed;
4445
private bool $ignoredByBaseline;
4546
private bool $ignoredByTest;
47+
private IssueTrigger $trigger;
4648

4749
/**
4850
* @psalm-param non-empty-string $message
4951
* @psalm-param non-empty-string $file
5052
* @psalm-param positive-int $line
5153
*/
52-
public function __construct(Telemetry\Info $telemetryInfo, Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest)
54+
public function __construct(Telemetry\Info $telemetryInfo, Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest, IssueTrigger $trigger)
5355
{
5456
$this->telemetryInfo = $telemetryInfo;
5557
$this->test = $test;
@@ -59,6 +61,7 @@ public function __construct(Telemetry\Info $telemetryInfo, Test $test, string $m
5961
$this->suppressed = $suppressed;
6062
$this->ignoredByBaseline = $ignoredByBaseline;
6163
$this->ignoredByTest = $ignoredByTest;
64+
$this->trigger = $trigger;
6265
}
6366

6467
public function telemetryInfo(): Telemetry\Info
@@ -110,6 +113,11 @@ public function ignoredByTest(): bool
110113
return $this->ignoredByTest;
111114
}
112115

116+
public function trigger(): IssueTrigger
117+
{
118+
return $this->trigger;
119+
}
120+
113121
public function asString(): string
114122
{
115123
$message = $this->message;
@@ -118,7 +126,7 @@ public function asString(): string
118126
$message = PHP_EOL . $message;
119127
}
120128

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

123131
if ($this->suppressed) {
124132
$details[] = 'suppressed using operator';

src/Event/Events/Test/Issue/PhpDeprecationTriggered.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use const PHP_EOL;
1313
use function implode;
1414
use function sprintf;
15+
use PHPUnit\Event\Code\IssueTrigger\IssueTrigger;
1516
use PHPUnit\Event\Code\Test;
1617
use PHPUnit\Event\Event;
1718
use PHPUnit\Event\Telemetry;
@@ -43,13 +44,14 @@
4344
private bool $suppressed;
4445
private bool $ignoredByBaseline;
4546
private bool $ignoredByTest;
47+
private IssueTrigger $trigger;
4648

4749
/**
4850
* @psalm-param non-empty-string $message
4951
* @psalm-param non-empty-string $file
5052
* @psalm-param positive-int $line
5153
*/
52-
public function __construct(Telemetry\Info $telemetryInfo, Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest)
54+
public function __construct(Telemetry\Info $telemetryInfo, Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest, IssueTrigger $trigger)
5355
{
5456
$this->telemetryInfo = $telemetryInfo;
5557
$this->test = $test;
@@ -59,6 +61,7 @@ public function __construct(Telemetry\Info $telemetryInfo, Test $test, string $m
5961
$this->suppressed = $suppressed;
6062
$this->ignoredByBaseline = $ignoredByBaseline;
6163
$this->ignoredByTest = $ignoredByTest;
64+
$this->trigger = $trigger;
6265
}
6366

6467
public function telemetryInfo(): Telemetry\Info
@@ -110,6 +113,11 @@ public function ignoredByTest(): bool
110113
return $this->ignoredByTest;
111114
}
112115

116+
public function trigger(): IssueTrigger
117+
{
118+
return $this->trigger;
119+
}
120+
113121
public function asString(): string
114122
{
115123
$message = $this->message;
@@ -118,7 +126,7 @@ public function asString(): string
118126
$message = PHP_EOL . $message;
119127
}
120128

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

123131
if ($this->suppressed) {
124132
$details[] = 'suppressed using operator';

src/Runner/ErrorHandler.php

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
use function restore_error_handler;
3232
use function set_error_handler;
3333
use PHPUnit\Event;
34+
use PHPUnit\Event\Code\IssueTrigger\IssueTrigger;
3435
use PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException;
36+
use PHPUnit\Event\Code\TestMethod;
3537
use PHPUnit\Runner\Baseline\Baseline;
3638
use PHPUnit\Runner\Baseline\Issue;
3739
use PHPUnit\TextUI\Configuration\Registry;
@@ -80,38 +82,6 @@ public function __invoke(int $errorNumber, string $errorString, string $errorFil
8082
$ignoredByBaseline = $this->ignoredByBaseline($errorFile, $errorLine, $errorString);
8183
$ignoredByTest = $test->metadata()->isIgnoreDeprecations()->isNotEmpty();
8284

83-
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
84-
85-
assert(isset($trace[1]['file']));
86-
assert(isset($trace[2]['file']));
87-
88-
$triggeredInFirstPartyCode = false;
89-
$triggerCalledFromFirstPartyCode = false;
90-
91-
if ($trace[1]['file'] === $test->file() ||
92-
$this->sourceFilter->includes($this->source, $trace[1]['file'])) {
93-
$triggeredInFirstPartyCode = true;
94-
}
95-
96-
if ($trace[2]['file'] === $test->file() ||
97-
$this->sourceFilter->includes($this->source, $trace[2]['file'])) {
98-
$triggerCalledFromFirstPartyCode = true;
99-
}
100-
101-
$self = false;
102-
$direct = false;
103-
$indirect = false;
104-
105-
if ($triggerCalledFromFirstPartyCode) {
106-
if ($triggeredInFirstPartyCode) {
107-
$self = true;
108-
} else {
109-
$direct = true;
110-
}
111-
} else {
112-
$indirect = true;
113-
}
114-
11585
switch ($errorNumber) {
11686
case E_NOTICE:
11787
case E_STRICT:
@@ -171,6 +141,7 @@ public function __invoke(int $errorNumber, string $errorString, string $errorFil
171141
$suppressed,
172142
$ignoredByBaseline,
173143
$ignoredByTest,
144+
$this->trigger($test),
174145
);
175146

176147
break;
@@ -184,6 +155,7 @@ public function __invoke(int $errorNumber, string $errorString, string $errorFil
184155
$suppressed,
185156
$ignoredByBaseline,
186157
$ignoredByTest,
158+
$this->trigger($test),
187159
);
188160

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

259231
return $this->baseline->has(Issue::from($file, $line, null, $description));
260232
}
233+
234+
private function trigger(TestMethod $test): IssueTrigger
235+
{
236+
if (!$this->source->notEmpty()) {
237+
return IssueTrigger::unknown();
238+
}
239+
240+
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 4);
241+
242+
assert(isset($trace[2]['file']));
243+
assert(isset($trace[3]['file']));
244+
245+
$triggeredInFirstPartyCode = false;
246+
$triggerCalledFromFirstPartyCode = false;
247+
248+
if ($trace[2]['file'] === $test->file() ||
249+
$this->sourceFilter->includes($this->source, $trace[2]['file'])) {
250+
$triggeredInFirstPartyCode = true;
251+
}
252+
253+
if ($trace[3]['file'] === $test->file() ||
254+
$this->sourceFilter->includes($this->source, $trace[3]['file'])) {
255+
$triggerCalledFromFirstPartyCode = true;
256+
}
257+
258+
if ($triggerCalledFromFirstPartyCode) {
259+
if ($triggeredInFirstPartyCode) {
260+
return IssueTrigger::self();
261+
}
262+
263+
return IssueTrigger::direct();
264+
}
265+
266+
return IssueTrigger::indirect();
267+
}
261268
}

tests/end-to-end/cli/fail-on/fail-on-deprecation.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Test Runner Execution Started (2 tests)
3030
Test Suite Started (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest, 2 tests)
3131
Test Preparation Started (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne)
3232
Test Prepared (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne)
33-
Test Triggered Deprecation (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne)
33+
Test Triggered Deprecation (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne, unknown whether this issue was triggered in first-party or third-party code)
3434
message
3535
Test Passed (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne)
3636
Test Finished (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne)

tests/end-to-end/cli/stop-on/stop-on-deprecation.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Test Runner Execution Started (2 tests)
3030
Test Suite Started (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest, 2 tests)
3131
Test Preparation Started (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne)
3232
Test Prepared (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne)
33-
Test Triggered Deprecation (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne)
33+
Test Triggered Deprecation (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne, unknown whether this issue was triggered in first-party or third-party code)
3434
message
3535
Test Passed (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne)
3636
Test Finished (PHPUnit\TestFixture\TestRunnerStopping\DeprecationTest::testOne)

tests/end-to-end/event/deprecations-can-be-ignored-using-attribute.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@ Test Runner Execution Started (4 tests)
2929
Test Suite Started (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest, 4 tests)
3030
Test Preparation Started (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOne)
3131
Test Prepared (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOne)
32-
Test Triggered Deprecation (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOne, ignored by test)
32+
Test Triggered Deprecation (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOne, unknown whether this issue was triggered in first-party or third-party code, ignored by test)
3333
message
3434
Test Passed (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOne)
3535
Test Finished (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOne)
3636
Test Preparation Started (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwo)
3737
Test Prepared (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwo)
38-
Test Triggered Deprecation (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwo)
38+
Test Triggered Deprecation (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwo, unknown whether this issue was triggered in first-party or third-party code)
3939
message
4040
Test Passed (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwo)
4141
Test Finished (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwo)
4242
Test Preparation Started (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOneErrorGetLast)
4343
Test Prepared (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOneErrorGetLast)
44-
Test Triggered Deprecation (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOneErrorGetLast, ignored by test)
44+
Test Triggered Deprecation (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOneErrorGetLast, unknown whether this issue was triggered in first-party or third-party code, ignored by test)
4545
message
4646
Test Passed (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOneErrorGetLast)
4747
Test Finished (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testOneErrorGetLast)
4848
Test Preparation Started (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwoErrorGetLast)
4949
Test Prepared (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwoErrorGetLast)
50-
Test Triggered Deprecation (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwoErrorGetLast)
50+
Test Triggered Deprecation (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwoErrorGetLast, unknown whether this issue was triggered in first-party or third-party code)
5151
message
5252
Test Passed (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwoErrorGetLast)
5353
Test Finished (PHPUnit\TestFixture\Event\IgnoreDeprecationsTest::testTwoErrorGetLast)

tests/end-to-end/event/php-deprecated.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ Test Runner Execution Started (1 test)
3232
Test Suite Started (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest, 1 test)
3333
Test Preparation Started (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest::testDeprecatedPhpFeature)
3434
Test Prepared (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest::testDeprecatedPhpFeature)
35-
Test Triggered PHP Deprecation (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest::testDeprecatedPhpFeature)
35+
Test Triggered PHP Deprecation (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest::testDeprecatedPhpFeature, unknown whether this issue was triggered in first-party or third-party code)
3636
strlen(): Passing null to parameter #1 ($string) of type string is deprecated
37-
Test Triggered PHP Deprecation (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest::testDeprecatedPhpFeature, suppressed using operator)
37+
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)
3838
strlen(): Passing null to parameter #1 ($string) of type string is deprecated
3939
Test Passed (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest::testDeprecatedPhpFeature)
4040
Test Finished (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest::testDeprecatedPhpFeature)

tests/end-to-end/event/user-deprecated.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ Test Runner Execution Started (2 tests)
2929
Test Suite Started (PHPUnit\TestFixture\Event\DeprecatedFeatureTest, 2 tests)
3030
Test Preparation Started (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedFeature)
3131
Test Prepared (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedFeature)
32-
Test Triggered Deprecation (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedFeature)
32+
Test Triggered Deprecation (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedFeature, unknown whether this issue was triggered in first-party or third-party code)
3333
message
34-
Test Triggered Deprecation (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedFeature, suppressed using operator)
34+
Test Triggered Deprecation (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedFeature, unknown whether this issue was triggered in first-party or third-party code, suppressed using operator)
3535
message
3636
Test Passed (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedFeature)
3737
Test Finished (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedFeature)
3838
Test Preparation Started (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedSuppressedErrorGetLast)
3939
Test Prepared (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedSuppressedErrorGetLast)
40-
Test Triggered Deprecation (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedSuppressedErrorGetLast, suppressed using operator)
40+
Test Triggered Deprecation (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedSuppressedErrorGetLast, unknown whether this issue was triggered in first-party or third-party code, suppressed using operator)
4141
message
4242
Test Passed (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedSuppressedErrorGetLast)
4343
Test Finished (PHPUnit\TestFixture\Event\DeprecatedFeatureTest::testDeprecatedSuppressedErrorGetLast)

0 commit comments

Comments
 (0)