I once created a tester for this purpose. It is based on how this is done in the web debugging toolbar (class sfWebDebugPanelDoctrine).
I expanded sfTesterDoctrine so that it behaves the same. To check the number of requests, only the approval method is added.
You can also overwrite the debug () method to display query statistics.
<?php
class zTesterDoctrine extends sfTesterDoctrine
{
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();
}
protected function countDoctrineEvents()
{
return count($this->getDoctrineEvents());
}
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;
}
}
Usage example:
$browser = new sfTestFunctional(new sfBrowser());
$browser->setTester('doctrine', 'zTesterDoctrine');
$browser
->get('/some/page')
->with('response')->begin()
->isStatusCode(200)
->end()
->with('doctrine')->begin()
->assertSqlCountLessThan(20)
->end()
->end();
source
share