3

我希望有人可以对我面临的这个问题有所了解。

[问题]

我在我的功能单元测试中模拟了教义.orm.default_entity_manager 服务。我将它注入到客户端服务容器中,这样我就不必在我的功能测试过程中访问我的数据库。对于仅涉及 GET 请求的测试,我能够验证我正在测试的控制器是否正在使用我的模拟服务。

但是,如果我尝试通过使用带有表单提交的爬虫来执行 POST 请求,我的模拟服务不会持续存在。在初始 GET 请求之后,客户端似乎只是在需要时再次注入教义.orm.default_entity_manager 服务,而不是我在客户端服务容器中设置的模拟版本。

总之,在 GET 请求期间使用了我的模拟服务,但在 POST 请求期间,EntityManager5144076565ee8_546a8d27f194334ee012bfe64f629947b07e4919__CG__\Doctrine\ORM\EntityManager 正在使用。 [参见下面的代码片段]

[问题]

有可能做我要求的吗?我想让我的所有请求都使用我定义的模拟服务。我想进行功能测试,但避免从数据库中写入或读取。

[示例代码]

    // Mocks
    $entityRepository = $this
    ->getMockBuilder('Doctrine\ORM\EntityRepository')
    ->setMethods(array('findby'))->disableOriginalConstructor()
    ->getMock();

    $entityRepository->expects($this->any())->method('findBy')
    ->will($this->returnValue(array()));

    $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
    ->setMethods(
    array('getRepository', 'getClassMetadata', 'flush',
    'persist'))->disableOriginalConstructor()
    ->getMock();

    $em->expects($this->any())->method('flush')
    ->will($this->returnValue(FALSE));

    $em->expects($this->any())->method('persist')
    ->will($this->returnValue(FALSE));

    $em->expects($this->any())->method('getRepository')
    ->will($this->returnValue($entityRepository));

    $em->expects($this->any())->method('getClassMetadata')
    ->will($this->returnValue(new ClassMetadata("test")));

    // Create test client.
    $client = static::createClient();

    // Inject entity mock into service container.
    $client->getContainer()
    ->set('doctrine.orm.default_entity_manager', $em, 'container');

    // Define request
    $crawler = $client->request('GET', '/locations/types/add');

    // Verify a few things
    $form = $crawler->selectButton('submit')->form();
    $form['location_type[title]'] = "TEST TITLE";
    $form['location_type[description]'] = "TEST DESCP";

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

1 回答 1

2

The issue here is that the kernel is booted after (during) every request:

protected function doRequest($request)
{
    // avoid shutting down the Kernel if no request has been performed yet
    // WebTestCase::createClient() boots the Kernel but do not handle a request
    if ($this->hasPerformedRequest) {
        $this->kernel->shutdown();
    } else {
        $this->hasPerformedRequest = true;
    }

    if ($this->profiler) {
        $this->profiler = false;

        $this->kernel->boot();
        $this->kernel->getContainer()->get('profiler')->enable();
    }

https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Client.php

So you need to replace doctrine with your mock after each request:

// Inject entity mock into service container.
$client->getContainer()
->set('doctrine.orm.default_entity_manager', $em, 'container');

// Define request
$crawler = $client->request('GET', '/locations/types/add'); 

// Inject entity mock into service container.
$client->getContainer()
->set('doctrine.orm.default_entity_manager', $em, 'container');

An easier way to use your mock globally would be to override the doctrine settings in config_test.yml

 orm:
        default_entity_manager:  Acme/MyBundle/Test/MockDoctrineEM

http://symfony.com/doc/master/reference/configuration/doctrine.html

于 2013-03-25T17:59:11.133 回答