From 3a75ce3d8fe753658c302a7c361b135376e22ec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Sun, 15 Dec 2024 13:22:35 +0100 Subject: [PATCH 1/2] Remove unneeded overrides We never call these methods since 5653e3fe9dd555fb8928c09e31085712e4a00ef1. --- phpstan.neon | 5 ----- tests/DeprecationTest.php | 20 -------------------- 2 files changed, 25 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 69e943b..81cb874 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -10,11 +10,6 @@ parameters: message: '/^Static property Doctrine\\Deprecations\\Deprecation::\$type \(int<0, 7>\|null\) does not accept int\.$/' path: src/Deprecation.php - - - message: '/^.*method_exists.*expectDeprecation.*$/' - path: tests/DeprecationTest.php - count: 2 - includes: - vendor/phpstan/phpstan-phpunit/extension.neon - vendor/phpstan/phpstan-phpunit/rules.neon diff --git a/tests/DeprecationTest.php b/tests/DeprecationTest.php index 79ddc9d..d63ca20 100644 --- a/tests/DeprecationTest.php +++ b/tests/DeprecationTest.php @@ -9,13 +9,11 @@ use DeprecationTests\RootDeprecation; use Doctrine\Deprecations\PHPUnit\VerifyDeprecations; use Doctrine\Foo\Baz; -use PHPUnit\Framework\Error\Deprecated; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; use ReflectionClass; use ReflectionProperty; -use function method_exists; use function set_error_handler; class DeprecationTest extends TestCase @@ -38,24 +36,6 @@ public function setUp(): void Deprecation::enableTrackingDeprecations(); } - public function expectDeprecation(): void - { - if (method_exists(TestCase::class, 'expectDeprecation')) { - parent::expectDeprecation(); - } else { - parent::expectException(Deprecated::class); - } - } - - public function expectDeprecationMessage(string $message): void - { - if (method_exists(TestCase::class, 'expectDeprecationMessage')) { - parent::expectDeprecationMessage($message); - } else { - parent::expectExceptionMessage($message); - } - } - public function expectErrorHandler(string $expectedMessage, string $identifier, int $times = 1): void { set_error_handler(function ($type, $message) use ($expectedMessage, $identifier, $times): bool { From 9e92b2e219341489fca97f6e30bc9d4b1574b34e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Sun, 15 Dec 2024 13:12:53 +0100 Subject: [PATCH 2/2] Allow running tests with PHPUnit 10 and 11 PHPUnit 11 emits complaints about the error handlers not being removed, which is risky, so I had to add a few calls to restore_error_handler(). This leaves us with PHPUnit deprecations about using @before and @after rather than the corresponding attributes. I do not think those can be addressed without a breaking change (adding the attributes is not enough, you have to remove the annotations). --- composer.json | 2 +- tests/DeprecationTest.php | 166 ++++++++++++++++++------------- tests/VerifyDeprecationsTest.php | 6 ++ 3 files changed, 103 insertions(+), 71 deletions(-) diff --git a/composer.json b/composer.json index a7a51e3..c20d805 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "doctrine/coding-standard": "^9 || ^12", "phpstan/phpstan": "1.4.10 || 2.0.3", "phpstan/phpstan-phpunit": "^1.0 || ^2", - "phpunit/phpunit": "^7.5 || ^8.5 || ^9.5", + "phpunit/phpunit": "^7.5 || ^8.5 || ^9.6 || ^10.5 || ^11.5", "psr/log": "^1 || ^2 || ^3" }, "suggest": { diff --git a/tests/DeprecationTest.php b/tests/DeprecationTest.php index d63ca20..99d798a 100644 --- a/tests/DeprecationTest.php +++ b/tests/DeprecationTest.php @@ -14,6 +14,7 @@ use ReflectionClass; use ReflectionProperty; +use function restore_error_handler; use function set_error_handler; class DeprecationTest extends TestCase @@ -55,30 +56,34 @@ public function testDeprecation(): void $this->expectDeprecationWithIdentifier('https://github.com/doctrine/deprecations/1234'); - $this->expectErrorHandler( - 'this is deprecated foo 1234 (DeprecationTest.php:%d called by TestCase.php:%d, https://github.com/doctrine/deprecations/1234, package doctrine/orm)', - 'https://github.com/doctrine/deprecations/1234' - ); + try { + $this->expectErrorHandler( + 'this is deprecated foo 1234 (DeprecationTest.php:%d called by TestCase.php:%d, https://github.com/doctrine/deprecations/1234, package doctrine/orm)', + 'https://github.com/doctrine/deprecations/1234' + ); - Deprecation::trigger( - 'doctrine/orm', - 'https://github.com/doctrine/deprecations/1234', - 'this is deprecated %s %d', - 'foo', - 1234 - ); + Deprecation::trigger( + 'doctrine/orm', + 'https://github.com/doctrine/deprecations/1234', + 'this is deprecated %s %d', + 'foo', + 1234 + ); - $this->assertEquals(1, Deprecation::getUniqueTriggeredDeprecationsCount()); + $this->assertEquals(1, Deprecation::getUniqueTriggeredDeprecationsCount()); - Deprecation::trigger( - 'doctrine/orm', - 'https://github.com/doctrine/deprecations/1234', - 'this is deprecated %s %d', - 'foo', - 1234 - ); + Deprecation::trigger( + 'doctrine/orm', + 'https://github.com/doctrine/deprecations/1234', + 'this is deprecated %s %d', + 'foo', + 1234 + ); - $this->assertEquals(2, Deprecation::getUniqueTriggeredDeprecationsCount()); + $this->assertEquals(2, Deprecation::getUniqueTriggeredDeprecationsCount()); + } finally { + restore_error_handler(); + } } public function testDeprecationWithoutDeduplication(): void @@ -86,36 +91,41 @@ public function testDeprecationWithoutDeduplication(): void Deprecation::enableWithTriggerError(); Deprecation::withoutDeduplication(); - $this->expectErrorHandler( - 'this is deprecated foo 2222 (DeprecationTest.php:%d called by TestCase.php:%d, https://github.com/doctrine/deprecations/2222, package doctrine/orm)', - 'https://github.com/doctrine/deprecations/2222' - ); + try { + $this->expectErrorHandler( + 'this is deprecated foo 2222 (DeprecationTest.php:%d called by TestCase.php:%d, https://github.com/doctrine/deprecations/2222, package doctrine/orm)', + 'https://github.com/doctrine/deprecations/2222' + ); - Deprecation::trigger( - 'doctrine/orm', - 'https://github.com/doctrine/deprecations/2222', - 'this is deprecated %s %d', - 'foo', - 2222 - ); + Deprecation::trigger( + 'doctrine/orm', + 'https://github.com/doctrine/deprecations/2222', + 'this is deprecated %s %d', + 'foo', + 2222 + ); - $this->assertEquals(1, Deprecation::getUniqueTriggeredDeprecationsCount()); + $this->assertEquals(1, Deprecation::getUniqueTriggeredDeprecationsCount()); + restore_error_handler(); - $this->expectErrorHandler( - 'this is deprecated foo 2222 (DeprecationTest.php:%d called by TestCase.php:%d, https://github.com/doctrine/deprecations/2222, package doctrine/orm)', - 'https://github.com/doctrine/deprecations/2222', - 2 - ); + $this->expectErrorHandler( + 'this is deprecated foo 2222 (DeprecationTest.php:%d called by TestCase.php:%d, https://github.com/doctrine/deprecations/2222, package doctrine/orm)', + 'https://github.com/doctrine/deprecations/2222', + 2 + ); - Deprecation::trigger( - 'doctrine/orm', - 'https://github.com/doctrine/deprecations/2222', - 'this is deprecated %s %d', - 'foo', - 2222 - ); + Deprecation::trigger( + 'doctrine/orm', + 'https://github.com/doctrine/deprecations/2222', + 'this is deprecated %s %d', + 'foo', + 2222 + ); - $this->assertEquals(2, Deprecation::getUniqueTriggeredDeprecationsCount()); + $this->assertEquals(2, Deprecation::getUniqueTriggeredDeprecationsCount()); + } finally { + restore_error_handler(); + } } public function testDisableResetsCounts(): void @@ -202,16 +212,20 @@ public function testDeprecationWithIgnoredLink(): void public function testDeprecationIfCalledFromOutside(): void { - Deprecation::enableWithTriggerError(); + Deprecation::enableWithTriggerError(); - $this->expectErrorHandler( - 'Bar::oldFunc() is deprecated, use Bar::newFunc() instead. (Bar.php:%d called by Foo.php:14, https://github.com/doctrine/foo, package doctrine/foo)', - 'https://github.com/doctrine/foo' - ); + try { + $this->expectErrorHandler( + 'Bar::oldFunc() is deprecated, use Bar::newFunc() instead. (Bar.php:%d called by Foo.php:14, https://github.com/doctrine/foo, package doctrine/foo)', + 'https://github.com/doctrine/foo' + ); - Foo::triggerDependencyWithDeprecation(); + Foo::triggerDependencyWithDeprecation(); - $this->assertEquals(1, Deprecation::getUniqueTriggeredDeprecationsCount()); + $this->assertEquals(1, Deprecation::getUniqueTriggeredDeprecationsCount()); + } finally { + restore_error_handler(); + } } public function testDeprecationIfCalledFromOutsideNotTriggeringFromInside(): void @@ -239,14 +253,18 @@ public function testDeprecationCalledFromOutsideInRoot(): void $this->expectDeprecationWithIdentifier('https://github.com/doctrine/deprecations/4444'); - $this->expectErrorHandler( - 'this is deprecated foo 1234 (RootDeprecation.php:%d called by DeprecationTest.php:%d, https://github.com/doctrine/deprecations/4444, package doctrine/orm)', - 'https://github.com/doctrine/deprecations/4444' - ); + try { + $this->expectErrorHandler( + 'this is deprecated foo 1234 (RootDeprecation.php:%d called by DeprecationTest.php:%d, https://github.com/doctrine/deprecations/4444, package doctrine/orm)', + 'https://github.com/doctrine/deprecations/4444' + ); - RootDeprecation::run(); + RootDeprecation::run(); - $this->assertEquals(1, Deprecation::getUniqueTriggeredDeprecationsCount()); + $this->assertEquals(1, Deprecation::getUniqueTriggeredDeprecationsCount()); + } finally { + restore_error_handler(); + } } public function testDeprecationTrackByEnv(): void @@ -272,13 +290,17 @@ public function testDeprecationTriggerByEnv(): void $reflectionProperty->setValue(null, null); $_ENV['DOCTRINE_DEPRECATIONS'] = 'trigger'; - $this->expectErrorHandler( - 'message (DeprecationTest.php:%d called by TestCase.php:%d, ' . __METHOD__ . ', package Foo)', - __METHOD__ - ); + try { + $this->expectErrorHandler( + 'message (DeprecationTest.php:%d called by TestCase.php:%d, ' . __METHOD__ . ', package Foo)', + __METHOD__ + ); - Deprecation::trigger('Foo', __METHOD__, 'message'); - $this->assertSame(1, Deprecation::getUniqueTriggeredDeprecationsCount()); + Deprecation::trigger('Foo', __METHOD__, 'message'); + $this->assertSame(1, Deprecation::getUniqueTriggeredDeprecationsCount()); + } finally { + restore_error_handler(); + } } public function testDeprecationTriggeredFromNativeCode(): void @@ -286,12 +308,16 @@ public function testDeprecationTriggeredFromNativeCode(): void $ref = new ReflectionClass(ConstructorDeprecation::class); Deprecation::enableWithTriggerError(); - $this->expectErrorHandler( - 'This constructor is deprecated. (ConstructorDeprecation.php:%d called by native code:0, https://github.com/doctrine/deprecations/issues/44, package doctrine/bar)', - 'https://github.com/doctrine/deprecations/issues/44' - ); + try { + $this->expectErrorHandler( + 'This constructor is deprecated. (ConstructorDeprecation.php:%d called by native code:0, https://github.com/doctrine/deprecations/issues/44, package doctrine/bar)', + 'https://github.com/doctrine/deprecations/issues/44' + ); - $ref->newInstance(); - $this->assertSame(1, Deprecation::getUniqueTriggeredDeprecationsCount()); + $ref->newInstance(); + $this->assertSame(1, Deprecation::getUniqueTriggeredDeprecationsCount()); + } finally { + restore_error_handler(); + } } } diff --git a/tests/VerifyDeprecationsTest.php b/tests/VerifyDeprecationsTest.php index 1d4f6de..f4dc82e 100644 --- a/tests/VerifyDeprecationsTest.php +++ b/tests/VerifyDeprecationsTest.php @@ -7,6 +7,7 @@ use Doctrine\Deprecations\PHPUnit\VerifyDeprecations; use PHPUnit\Framework\TestCase; +use function restore_error_handler; use function set_error_handler; class VerifyDeprecationsTest extends TestCase @@ -20,6 +21,11 @@ public function setUp(): void }); } + public function tearDown(): void + { + restore_error_handler(); + } + public function testExpectDeprecationWithIdentifier(): void { $this->expectDeprecationWithIdentifier('http://example.com');