-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[FrameworkBundle] Allow setting private services with the test container #17734
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
[FrameworkBundle] Allow setting private services with the test container #17734
Conversation
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.
I think part of this section is outdated with this new feature.
We can keep the following doc with some adjustments (ex: remove "// the following line won't work unless the alias is made public ")
Lines 289 to 323 in d1c0d4a
Sometimes it can be useful to mock a dependency of a tested service. | |
From the example in the previous section, let's assume the | |
``NewsletterGenerator`` has a dependency to a private alias | |
``NewsRepositoryInterface`` pointing to a private ``NewsRepository`` service | |
and you'd like to use a mocked ``NewsRepositoryInterface`` instead of the | |
concrete one:: | |
// ... | |
use App\Contracts\Repository\NewsRepositoryInterface; | |
class NewsletterGeneratorTest extends KernelTestCase | |
{ | |
public function testSomething() | |
{ | |
// ... same bootstrap as the section above | |
$newsRepository = $this->createMock(NewsRepositoryInterface::class); | |
$newsRepository->expects(self::once()) | |
->method('findNewsFromLastMonth') | |
->willReturn([ | |
new News('some news'), | |
new News('some other news'), | |
]) | |
; | |
// the following line won't work unless the alias is made public | |
$container->set(NewsRepositoryInterface::class, $newsRepository); | |
// will be injected the mocked repository | |
$newsletterGenerator = $container->get(NewsletterGenerator::class); | |
// ... | |
} | |
} | |
We can add a versionadded
to tell is new.
And for "old" way I think we should remove it in 7.0. However I don't know in which block it must be documented (deprecated ?)
1f09911
to
010c3b1
Compare
@alamirault Good point! I updated the PR. Does it feel more natural? |
dded8a9
to
c6c9186
Compare
testing.rst
Outdated
@@ -321,52 +320,11 @@ concrete one:: | ||
} | ||
} | ||
|
||
In order to make the alias public, you will need to update configuration for | ||
the ``test`` environment as follows: | ||
.. deprecated:: 6.3 |
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 get removed in 7.0
I think we should add a versionadded that this is now possible from 6.3 on
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.
Is the first version (1f09911) what you mean, but with versionadded
instead of tip
?
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.
Always keep in mind to keep the info itself "forever" and only the versionadded
directive unit the next major
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.
Alright, it gets more and more clear. I tried a new version that will "keep the info forever" but also with a versionadded
. Hope it is the way to go!
c6c9186
to
f305d37
Compare
Great feature (thanks Nicolas); great contribution (thanks Alex); and great review (thanks Antoine and Oskar). |
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.
// the following line won't work unless the alias is made public
not true anymore ;)
True! I removed it while merging 🙌 |
Fixes #17733