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

Document how to integrate with PHPUnit #66

Merged
merged 1 commit into from
Dec 7, 2024

Conversation

greg0ire
Copy link
Member

Right now, it is possible to display Doctrine deprecations with a bit of PHPUnit configuration.
Note that this does not seem to clash with triggering self-deprecations, provided you use the VerifyDeprecations trait in tests that call the deprecated pieces of code.

@greg0ire
Copy link
Member Author

Stolen from @wouterj at phpDocumentor/guides#1184

@wouterj
Copy link

wouterj commented Nov 30, 2024

Note that this does not seem to clash with triggering self-deprecations, provided you use the VerifyDeprecations trait in tests that call the deprecated pieces of code.

This is not 100% true, you must use PHPUnit's WithoutErrorHandler in tests that expect deprecations. This will disable the entire error handler (so warnings/errors also won't be reported for that specific test), which is a bit of a drawback.

@greg0ire
Copy link
Member Author

you must use PHPUnit's WithoutErrorHandler in tests that expect deprecations

Why do you have to do that, and how do you do that? I tried using just VerifyDeprecations on a test case that triggered some, and it did not seem to have any wrong side effects 🤔

@wouterj
Copy link

wouterj commented Dec 1, 2024

Using failOnDeprecation="true" you tell phpunit that a deprecation notice should be regarded as a test failure (i.e. exit code != 0).

Without #[WithoutErrorHandler], the deprecation is caught by PHPUnit's error handler which marks the test run as failed (Deprecation::enableTrackingDeprecations() does not disable the trigger mode set by the env var).

@greg0ire
Copy link
Member Author

greg0ire commented Dec 1, 2024

@wouterj that's not what I observe.

Without use VerifyDeprecations

$ vendor/bin/phpunit
PHPUnit 11.4.4 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.14
Configuration: /home/greg/dev/DoctrineFixturesBundle/phpunit.xml.dist

D..............                                                   15 / 15 (100%)

Time: 00:00.088, Memory: 16.00 MB

1 test triggered 1 deprecation:

1) /home/greg/dev/DoctrineFixturesBundle/vendor/doctrine/deprecations/lib/Doctrine/Deprecations/Deprecation.php:210
pwet (SymfonyFixturesLoader.php:36 called by getTest_Doctrine_Fixtures_LoaderService.php:23, pwet, package doctrine/orm)

Triggered by:

* Doctrine\Bundle\FixturesBundle\Tests\IntegrationTest::testFixturesLoader
  /home/greg/dev/DoctrineFixturesBundle/tests/IntegrationTest.php:33

OK, but there were issues!
Tests: 15, Assertions: 46, Deprecations: 1.
╭─~/dev/DoctrineFixturesBundle on be1bc35 ✘
╰$ echo $?
1

With use VerifyDeprecations

$ vendor/bin/phpunit
PHPUnit 11.4.4 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.14
Configuration: /home/greg/dev/DoctrineFixturesBundle/phpunit.xml.dist

...............                                                   15 / 15 (100%)

Time: 00:00.087, Memory: 14.00 MB

OK, but there were issues!
Tests: 15, Assertions: 46, PHPUnit Deprecations: 2.
╭─~/dev/DoctrineFixturesBundle on be1bc35 ✘
╰$ echo $?
0

Deprecation::enableTrackingDeprecations() does not disable the trigger mode set by the env var

Ah I think I found a bug. It all depends whether you call it before calling trigger(), because trigger() starts like this:

$type = self::$type ?? self::getTypeFromEnv();

So I think you're right about the necessity to add #[WithoutErrorHandler], but that's not all, you also have to somehow make sure the env is read into Deprecations::$type before using VerifyDeprecations, otherwise you will mask all subsequent calls to trigger.

@greg0ire greg0ire marked this pull request as draft December 1, 2024 15:24
@greg0ire greg0ire force-pushed the phpunit-integration branch from dfaf597 to 5eff5be Compare December 1, 2024 15:47
@greg0ire greg0ire added the bug Something isn't working label Dec 1, 2024
Right now, it is possible to display Doctrine deprecations with a bit of
PHPUnit configuration.
Note that this does not seem to clash with triggering self-deprecations,
provided you use the VerifyDeprecations trait in tests that call the
deprecated pieces of code.
@greg0ire greg0ire force-pushed the phpunit-integration branch from 5eff5be to a8a2989 Compare December 1, 2024 16:01
@greg0ire
Copy link
Member Author

greg0ire commented Dec 1, 2024

I pushed a code change that I believe addresses the issue

@greg0ire greg0ire marked this pull request as ready for review December 3, 2024 17:15
@greg0ire greg0ire added this to the 1.1.4 milestone Dec 7, 2024
@greg0ire greg0ire merged commit 31610db into doctrine:1.1.x Dec 7, 2024
13 checks passed
@greg0ire greg0ire deleted the phpunit-integration branch December 7, 2024 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants