Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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

Merged

Conversation

alexandre-daubois
Copy link
Member

Fixes #17733

Copy link
Contributor

@alamirault alamirault left a 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 ")

symfony-docs/testing.rst

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 ?)

@alexandre-daubois
Copy link
Member Author

@alamirault Good point! I updated the PR. Does it feel more natural?

@alexandre-daubois alexandre-daubois force-pushed the feat-set-private-service branch 2 times, most recently from dded8a9 to c6c9186 Compare January 14, 2023 15:09
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
Copy link
Contributor

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

Copy link
Member Author

@alexandre-daubois alexandre-daubois Jan 14, 2023

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?

Copy link
Contributor

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

Copy link
Member Author

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!

@javiereguiluz
Copy link
Member

Great feature (thanks Nicolas); great contribution (thanks Alex); and great review (thanks Antoine and Oskar).

Copy link
Member

@nicolas-grekas nicolas-grekas left a 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 ;)

@javiereguiluz
Copy link
Member

True! I removed it while merging 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.