StopwatchEventTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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\StopwatchEvent;
  13. /**
  14. * StopwatchEventTest.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. *
  18. * @group time-sensitive
  19. */
  20. class StopwatchEventTest extends TestCase
  21. {
  22. const DELTA = 37;
  23. public function testGetOrigin()
  24. {
  25. $event = new StopwatchEvent(12);
  26. $this->assertEquals(12, $event->getOrigin());
  27. }
  28. public function testGetCategory()
  29. {
  30. $event = new StopwatchEvent(microtime(true) * 1000);
  31. $this->assertEquals('default', $event->getCategory());
  32. $event = new StopwatchEvent(microtime(true) * 1000, 'cat');
  33. $this->assertEquals('cat', $event->getCategory());
  34. }
  35. public function testGetPeriods()
  36. {
  37. $event = new StopwatchEvent(microtime(true) * 1000);
  38. $this->assertEquals(array(), $event->getPeriods());
  39. $event = new StopwatchEvent(microtime(true) * 1000);
  40. $event->start();
  41. $event->stop();
  42. $this->assertCount(1, $event->getPeriods());
  43. $event = new StopwatchEvent(microtime(true) * 1000);
  44. $event->start();
  45. $event->stop();
  46. $event->start();
  47. $event->stop();
  48. $this->assertCount(2, $event->getPeriods());
  49. }
  50. public function testLap()
  51. {
  52. $event = new StopwatchEvent(microtime(true) * 1000);
  53. $event->start();
  54. $event->lap();
  55. $event->stop();
  56. $this->assertCount(2, $event->getPeriods());
  57. }
  58. public function testDuration()
  59. {
  60. $event = new StopwatchEvent(microtime(true) * 1000);
  61. $event->start();
  62. usleep(200000);
  63. $event->stop();
  64. $this->assertEquals(200, $event->getDuration(), null, self::DELTA);
  65. $event = new StopwatchEvent(microtime(true) * 1000);
  66. $event->start();
  67. usleep(100000);
  68. $event->stop();
  69. usleep(50000);
  70. $event->start();
  71. usleep(100000);
  72. $event->stop();
  73. $this->assertEquals(200, $event->getDuration(), null, self::DELTA);
  74. }
  75. public function testDurationBeforeStop()
  76. {
  77. $event = new StopwatchEvent(microtime(true) * 1000);
  78. $event->start();
  79. usleep(200000);
  80. $this->assertEquals(200, $event->getDuration(), null, self::DELTA);
  81. $event = new StopwatchEvent(microtime(true) * 1000);
  82. $event->start();
  83. usleep(100000);
  84. $event->stop();
  85. usleep(50000);
  86. $event->start();
  87. usleep(100000);
  88. $this->assertEquals(100, $event->getDuration(), null, self::DELTA);
  89. }
  90. /**
  91. * @expectedException \LogicException
  92. */
  93. public function testStopWithoutStart()
  94. {
  95. $event = new StopwatchEvent(microtime(true) * 1000);
  96. $event->stop();
  97. }
  98. public function testIsStarted()
  99. {
  100. $event = new StopwatchEvent(microtime(true) * 1000);
  101. $event->start();
  102. $this->assertTrue($event->isStarted());
  103. }
  104. public function testIsNotStarted()
  105. {
  106. $event = new StopwatchEvent(microtime(true) * 1000);
  107. $this->assertFalse($event->isStarted());
  108. }
  109. public function testEnsureStopped()
  110. {
  111. // this also test overlap between two periods
  112. $event = new StopwatchEvent(microtime(true) * 1000);
  113. $event->start();
  114. usleep(100000);
  115. $event->start();
  116. usleep(100000);
  117. $event->ensureStopped();
  118. $this->assertEquals(300, $event->getDuration(), null, self::DELTA);
  119. }
  120. public function testStartTime()
  121. {
  122. $event = new StopwatchEvent(microtime(true) * 1000);
  123. $this->assertLessThanOrEqual(0.5, $event->getStartTime());
  124. $event = new StopwatchEvent(microtime(true) * 1000);
  125. $event->start();
  126. $event->stop();
  127. $this->assertLessThanOrEqual(1, $event->getStartTime());
  128. $event = new StopwatchEvent(microtime(true) * 1000);
  129. $event->start();
  130. usleep(100000);
  131. $event->stop();
  132. $this->assertEquals(0, $event->getStartTime(), null, self::DELTA);
  133. }
  134. /**
  135. * @expectedException \InvalidArgumentException
  136. */
  137. public function testInvalidOriginThrowsAnException()
  138. {
  139. new StopwatchEvent('abc');
  140. }
  141. public function testHumanRepresentation()
  142. {
  143. $event = new StopwatchEvent(microtime(true) * 1000);
  144. $this->assertEquals('default: 0.00 MiB - 0 ms', (string) $event);
  145. $event->start();
  146. $event->stop();
  147. $this->assertEquals(1, preg_match('/default: [0-9\.]+ MiB - [0-9]+ ms/', (string) $event));
  148. $event = new StopwatchEvent(microtime(true) * 1000, 'foo');
  149. $this->assertEquals('foo: 0.00 MiB - 0 ms', (string) $event);
  150. }
  151. }