-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
[DI] Cannot replace autowired services for testing purpose #35176
Comments
I found how to fix my problem services:
_defaults:
autowire: true
autoconfigure: true
App\Coffee\WaterTankInterface:
alias: App\Coffee\Infra\FullTank
public: true This works fine, I can use // Test extends Symfony\Bundle\FrameworkBundle\Test\KernelTestCase
public function testShouldReturn400WhenWaterTankIsEmpty()
{
// Given
$kernel = self::bootKernel();
$kernel->getContainer()->set(WaterTankInterface::class, new EmptyTank());
// When
$response = $kernel->handle(Request::create('/coffee', 'POST'));
// Then
$this->assertSame(400, $response->getStatusCode());
} However, this code below doesn't produce the same result. I thought it behaves as the code above but it looks like it isn't: services:
_defaults:
autowire: true
autoconfigure: true
public: true
App\Coffee\WaterTankInterface:
alias: App\Coffee\Infra\FullTank Could someone explain me why and if it's a bug or not please ? To easily reproduce the bug please checkout this PR branch lucascourot/sf-issue-replacing-services#2 and read its description |
Hey, thanks for your report! |
|
note that when your alias is public and the targeted service is private, the container will actually inline the service into its alias (and update references accordingly) as it cannot remove the public name while it can remove a private name. That's why your workaround works. |
Looks related to #48938 |
Symfony version(s) affected: 3.3+
Description
Services used as autowired arguments are not replaced by
$container->set()
even though they are replaced in the container itself. Thus I found quite a weird behaviour when trying to stub a service for testing purpose.How to reproduce
Please see the repo I created to reproduce the bug, services are public in test env
Launch phpunit
See the different behaviour in the controller executed by phpunit in the controller test
Additional context
Related feature : #24418
The text was updated successfully, but these errors were encountered: