StopwatchTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Stopwatch\Tests;
  11. use Symfony\Component\Stopwatch\Stopwatch;
  12. /**
  13. * StopwatchTest.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. *
  17. * @group time-sensitive
  18. */
  19. class StopwatchTest extends \PHPUnit_Framework_TestCase
  20. {
  21. const DELTA = 20;
  22. public function testStart()
  23. {
  24. $stopwatch = new Stopwatch();
  25. $event = $stopwatch->start('foo', 'cat');
  26. $this->assertInstanceOf('Symfony\Component\Stopwatch\StopwatchEvent', $event);
  27. $this->assertEquals('cat', $event->getCategory());
  28. $this->assertSame($event, $stopwatch->getEvent('foo'));
  29. }
  30. public function testIsStarted()
  31. {
  32. $stopwatch = new Stopwatch();
  33. $stopwatch->start('foo', 'cat');
  34. $this->assertTrue($stopwatch->isStarted('foo'));
  35. }
  36. public function testIsNotStarted()
  37. {
  38. $stopwatch = new Stopwatch();
  39. $this->assertFalse($stopwatch->isStarted('foo'));
  40. }
  41. public function testIsNotStartedEvent()
  42. {
  43. $stopwatch = new Stopwatch();
  44. $sections = new \ReflectionProperty('Symfony\Component\Stopwatch\Stopwatch', 'sections');
  45. $sections->setAccessible(true);
  46. $section = $sections->getValue($stopwatch);
  47. $events = new \ReflectionProperty('Symfony\Component\Stopwatch\Section', 'events');
  48. $events->setAccessible(true);
  49. $stopwatchMockEvent = $this->getMockBuilder('Symfony\Component\Stopwatch\StopwatchEvent')
  50. ->setConstructorArgs(array(microtime(true) * 1000))
  51. ->getMock()
  52. ;
  53. $events->setValue(end($section), array('foo' => $stopwatchMockEvent));
  54. $this->assertFalse($stopwatch->isStarted('foo'));
  55. }
  56. public function testStop()
  57. {
  58. $stopwatch = new Stopwatch();
  59. $stopwatch->start('foo', 'cat');
  60. usleep(200000);
  61. $event = $stopwatch->stop('foo');
  62. $this->assertInstanceOf('Symfony\Component\Stopwatch\StopwatchEvent', $event);
  63. $this->assertEquals(200, $event->getDuration(), null, self::DELTA);
  64. }
  65. /**
  66. * @expectedException \LogicException
  67. */
  68. public function testUnknownEvent()
  69. {
  70. $stopwatch = new Stopwatch();
  71. $stopwatch->getEvent('foo');
  72. }
  73. /**
  74. * @expectedException \LogicException
  75. */
  76. public function testStopWithoutStart()
  77. {
  78. $stopwatch = new Stopwatch();
  79. $stopwatch->stop('foo');
  80. }
  81. public function testSection()
  82. {
  83. $stopwatch = new Stopwatch();
  84. $stopwatch->openSection();
  85. $stopwatch->start('foo', 'cat');
  86. $stopwatch->stop('foo');
  87. $stopwatch->start('bar', 'cat');
  88. $stopwatch->stop('bar');
  89. $stopwatch->stopSection('1');
  90. $stopwatch->openSection();
  91. $stopwatch->start('foobar', 'cat');
  92. $stopwatch->stop('foobar');
  93. $stopwatch->stopSection('2');
  94. $stopwatch->openSection();
  95. $stopwatch->start('foobar', 'cat');
  96. $stopwatch->stop('foobar');
  97. $stopwatch->stopSection('0');
  98. // the section is an event by itself
  99. $this->assertCount(3, $stopwatch->getSectionEvents('1'));
  100. $this->assertCount(2, $stopwatch->getSectionEvents('2'));
  101. $this->assertCount(2, $stopwatch->getSectionEvents('0'));
  102. }
  103. public function testReopenASection()
  104. {
  105. $stopwatch = new Stopwatch();
  106. $stopwatch->openSection();
  107. $stopwatch->start('foo', 'cat');
  108. $stopwatch->stopSection('section');
  109. $stopwatch->openSection('section');
  110. $stopwatch->start('bar', 'cat');
  111. $stopwatch->stopSection('section');
  112. $events = $stopwatch->getSectionEvents('section');
  113. $this->assertCount(3, $events);
  114. $this->assertCount(2, $events['__section__']->getPeriods());
  115. }
  116. /**
  117. * @expectedException \LogicException
  118. */
  119. public function testReopenANewSectionShouldThrowAnException()
  120. {
  121. $stopwatch = new Stopwatch();
  122. $stopwatch->openSection('section');
  123. }
  124. }