如何测试更新操作?

时间:2013-10-07 08:54:10

标签: php symfony phpunit functional-testing

我正在尝试测试我的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);
    }

1 个答案:

答案 0 :(得分: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()
    );
}