TimerTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /*
  3. * This file is part of phpunit/php-timer.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  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 SebastianBergmann\Timer;
  11. use PHPUnit\Framework\TestCase;
  12. /**
  13. * @covers \SebastianBergmann\Timer\Timer
  14. */
  15. class TimerTest extends TestCase
  16. {
  17. public function testStartStop(): void
  18. {
  19. $this->assertInternalType('float', Timer::stop());
  20. }
  21. /**
  22. * @dataProvider secondsProvider
  23. */
  24. public function testSecondsToTimeString(string $string, string $seconds): void
  25. {
  26. $this->assertEquals(
  27. $string,
  28. Timer::secondsToTimeString($seconds)
  29. );
  30. }
  31. public function testTimeSinceStartOfRequest(): void
  32. {
  33. $this->assertStringMatchesFormat(
  34. '%f %s',
  35. Timer::timeSinceStartOfRequest()
  36. );
  37. }
  38. public function testTimeSinceStartOfRequest2(): void
  39. {
  40. if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
  41. unset($_SERVER['REQUEST_TIME_FLOAT']);
  42. }
  43. $this->assertStringMatchesFormat(
  44. '%f %s',
  45. Timer::timeSinceStartOfRequest()
  46. );
  47. }
  48. /**
  49. * @backupGlobals enabled
  50. */
  51. public function testTimeSinceStartOfRequest3(): void
  52. {
  53. if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
  54. unset($_SERVER['REQUEST_TIME_FLOAT']);
  55. }
  56. if (isset($_SERVER['REQUEST_TIME'])) {
  57. unset($_SERVER['REQUEST_TIME']);
  58. }
  59. $this->expectException(RuntimeException::class);
  60. Timer::timeSinceStartOfRequest();
  61. }
  62. public function testResourceUsage(): void
  63. {
  64. $this->assertStringMatchesFormat(
  65. 'Time: %s, Memory: %fMB',
  66. Timer::resourceUsage()
  67. );
  68. }
  69. public function secondsProvider()
  70. {
  71. return [
  72. ['0 ms', 0],
  73. ['1 ms', .001],
  74. ['10 ms', .01],
  75. ['100 ms', .1],
  76. ['999 ms', .999],
  77. ['1 second', .9999],
  78. ['1 second', 1],
  79. ['2 seconds', 2],
  80. ['59.9 seconds', 59.9],
  81. ['59.99 seconds', 59.99],
  82. ['59.99 seconds', 59.999],
  83. ['1 minute', 59.9999],
  84. ['59 seconds', 59.001],
  85. ['59.01 seconds', 59.01],
  86. ['1 minute', 60],
  87. ['1.01 minutes', 61],
  88. ['2 minutes', 120],
  89. ['2.01 minutes', 121],
  90. ['59.99 minutes', 3599.9],
  91. ['59.99 minutes', 3599.99],
  92. ['59.99 minutes', 3599.999],
  93. ['1 hour', 3599.9999],
  94. ['59.98 minutes', 3599.001],
  95. ['59.98 minutes', 3599.01],
  96. ['1 hour', 3600],
  97. ['1 hour', 3601],
  98. ['1 hour', 3601.9],
  99. ['1 hour', 3601.99],
  100. ['1 hour', 3601.999],
  101. ['1 hour', 3601.9999],
  102. ['1.01 hours', 3659.9999],
  103. ['1.01 hours', 3659.001],
  104. ['1.01 hours', 3659.01],
  105. ['2 hours', 7199.9999],
  106. ];
  107. }
  108. }