Без опису

TimerTest.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php declare(strict_types=1);
  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. final class TimerTest extends TestCase
  16. {
  17. public function testCanBeStartedAndStopped(): void
  18. {
  19. $this->assertIsFloat(Timer::stop());
  20. }
  21. public function testCanFormatTimeSinceStartOfRequest(): void
  22. {
  23. $this->assertStringMatchesFormat('%f %s', Timer::timeSinceStartOfRequest());
  24. }
  25. /**
  26. * @backupGlobals enabled
  27. */
  28. public function testCanFormatSinceStartOfRequestWhenRequestTimeIsNotAvailableAsFloat(): void
  29. {
  30. if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
  31. unset($_SERVER['REQUEST_TIME_FLOAT']);
  32. }
  33. $this->assertStringMatchesFormat('%f %s', Timer::timeSinceStartOfRequest());
  34. }
  35. /**
  36. * @backupGlobals enabled
  37. */
  38. public function testCannotFormatTimeSinceStartOfRequestWhenRequestTimeIsNotAvailable(): void
  39. {
  40. if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
  41. unset($_SERVER['REQUEST_TIME_FLOAT']);
  42. }
  43. if (isset($_SERVER['REQUEST_TIME'])) {
  44. unset($_SERVER['REQUEST_TIME']);
  45. }
  46. $this->expectException(RuntimeException::class);
  47. Timer::timeSinceStartOfRequest();
  48. }
  49. public function testCanFormatResourceUsage(): void
  50. {
  51. $this->assertStringMatchesFormat('Time: %s, Memory: %f %s', Timer::resourceUsage());
  52. }
  53. /**
  54. * @dataProvider secondsProvider
  55. */
  56. public function testCanFormatSecondsAsString(string $string, float $seconds): void
  57. {
  58. $this->assertEquals($string, Timer::secondsToTimeString($seconds));
  59. }
  60. public function secondsProvider(): array
  61. {
  62. return [
  63. ['0 ms', 0],
  64. ['1 ms', .001],
  65. ['10 ms', .01],
  66. ['100 ms', .1],
  67. ['999 ms', .999],
  68. ['1 second', .9999],
  69. ['1 second', 1],
  70. ['2 seconds', 2],
  71. ['59.9 seconds', 59.9],
  72. ['59.99 seconds', 59.99],
  73. ['59.99 seconds', 59.999],
  74. ['1 minute', 59.9999],
  75. ['59 seconds', 59.001],
  76. ['59.01 seconds', 59.01],
  77. ['1 minute', 60],
  78. ['1.01 minutes', 61],
  79. ['2 minutes', 120],
  80. ['2.01 minutes', 121],
  81. ['59.99 minutes', 3599.9],
  82. ['59.99 minutes', 3599.99],
  83. ['59.99 minutes', 3599.999],
  84. ['1 hour', 3599.9999],
  85. ['59.98 minutes', 3599.001],
  86. ['59.98 minutes', 3599.01],
  87. ['1 hour', 3600],
  88. ['1 hour', 3601],
  89. ['1 hour', 3601.9],
  90. ['1 hour', 3601.99],
  91. ['1 hour', 3601.999],
  92. ['1 hour', 3601.9999],
  93. ['1.01 hours', 3659.9999],
  94. ['1.01 hours', 3659.001],
  95. ['1.01 hours', 3659.01],
  96. ['2 hours', 7199.9999],
  97. ];
  98. }
  99. /**
  100. * @dataProvider bytesProvider
  101. */
  102. public function testCanFormatBytesAsString(string $string, float $bytes): void
  103. {
  104. $this->assertEquals($string, Timer::bytesToString($bytes));
  105. }
  106. public function bytesProvider(): array
  107. {
  108. return [
  109. ['0 bytes', 0],
  110. ['1 byte', 1],
  111. ['1023 bytes', 1023],
  112. ['1.00 KB', 1024],
  113. ['1.50 KB', 1.5 * 1024],
  114. ['2.00 MB', 2 * 1048576],
  115. ['2.50 MB', 2.5 * 1048576],
  116. ['3.00 GB', 3 * 1073741824],
  117. ['3.50 GB', 3.5 * 1073741824],
  118. ];
  119. }
  120. }