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] 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');