【问题标题】:phpunit and symfony2 : how to assert number of queries from client or response?phpunit 和 symfony2:如何断言来自客户端或响应的查询数量?
【发布时间】:2015-06-24 09:39:49
【问题描述】:

我正在使用 symfony2 和 phpunit 进行测试。

有没有类似的东西:

$client->getResponse()->getNumberOfQueries()

如果不是类似的东西,那么从响应中检索查询数量的方法是什么?

我想快速检查我没有优化查询的地方。

编辑:我的变量 $profile 似乎总是为空

/**
 * @dataProvider urlProvider
 * @param $url
 */
public function testPageIsSuccessful($url)
{
    $client = self::createClient(array(), array(
            'PHP_AUTH_USER' => 'xx',
            'PHP_AUTH_PW'   => 'xx',
        ));
    $client->enableProfiler();
    $client->followRedirects();
    $client->request('GET', $url);

    $this->assertEquals(200, $client->getResponse()->getStatusCode());
    if ($profile = $client->getProfile())
    {
        $this->assertLessThan(10, $profile->getCollector('db')->getQueryCount());
    }
}

在 lmy config_dev.yml:

web_profiler:
    toolbar: true
    intercept_redirects: false

仍然得到:

致命错误:在 D:\Divers\Programmation\Web\xxx\src\AppBundle\Tests\Controller\ApplicationAvailabilityFunctionalTest.php 第 59 行中的非对象上调用成员函数 getCollector()

【问题讨论】:

  • 再次查看我的答案。

标签: php symfony doctrine-orm phpunit


【解决方案1】:

在功能测试中,您可以访问分析器,并获取在请求期间进行的查询数量:

class HelloControllerTest extends WebTestCase
{
    public function testIndex()
    {
        $client = static::createClient();
        $client->enableProfiler();

        $crawler = $client->request('GET', '/hello/Fabien');

        $this->assertLessThan(30, $profile->getCollector('db')->getQueryCount());
    }
}

确保分析器配置为在您的测试环境中收集分析数据:

# app/config/config_test.yml

# ...
framework:
    profiler:
        enabled: true
        collect: true

从“How to Use the Profiler in a Functional Test”食谱中了解更多信息。

【讨论】:

  • 嗨!谢谢,似乎是正确的方法,但是按照描述的方式检索分析器,我只是得到一个空的配置文件,你知道为什么吗? (请参阅我的编辑)。
猜你喜欢
  • 2020-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-02
相关资源
最近更新 更多