From d3153a17192fb7d6eca70056735aa21d74caa3b2 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sat, 13 Mar 2021 20:54:47 +0100 Subject: [PATCH] [GH-20] Add tests for triggerIfCalledFromOutside --- composer.json | 6 +++ .../Doctrine/Deprecations/DeprecationTest.php | 37 +++++++++++++++++++ tests/fixtures/src/Foo.php | 20 ++++++++++ tests/fixtures/vendor/doctrine/foo/Bar.php | 22 +++++++++++ tests/fixtures/vendor/doctrine/foo/Baz.php | 12 ++++++ 5 files changed, 97 insertions(+) create mode 100644 tests/fixtures/src/Foo.php create mode 100644 tests/fixtures/vendor/doctrine/foo/Bar.php create mode 100644 tests/fixtures/vendor/doctrine/foo/Baz.php 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/tests/Doctrine/Deprecations/DeprecationTest.php b/tests/Doctrine/Deprecations/DeprecationTest.php index 291257c..80d8f02 100644 --- a/tests/Doctrine/Deprecations/DeprecationTest.php +++ b/tests/Doctrine/Deprecations/DeprecationTest.php @@ -184,4 +184,41 @@ public function testDeprecationWithIgnoredPackage(): void $this->assertEquals(1, Deprecation::getUniqueTriggeredDeprecationsCount()); $this->assertEquals(['https://github.com/doctrine/orm/issue/1234' => 1], Deprecation::getTriggeredDeprecations()); } + + public function testDeprecationIfCalledFromOutside(): void + { + Deprecation::enableWithTriggerError(); + + try { + \DeprecationTests\Foo::triggerDependencyWithDeprecation(); + + $this->fail('Not expected to get here'); + } catch (\Throwable $e) { + $this->assertEquals( + 'Bar::oldFunc() is deprecated, use Bar::newFunc() instead. (Bar.php:14 called by Foo.php:12, 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(); + + \DeprecationTests\Foo::triggerDependencyWithDeprecationFromInside(); + + $this->assertEquals(0, Deprecation::getUniqueTriggeredDeprecationsCount()); + } + + public function testDeprecationIfCalledFromOutsideNotTriggeringFromInsideClass(): void + { + Deprecation::enableWithTriggerError(); + + $baz = new \Doctrine\Foo\Baz(); + $baz->usingOldFunc(); + + $this->assertEquals(0, Deprecation::getUniqueTriggeredDeprecationsCount()); + } } diff --git a/tests/fixtures/src/Foo.php b/tests/fixtures/src/Foo.php new file mode 100644 index 0000000..fd59fcf --- /dev/null +++ b/tests/fixtures/src/Foo.php @@ -0,0 +1,20 @@ +oldFunc(); + } + + public function triggerDependencyWithDeprecationFromInside() + { + $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..0ba7d97 --- /dev/null +++ b/tests/fixtures/vendor/doctrine/foo/Bar.php @@ -0,0 +1,22 @@ +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..890bb17 --- /dev/null +++ b/tests/fixtures/vendor/doctrine/foo/Baz.php @@ -0,0 +1,12 @@ +oldFunc(); + } +}