DurationTest.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 function round;
  12. use PHPUnit\Framework\TestCase;
  13. /**
  14. * @covers \SebastianBergmann\Timer\Duration
  15. *
  16. * @uses \SebastianBergmann\Timer\Timer
  17. */
  18. final class DurationTest extends TestCase
  19. {
  20. public function testCanBeCreatedFromNanoseconds(): void
  21. {
  22. $duration = Duration::fromNanoseconds(1);
  23. $this->assertSame(1.0, $duration->asNanoseconds());
  24. $this->assertSame(1.0E-3, $duration->asMicroseconds());
  25. $this->assertSame(1.0E-6, $duration->asMilliseconds());
  26. $this->assertSame(1.0E-9, $duration->asSeconds());
  27. }
  28. public function testCanBeCreatedFromMicroseconds(): void
  29. {
  30. $duration = Duration::fromMicroseconds(1);
  31. $this->assertSame(1000.0, $duration->asNanoseconds());
  32. $this->assertSame(1.0, $duration->asMicroseconds());
  33. $this->assertSame(1.0E-3, $duration->asMilliseconds());
  34. $this->assertSame(1.0E-6, $duration->asSeconds());
  35. }
  36. /**
  37. * @dataProvider durationProvider
  38. * @testdox Formats $microseconds microseconds as "$string"
  39. */
  40. public function testCanBeFormattedAsString(string $string, int $microseconds): void
  41. {
  42. $duration = Duration::fromMicroseconds($microseconds);
  43. $this->assertSame($string, $duration->asString());
  44. }
  45. public function durationProvider(): array
  46. {
  47. return [
  48. ['00:00', (int) round((0 * 1000000))],
  49. ['00:00.001', (int) round((.001 * 1000000))],
  50. ['00:00.010', (int) round((.01 * 1000000))],
  51. ['00:00.100', (int) round((.1 * 1000000))],
  52. ['00:00.999', (int) round((.999 * 1000000))],
  53. ['00:00.999', (int) round((.9999 * 1000000))],
  54. ['00:01', (int) round((1 * 1000000))],
  55. ['00:02', (int) round((2 * 1000000))],
  56. ['00:59.900', (int) round((59.9 * 1000000))],
  57. ['00:59.990', (int) round((59.99 * 1000000))],
  58. ['00:59.999', (int) round((59.999 * 1000000))],
  59. ['00:59.999', (int) round((59.9999 * 1000000))],
  60. ['00:59.001', (int) round((59.001 * 1000000))],
  61. ['00:59.010', (int) round((59.01 * 1000000))],
  62. ['01:00', (int) round((60 * 1000000))],
  63. ['01:01', (int) round((61 * 1000000))],
  64. ['02:00', (int) round((120 * 1000000))],
  65. ['02:01', (int) round((121 * 1000000))],
  66. ['59:59.900', (int) round((3599.9 * 1000000))],
  67. ['59:59.990', (int) round((3599.99 * 1000000))],
  68. ['59:59.999', (int) round((3599.999 * 1000000))],
  69. ['59:59.999', (int) round((3599.9999 * 1000000))],
  70. ['59:59.001', (int) round((3599.001 * 1000000))],
  71. ['59:59.010', (int) round((3599.01 * 1000000))],
  72. ['01:00:00', (int) round((3600 * 1000000))],
  73. ['01:00:01', (int) round((3601 * 1000000))],
  74. ['01:00:01.900', (int) round((3601.9 * 1000000))],
  75. ['01:00:01.990', (int) round((3601.99 * 1000000))],
  76. ['01:00:01.999', (int) round((3601.999 * 1000000))],
  77. ['01:00:01.999', (int) round((3601.9999 * 1000000))],
  78. ['01:00:59.999', (int) round((3659.9999 * 1000000))],
  79. ['01:00:59.001', (int) round((3659.001 * 1000000))],
  80. ['01:00:59.010', (int) round((3659.01 * 1000000))],
  81. ['01:59:59.999', (int) round((7199.9999 * 1000000))],
  82. ];
  83. }
  84. }