StopwatchTest.php 4.2 KB

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