StopwatchEventTest.php 4.9 KB

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