-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[PhpUnitBridge] Making private service deprecation warning supressor more strict #27342
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
Conversation
… fetching private services from within tests more specific to check that the source class that called the deprecated code extends KernelTestCase instead of PhpUnit's base TestCase.
I still want to see my comments in #27037 as I don't think this code should exist in the PHPUnit bridge at all (the new TestContainer should not be using any deprecated API) |
@stof, the new TestContainer is not using deprecated APIs. The reason this was introduced to PhpUnitBridge was because of the following scenario: 4.0 made it impossible to fetch private services from the container. 4.1 then introduced the new TestContainer that allows us to fetch private services from the container within tests (thanks to the new TestContainer). If we are running 3.4 code, and want to make our code 4.x compliant, we start using PhpUnitBridge to weed out the deprecations. We clean up all of the issue in our non-test code where we were getting private services from the container by using proper Dependency Injection and autowiring. However, our tests are still throw warnings about accessing private services from the container within the tests themselves. However, we know that we actually can do this as of 4.1. We don't need to refactor our test code, as the warnings would suggest. So instead, as @nicolas-grekas suggested, we decided to silence these specific warnings when thrown from contexts where private services are access from within tests. Maybe @nicolas-grekas will be able to explain better than me. |
if (isset($trace[3]['object'])) { | ||
$isPrivateServiceNotice = false !== strpos($msg, ' service is private, '); | ||
$isNoticeForContainerGetHasUsage = 'Symfony\Component\DependencyInjection\Container' === $trace[2]['class'] && in_array($trace[2]['function'], array('get', 'has')); | ||
$noticeWasTriggeredByPhpUnitTest = $trace[3]['object'] instanceof \PHPUnit\Framework\TestCase; | ||
if ($isPrivateServiceNotice && $isNoticeForContainerGetHasUsage && $noticeWasTriggeredByPhpUnitTest) { | ||
$kernelTestCaseClass = 'Symfony\\Bundle\\FrameworkBundle\\Test\\KernelTestCase'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this var should be removed, instanceof works better with a symbol than with a string
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(and a use
statement be used instead)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should definitely be WebTestCase at least, as KernelTestCase only allows to use $kernel->getContainer()->get('private_something')
in 3.x, which is always a valid deprecation
@stof here is how to get this situation: composer create-project symfony/skeleton:3.4 demo
cd demo
composer req test This will install That's why I think this is correct and should be supported. |
From @stof
this is correct: if one does We have a choice to do: either favor the false positive deprecation (ie revert this altogether) or merge this PR and live with the fact that some tests might break in 4.1. I'm mixed here, both have advantages... (and I think we cannot be more accurate than done here, unfortunately.) |
we indeed cannot be more accurate without changes to the 3.4 WebTestCase, as there is currently no difference between the 2 Reporting deprecation properly would require having |
We'll @stof, that's a very valid and good point. Since both of our two options outlines by @nicolas-grekas are imperfect, I suppose I'm actually in support of undoing #27037, as the code is clearly confusing the community. Hopefully users moving through 4.x deprecation in preperation for an upgrade pay close attention to the documentation and discern that they will be able to ignore certain instances of this deprecation. That is, unless we can touch the 3.4 branch to change the deprecation error to be more specific in regards to the 4.1 TestContainer implementation. But its too late to push this type of change to the 3.4 branch, isn't it? |
Thank you for your understanding, I share this opinion. Yes, too late for 3.4. |
This issue related to #27037 and, more specifically, to @stof's comment on #27312
I think this is sound logic, but I think we might even want to consider taking a step further. Really, shouldn't we be checking for extensions of
Symfony\Bundle\FrameworkBundle\Test\WebTestCase
? After all,WebTestCase
is the class that provides the staticcreateClient()
method used for building a client, from which the (test) container is retrieved, according to the current documentation: https://symfony.com/doc/current/testing.html@stof and @nicolas-grekas, the PR here is for
KernelTestCase
, but I can easily change it to check instead forWebTestCase
if you think the reasoning above is sound. I've tested both against my code, and they both work, but I am struggling to think of examples whereby a (proper) client/container is used within a test without extendingWebTestCase
and using the official approaches documented on the page linked above. Let me know what you all think.