编程语言
首页 > 编程语言> > php – 使用Doctrine在Symfony2中测试控制器

php – 使用Doctrine在Symfony2中测试控制器

作者:互联网

我在Symony2中创建了一个非常简单的REST控制器,其中包含控制器操作中的数据库插入/更新/删除.

是否有一种很好的方法可以为这些控制器操作编写单元/集成测试而不会污染生产数据库?我是否必须使用不同的环境 – 或者是否有来自框架供应商的建议方法?

电流控制器示例:

public function postAction()
{
    $json = $this->getRequest()->getContent();
    $params = json_decode($json);
    $name = $params->name;
    $description = $params->description;

    $sandbox = new Sandbox();
    $sandbox->setName($name);
    $sandbox->setDescription($description);
    $em = $this->getDoctrine()->getManager();
    $em->persist($sandbox);
    $em->flush();

    $response = new Response('/sandbox/'.$sandbox->getId());
    $response->setStatusCode(201);
    return $response;
}

目前的测试示例:

class SandboxControllerTest extends WebTestCase
{

    public function testRest()
    {
        $client = static::createClient();

        $crawler = $client->request('POST', '/service/sandbox', array(), array(), array(), json_encode(array('name' => 'TestMe', 'description' => 'TestDesc')));

        $this->assertEquals(
                201, $client->getResponse()->getStatusCode()
        );
    }
}

解决方法:

在我看来,你绝对应该避免使用测试更改数据库.

我最喜欢的方法是在测试客户端中注入实体管理器mock.例如:

public function testRest()
{
    // create entity manager mock
    $entityManagerMock = $this->getMockBuilder('Doctrine\ORM\EntityManager')
        ->setMethods(array('persist', 'flush'))
        ->disableOriginalConstructor()
        ->getMock();

    // now you can get some assertions if you want, eg.:
    $entityManagerMock->expects($this->once())
        ->method('flush');

    // next you need inject your mocked em into client's service container
    $client = static::createClient();
    $client->getContainer()->set('doctrine.orm.default_entity_manager', $entityManagerMock);

    // then you just do testing as usual
    $crawler = $client->request('POST', '/service/sandbox', array(), array(), array(), json_encode(array('name' => 'TestMe', 'description' => 'TestDesc')));

    $this->assertEquals(
            201, $client->getResponse()->getStatusCode()
    );
}

您应该注意这个解决方案的一件事是您需要在每个请求之前注入您的模拟服务.这是因为客户端在每个请求之间重新启动内核(这意味着容器也在重建).

编辑:

我在控制器测试中的GET方法是我可以模拟实体存储库等等,以便从db中获取每个数据,但这是很多工作而且不是很舒服,所以我更喜欢这种情况(我的意思是只有我们说话)关于控制器的测试)实际上从db获取真实数据.根据真实数据,我指的是用学说固定装置创建的数据.只要我们不更改数据库,我们就可以依赖于灯具.

但是,如果我们谈论在db(POST / PUT / DELETE方法)中更改数据,我总是使用模拟.如果您将使用em mock并对“perist”和“flush”方法设置适当的期望,您可以确保在没有任何数据库修改的情况下正确创建/更新/删除数据.

标签:php,symfony,phpunit,symfony-2-1
来源: https://codeday.me/bug/20190930/1835375.html