Container Dependecy Injection not working in Fixtures after upgrading symfony 2.0 to 2.1/2.2

爱⌒轻易说出口 提交于 2019-12-07 12:34:24

I had exactly the same issue, nothing would fix it via changing round composer order, order in the kernal, extending or implementing etc, so in the end, I just pass the container in from the test:

public function testLogin()
{

///
$fixture = new UserFixtures();
$fixture->setContainer($container);
///

$executor->execute($loader->getFixtures(), true);

And now it works fine.

Solution:

Make sure you not only implement ContainerAwareInterface - you should extend ContainerAware

use Symfony\Component\DependencyInjection\ContainerAware;

// ContainerAware... already implements ContainerAwareInterface

class YourFixture extends ContainerAware 
{
    // ...

Another suggestion:

Move Datafixtures further up in your composer.json ( before doctrine/orm ) to have composer generate the classmap correctly.

The 2.0 documentation states.

Be sure to register the new namespace before Doctrine\Common. Otherwise, Symfony will look for data fixture classes inside the Doctrine\Common directory. Symfony's autoloader always looks for a class inside the directory of the first matching namespace, so more specific namespaces should always come first.

Furthermore you don't need fixtures-bundle and fixtures as fixtures is a dependency of fixtures-bundle.

Hope that helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!