diff --git a/lib/Doctrine/Deprecations/Deprecation.php b/lib/Doctrine/Deprecations/Deprecation.php index f656486..f2894d0 100644 --- a/lib/Doctrine/Deprecations/Deprecation.php +++ b/lib/Doctrine/Deprecations/Deprecation.php @@ -108,11 +108,12 @@ public static function triggerIfCalledFromOutside(string $package, string $link, // dependency and the caller is not a file in that package. // When $package is installed as a root package, then this deprecation // is always ignored - if (strpos($backtrace[0]['file'], 'vendor/' . $package . '/') === false) { + if (strpos($backtrace[0]['file'], '/vendor/' . $package . '/') === false && + strpos($backtrace[1]['file'], '/tests/') !== false) { return; } - if (strpos($backtrace[1]['file'], 'vendor/' . $package . '/') !== false) { + if (strpos($backtrace[1]['file'], '/vendor/' . $package . '/') !== false) { return; } diff --git a/tests/Doctrine/Deprecations/DeprecationTest.php b/tests/Doctrine/Deprecations/DeprecationTest.php index 291257c..bc5e0af 100644 --- a/tests/Doctrine/Deprecations/DeprecationTest.php +++ b/tests/Doctrine/Deprecations/DeprecationTest.php @@ -184,4 +184,36 @@ public function testDeprecationWithIgnoredPackage(): void $this->assertEquals(1, Deprecation::getUniqueTriggeredDeprecationsCount()); $this->assertEquals(['https://github.com/doctrine/orm/issue/1234' => 1], Deprecation::getTriggeredDeprecations()); } + + public function testDeprecationCalledFromOutside(): void + { + Deprecation::enableWithTriggerError(); + + $this->expectDeprecation(); + $this->expectDeprecationMessage('this is deprecated foo 1234 (DeprecationTest.php'); + + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/deprecations/4444'); + + $e = null; + try { + Deprecation::triggerIfCalledFromOutside( + 'doctrine/orm', + 'https://github.com/doctrine/deprecations/4444', + 'this is deprecated %s %d', + 'foo', + 1234 + ); + + $this->fail('Should never be reached because of deprecation exception'); + } catch (Throwable $e) { + $this->assertStringMatchesFormat( + 'this is deprecated foo 1234 (DeprecationTest.php:%d called by TestCase.php:%d, https://github.com/doctrine/deprecations/4444, package doctrine/orm)', + $e->getMessage() + ); + $this->assertEquals(1, Deprecation::getUniqueTriggeredDeprecationsCount()); + $this->assertEquals(['https://github.com/doctrine/deprecations/4444' => 1], Deprecation::getTriggeredDeprecations()); + + throw $e; + } + } }