diff --git a/composer.json b/composer.json index 67d565e..82bd660 100644 --- a/composer.json +++ b/composer.json @@ -17,5 +17,11 @@ }, "autoload": { "psr-4": {"Doctrine\\Deprecations\\": "lib/Doctrine/Deprecations"} + }, + "autoload-dev": { + "psr-4": { + "DeprecationTests\\": "tests/fixtures/src", + "Doctrine\\Foo\\": "tests/fixtures/vendor/doctrine/foo" + } } } diff --git a/lib/Doctrine/Deprecations/Deprecation.php b/lib/Doctrine/Deprecations/Deprecation.php index f2894d0..cdc8e0d 100644 --- a/lib/Doctrine/Deprecations/Deprecation.php +++ b/lib/Doctrine/Deprecations/Deprecation.php @@ -108,8 +108,10 @@ 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 && - strpos($backtrace[1]['file'], '/tests/') !== false) { + if ( + strpos($backtrace[0]['file'], '/vendor/' . $package . '/') === false && + strpos($backtrace[1]['file'], '/tests/') !== false + ) { return; } diff --git a/tests/Doctrine/Deprecations/DeprecationTest.php b/tests/Doctrine/Deprecations/DeprecationTest.php index bc5e0af..2b777a1 100644 --- a/tests/Doctrine/Deprecations/DeprecationTest.php +++ b/tests/Doctrine/Deprecations/DeprecationTest.php @@ -4,7 +4,9 @@ namespace Doctrine\Deprecations; +use DeprecationTests\Foo; use Doctrine\Deprecations\PHPUnit\VerifyDeprecations; +use Doctrine\Foo\Baz; use PHPUnit\Framework\Error\Deprecated; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; @@ -185,7 +187,44 @@ public function testDeprecationWithIgnoredPackage(): void $this->assertEquals(['https://github.com/doctrine/orm/issue/1234' => 1], Deprecation::getTriggeredDeprecations()); } - public function testDeprecationCalledFromOutside(): void + public function testDeprecationIfCalledFromOutside(): void + { + Deprecation::enableWithTriggerError(); + + try { + Foo::triggerDependencyWithDeprecation(); + + $this->fail('Not expected to get here'); + } catch (Throwable $e) { + $this->assertEquals( + 'Bar::oldFunc() is deprecated, use Bar::newFunc() instead. (Bar.php:16 called by Foo.php:14, https://github.com/doctrine/foo, package doctrine/foo)', + $e->getMessage() + ); + + $this->assertEquals(['https://github.com/doctrine/foo' => 1], Deprecation::getTriggeredDeprecations()); + } + } + + public function testDeprecationIfCalledFromOutsideNotTriggeringFromInside(): void + { + Deprecation::enableWithTriggerError(); + + Foo::triggerDependencyWithDeprecationFromInside(); + + $this->assertEquals(0, Deprecation::getUniqueTriggeredDeprecationsCount()); + } + + public function testDeprecationIfCalledFromOutsideNotTriggeringFromInsideClass(): void + { + Deprecation::enableWithTriggerError(); + + $baz = new Baz(); + $baz->usingOldFunc(); + + $this->assertEquals(0, Deprecation::getUniqueTriggeredDeprecationsCount()); + } + + public function testDeprecationCalledFromOutsideInRoot(): void { Deprecation::enableWithTriggerError(); diff --git a/tests/fixtures/src/Foo.php b/tests/fixtures/src/Foo.php new file mode 100644 index 0000000..79e27ae --- /dev/null +++ b/tests/fixtures/src/Foo.php @@ -0,0 +1,22 @@ +oldFunc(); + } + + public function triggerDependencyWithDeprecationFromInside(): void + { + $bar = new Bar(); + $bar->newFunc(); + } +} diff --git a/tests/fixtures/vendor/doctrine/foo/Bar.php b/tests/fixtures/vendor/doctrine/foo/Bar.php new file mode 100644 index 0000000..2a84f1a --- /dev/null +++ b/tests/fixtures/vendor/doctrine/foo/Bar.php @@ -0,0 +1,24 @@ +oldFunc(); + } +} diff --git a/tests/fixtures/vendor/doctrine/foo/Baz.php b/tests/fixtures/vendor/doctrine/foo/Baz.php new file mode 100644 index 0000000..62b2bb1 --- /dev/null +++ b/tests/fixtures/vendor/doctrine/foo/Baz.php @@ -0,0 +1,14 @@ +oldFunc(); + } +}