Codeception: Suggestion: Make `grabService()` (Symfony Module) try to use Symfony 4.1's `test.service_container`

Created on 13 Jul 2018  路  4Comments  路  Source: Codeception/Codeception

grabService() is not working anymore in Symfony 4 (see #4976 ). The best practice seems to be to use the public test.service_container (https://symfony.com/blog/new-in-symfony-4-1-simpler-service-testing#comment-22223) which was introduced in Symfony 4.1.

$I->grabService('test.service_container') returns an instance of https://api.symfony.com/4.1/Symfony/Bundle/FrameworkBundle/Test/TestContainer.html on which you can call ->get('App\Service\MyService') to retrieve the service (even if it's private).

Instead of requiring everybody to write their own helper method (something like getSymfonyService() as suggested at https://github.com/Codeception/Codeception/issues/4976#issuecomment-399973664 ), it would be better to modify the existing Codeception\Module\Symfony::grabService(): First check if test.service_container is available, and only use the existing code as fallback:
https://github.com/Codeception/Codeception/blob/2.4/src/Codeception/Module/Symfony.php#L537

Or do this check already inside _getContainer()?:
https://github.com/Codeception/Codeception/blob/2.4/src/Codeception/Module/Symfony.php#L256

Should I try to commit this?

Symfony

All 4 comments

Some time has passed and this issue is still relevant. Any update @Naktibalda?

@krukru No, it is up to users of Symfony module to implement it.

Since I also encountered this issue today, I tried the suggested fix (using test.service_container) and it solved the issue for me, too. :+1:
I'll try to submit a PR for this.

Implemented in Codeception 3.1.2 via https://github.com/Codeception/Codeception/pull/5706

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sebastianneubert picture sebastianneubert  路  3Comments

rogoit picture rogoit  路  3Comments

raistlin picture raistlin  路  3Comments

sasha-x picture sasha-x  路  3Comments

gimler picture gimler  路  3Comments