Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow running tests with PHPUnit 10 and 11 #74

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 0 additions & 5 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
186 changes: 96 additions & 90 deletions tests/DeprecationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
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 restore_error_handler;
use function set_error_handler;

class DeprecationTest extends TestCase
Expand All @@ -38,24 +37,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 {
Expand All @@ -75,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 @@ -222,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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't tearDown() be used in this test class just like in tests/VerifyDeprecationsTest.php?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could, but this is safer: if for some reason something fails before we get to set the error handler, it's better not to restore it.

}
}

public function testDeprecationIfCalledFromOutsideNotTriggeringFromInside(): void
Expand Down Expand Up @@ -259,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 @@ -292,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