1

我正在尝试测试我的 updateAction,它允许更新我的数据库中的用户,但我不知道如何测试它......我已经成功测试了我的 createAction,它在我的数据库中添加了一个用户。所以我想获取我刚刚创建的用户 ID 来更新它。

这是我的函数 testCreate :

public function testCreate()
    {
        $crawler = $this->client->request('GET', '/admin/user/new');

        $buttonCrawlerNode = $crawler->selectButton('submit');

        $form = $buttonCrawlerNode->form(array(
            'myapp_usertype[email]'            => 'testadd@gmail.fr',
            'myapp_usertype[role]'             => 'ROLE_USER',
            'myapp_usertype[password][first]'  => 'test',
            'myapp_usertype[password][second]' => 'test',
        ));

        $this->client->submit($form);
    }
4

1 回答 1

1

没那么干,但你可以试试:

public function testUpdate()
{
    $user = static::$kernel->getContainer()->get('doctrine')->getRepository('YourUserBundle:User')->findOneByEmail('testadd@gmail.fr');

    $crawler = $client->request('GET', '/admin/user/edit/'.$user->getId());
    $form = $crawler->selectButton('submit')->form();

    $form = $buttonCrawlerNode->form(array(
        'myapp_usertype[email]'            => 'another@gmail.fr',
        'myapp_usertype[role]'             => 'ROLE_ANOTHER',
        'myapp_usertype[password][first]'  => 'test',
        'myapp_usertype[password][second]' => 'test',
        ));

        $crawler = $client->submit($form);

    $user = static::$kernel->getContainer()->get('doctrine')->getRepository('YourUserBundle:User')->find($user->getId());

    $this->assertEquals(
        'another@gmail.fr',
        $user->getEmail()
    );

    $this->assertEquals(
        'ROLE_ANOTHER',
        $user->getRole()
    );
}
于 2013-10-13T17:35:39.353 回答