【问题标题】:Test that each page doesn't exceed allowed SQL count in Symfony?测试每个页面不超过 Symfony 中允许的 SQL 计数?
【发布时间】:2011-03-16 08:57:36
【问题描述】:

我希望我可以有一个 isSQLCountLessThan() 函数之类的。

$browser = new sfTestFunctional(new sfBrowser());
$browser
  ->get('/some/page')
  ->with('response')->begin()
    ->isStatusCode(200)
    // ...
    ->isSQLCountLessThan(20) // imagine how cool :)
  ->end();

有没有这样的方法?

【问题讨论】:

    标签: symfony1 automated-tests lime


    【解决方案1】:

    我曾经为此目的创建了测试器。它基于它在 Web 调试工具栏(sfWebDebugPanelDoctrine 类)中的完成方式。

    我扩展了 sfTesterDoctrine,所以它的行为是一样的。仅添加断言方法来检查查询计数。

    您还可以覆盖 debug() 方法以显示查询统计信息。

    <?php
    /*
     * (c) 2010 Jakub Zalas
     *
     * For the full copyright and license information, please view the LICENSE
     * file that was distributed with this source code.
     */
    
    /**
     * @package     zTestPlugin
     * @subpackage  test
     * @author      Jakub Zalas <jakub@zalas.pl>
     */
    class zTesterDoctrine extends sfTesterDoctrine
    {
      /**
       * @param integer $limit 
       * @return sfTestFunctionalBase|sfTester
       */
      public function assertSqlCountLessThan($limit)
      {
        $queryCount = $this->countDoctrineEvents();
    
        $this->tester->cmp_ok($queryCount, '<', (int) $limit, sprintf('There are less than "%d" SQL queries performed', $limit));
    
        return $this->getObjectToReturn();
      }
    
      /**
       * @return integer
       */
      protected function countDoctrineEvents()
      {
        return count($this->getDoctrineEvents());
      }
    
      /**
       * @return array
       */
      protected function getDoctrineEvents()
      {
        if (!$databaseManager = $this->browser->getContext()->getDatabaseManager())
        {
          throw new LogicConnection('The current context does not include a database manager.');
        }
    
        $events = array();
        foreach ($databaseManager->getNames() as $name)
        {
          $database = $databaseManager->getDatabase($name);
          if ($database instanceof sfDoctrineDatabase && $profiler = $database->getProfiler())
          {
            foreach ($profiler->getQueryExecutionEvents() as $event)
            {
              $events[$event->getSequence()] = $event;
            }
          }
        }
    
        ksort($events);
    
        return $events;
      }
    }
    

    示例用法:

    $browser = new sfTestFunctional(new sfBrowser());
    $browser->setTester('doctrine', 'zTesterDoctrine');
    
    $browser
      ->get('/some/page')
      ->with('response')->begin()
        ->isStatusCode(200)
      ->end()
      ->with('doctrine')->begin()
        ->assertSqlCountLessThan(20) // imagine how cool :)
      ->end()
    ->end();
    

    【讨论】:

    • 不错。它会根据每个请求重置吗?
    • 是的。每个 get() 或 post() 请求都有一个单独的计数器。它适用于所有测试人员,不仅适用于我。
    • 理想!谢谢你,库巴。如果有人会出错 - “Lass”必须是“Less”,并且 ->begin() 在调用之前丢失。
    猜你喜欢
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    相关资源
    最近更新 更多