2011-10-17 09:25:52 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Tests\Component\HttpKernel\Debug;
|
|
|
|
|
|
|
|
use Symfony\Component\HttpKernel\Debug\StopwatchEvent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* StopwatchEventTest
|
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
|
*/
|
|
|
|
class StopwatchEventTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
public function testGetOrigin()
|
|
|
|
{
|
|
|
|
$event = new StopwatchEvent(12);
|
|
|
|
$this->assertEquals(12, $event->getOrigin());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetCategory()
|
|
|
|
{
|
|
|
|
$event = new StopwatchEvent(microtime(true) * 1000);
|
|
|
|
$this->assertEquals('default', $event->getCategory());
|
|
|
|
|
2012-01-25 13:27:59 +00:00
|
|
|
$event = new StopwatchEvent(microtime(true) * 1000, 'cat');
|
2011-10-17 09:25:52 +01:00
|
|
|
$this->assertEquals('cat', $event->getCategory());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPeriods()
|
|
|
|
{
|
|
|
|
$event = new StopwatchEvent(microtime(true) * 1000);
|
|
|
|
$this->assertEquals(array(), $event->getPeriods());
|
|
|
|
|
|
|
|
$event = new StopwatchEvent(microtime(true) * 1000);
|
|
|
|
$event->start();
|
|
|
|
$event->stop();
|
2012-01-18 13:42:47 +00:00
|
|
|
$this->assertCount(1, $event->getPeriods());
|
2011-10-17 09:25:52 +01:00
|
|
|
|
|
|
|
$event = new StopwatchEvent(microtime(true) * 1000);
|
|
|
|
$event->start();
|
|
|
|
$event->stop();
|
|
|
|
$event->start();
|
|
|
|
$event->stop();
|
2012-01-18 13:42:47 +00:00
|
|
|
$this->assertCount(2, $event->getPeriods());
|
2011-10-17 09:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLap()
|
|
|
|
{
|
|
|
|
$event = new StopwatchEvent(microtime(true) * 1000);
|
|
|
|
$event->start();
|
|
|
|
$event->lap();
|
|
|
|
$event->stop();
|
2012-01-18 13:42:47 +00:00
|
|
|
$this->assertCount(2, $event->getPeriods());
|
2011-10-17 09:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTotalTime()
|
|
|
|
{
|
|
|
|
$event = new StopwatchEvent(microtime(true) * 1000);
|
|
|
|
$event->start();
|
2012-02-23 19:36:56 +00:00
|
|
|
usleep(20000);
|
2011-10-17 09:25:52 +01:00
|
|
|
$event->stop();
|
|
|
|
$total = $event->getTotalTime();
|
2012-02-23 19:36:56 +00:00
|
|
|
$this->assertTrue($total > 10 && $total <= 29, $total.' should be 20 (between 10 and 29)');
|
2011-10-17 09:25:52 +01:00
|
|
|
|
|
|
|
$event = new StopwatchEvent(microtime(true) * 1000);
|
|
|
|
$event->start();
|
|
|
|
usleep(10000);
|
|
|
|
$event->stop();
|
|
|
|
$event->start();
|
|
|
|
usleep(10000);
|
|
|
|
$event->stop();
|
|
|
|
$total = $event->getTotalTime();
|
2012-02-23 19:36:56 +00:00
|
|
|
$this->assertTrue($total > 10 && $total <= 29, $total.' should be 20 (between 10 and 29)');
|
2011-10-17 09:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \LogicException
|
|
|
|
*/
|
|
|
|
public function testStopWithoutStart()
|
|
|
|
{
|
|
|
|
$event = new StopwatchEvent(microtime(true) * 1000);
|
|
|
|
$event->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnsureStopped()
|
|
|
|
{
|
|
|
|
// this also test overlap between two periods
|
|
|
|
$event = new StopwatchEvent(microtime(true) * 1000);
|
|
|
|
$event->start();
|
|
|
|
usleep(10000);
|
|
|
|
$event->start();
|
|
|
|
usleep(10000);
|
|
|
|
$event->ensureStopped();
|
|
|
|
$total = $event->getTotalTime();
|
2012-02-23 19:36:56 +00:00
|
|
|
$this->assertTrue($total >= 21 && $total <= 39, $total.' should be 30 (between 21 and 39)');
|
2011-10-17 09:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testStartTime()
|
|
|
|
{
|
|
|
|
$event = new StopwatchEvent(microtime(true) * 1000);
|
2011-12-22 07:06:15 +00:00
|
|
|
$this->assertTrue($event->getStartTime() < 0.5);
|
2011-10-17 09:25:52 +01:00
|
|
|
|
|
|
|
$event = new StopwatchEvent(microtime(true) * 1000);
|
|
|
|
$event->start();
|
|
|
|
$event->stop();
|
2011-12-22 07:06:15 +00:00
|
|
|
$this->assertTrue($event->getStartTime() < 1);
|
2011-10-17 09:25:52 +01:00
|
|
|
|
|
|
|
$event = new StopwatchEvent(microtime(true) * 1000);
|
|
|
|
$event->start();
|
|
|
|
usleep(10000);
|
|
|
|
$event->stop();
|
|
|
|
$start = $event->getStartTime();
|
|
|
|
$this->assertTrue($start >= 0 && $start <= 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEndTime()
|
|
|
|
{
|
|
|
|
$event = new StopwatchEvent(microtime(true) * 1000);
|
|
|
|
$this->assertEquals(0, $event->getEndTime());
|
|
|
|
|
|
|
|
$event = new StopwatchEvent(microtime(true) * 1000);
|
|
|
|
$event->start();
|
|
|
|
$this->assertEquals(0, $event->getEndTime());
|
|
|
|
|
|
|
|
$event = new StopwatchEvent(microtime(true) * 1000);
|
|
|
|
$event->start();
|
|
|
|
usleep(10000);
|
|
|
|
$event->stop();
|
|
|
|
$event->start();
|
|
|
|
usleep(10000);
|
|
|
|
$event->stop();
|
|
|
|
$end = $event->getEndTime();
|
2012-02-23 19:36:56 +00:00
|
|
|
$this->assertTrue($end > 10 && $end <= 29, $end.' should be 20 (between 10 and 29)');
|
2011-10-17 09:25:52 +01:00
|
|
|
}
|
2012-01-25 13:27:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testInvalidOriginThrowsAnException()
|
|
|
|
{
|
|
|
|
new StopwatchEvent("abc");
|
|
|
|
}
|
2011-10-17 09:25:52 +01:00
|
|
|
}
|