Symfony1 测试每个页面是否没有';是否不超过Symfony中允许的SQL计数?

Symfony1 测试每个页面是否没有';是否不超过Symfony中允许的SQL计数?,symfony1,automated-tests,lime,Symfony1,Automated Tests,Lime,我希望我能有一个isSQLCountLessThan()函数或其他什么 $browser = new sfTestFunctional(new sfBrowser()); $browser ->get('/some/page') ->with('response')->begin() ->isStatusCode(200) // ... ->isSQLCountLessThan(20) // imagine how cool :)

我希望我能有一个isSQLCountLessThan()函数或其他什么

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

有没有办法做到这一点?

我曾经为此创建了tester。它基于如何在web调试工具栏(SFWebDebugPanel类)中完成

我扩展了这个理论,所以它的行为是一样的。只有断言方法被添加到检查查询计数

您还可以覆盖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;
  }
}

美好的它是否根据每个请求重置?是。每个get()或post()请求都有一个单独的计数器。它适用于所有的测试人员,不仅适用于我的。非常理想!谢谢你,库巴。如果有人会出错-“Lass”必须是“Less”,并且->begin()在调用之前丢失。
$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();