简体   繁体   English

Symfony 2中的实体管理器和phpunit问题

[英]Issue with the Entity Manager and phpunit in Symfony 2

I have an issue with my Entity Manager in phpunit. 我的phpunit中的实体管理器有问题。

This is my test : 这是我的测试:

public function testValidChangeEmail()
{
    $client = self::createAuthClient('user','password');

    $crawler = $client->request('GET', '/user/edit/30');
    $crawler = $client->submit($crawler->selectButton('submit')->form(array(
        'form[email]' => 'new@email.com',
    )));

    /*
     * With this em, this work perfectly 
     * $em = $client->getContainer()->get('doctrine.orm.entity_manager');
     */

    $user = self::$em->getRepository('MyBundle:User')->findUser('new@email.com');

    die(var_dump($user->getEmail()));
}

and this is my WebTestCase which extends original WebTestCase : 这是我的WebTestCase,它扩展了原始的WebTestCase:

class WebTestCase extends BaseWebTestCase
{
    static protected $container;
    static protected $em;

    static protected function createClient(array $options = array(), array $server = array())
    {
        $client = parent::createClient($options, $server);
        self::$em = $client->getContainer()->get('doctrine.orm.entity_manager');
        self::$container = $client->getContainer();

        return $client;
    }

    protected function createAuthClient($user, $pass)
    {
        return self::createClient(array(), array(
            'PHP_AUTH_USER' => $user,
            'PHP_AUTH_PW'   => $pass,
        ));
    }

As you can see, I replace the self::$em when I created my client. 如您所见,我在创建客户端时替换了self :: $ em。

My issue : 我的问题:

In my test, the die() give me the old email and not the new email ( new@email.com ) which has registered in the test. 在我的测试中, die()给了我旧的电子邮件,而不是在测试中注册的新电子邮件( new@email.com )。 However in my database, I have the new@email.com correctly saved. 但是在我的数据库中,我正确保存了new@email.com

When I retrieve my user in the database, I use sefl::$em . 当我在数据库中检索我的用户时,我使用sefl::$em If I use the $em in comments, I retrieve the right new email. 如果我在评论中使用$em ,我会检索正确的新电子邮件。

I don't understand why in my WebTestCase, I can access to the new Entity Manager... 我不明白为什么在我的WebTestCase中,我可以访问新的实体管理器...

You can't access to the new entity manager because Symfony's client class shutdown's kernel before each request, which means that it erase whole service container and build it again from scratch. 您无法访问新的实体管理器,因为Symfony的客户端类在每个请求之前关闭内核,这意味着它会擦除整个服务容器并从头开始再次构建它。

So, after SECOND request you get very different entity manager than one you have in your own WebTestCase class. 因此,在SECOND请求之后,您获得的实体管理器与您在自己的WebTestCase类中的实体管理器非常不同。 (I said after second because client shutdown's kernel only if any request has been already performed) (我之后说过,因为只有在已经执行了任何请求时客户端关闭内核)

The question is - do you really need the same entity manafer in your WebTestCase class? 问题是 - 你真的需要在你的WebTestCase类中使用相同的实体manafer吗? Actually, you may want use the same entity manager because you want let say get controll over transaction between requests. 实际上,您可能希望使用相同的实体管理器,因为您希望在请求之间对事务进行get controll。 But in this case you should create your own test client class extended symfony's one and there define static connection or entity manager, and put it into container before every request. 但在这种情况下,您应该创建自己的测试客户端类扩展symfony的一个,并定义静态连接或实体管理器,并在每个请求之前将其放入容器中。

Look at example: http://alexandre-salome.fr/blog/Symfony2-Isolation-Of-Tests 看例子: http//alexandre-salome.fr/blog/Symfony2-Isolation-Of-Tests

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM