Skip to content

Commit

Permalink
Allow running tests with PHPUnit 10 and 11
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
greg0ire committed Dec 16, 2024
1 parent 3a75ce3 commit 9e92b2e
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 71 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
166 changes: 96 additions & 70 deletions tests/DeprecationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use ReflectionClass;
use ReflectionProperty;

use function restore_error_handler;
use function set_error_handler;

class DeprecationTest extends TestCase
Expand Down Expand Up @@ -55,67 +56,76 @@ 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
{
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -272,26 +290,34 @@ 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
{
$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();
}
}
}
6 changes: 6 additions & 0 deletions tests/VerifyDeprecationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +21,11 @@ public function setUp(): void
});
}

public function tearDown(): void
{
restore_error_handler();
}

public function testExpectDeprecationWithIdentifier(): void
{
$this->expectDeprecationWithIdentifier('http://example.com');
Expand Down

0 comments on commit 9e92b2e

Please sign in to comment.