TimerTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /*
  3. * This file is part of the PHP_Timer package.
  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. use PHPUnit\Framework\TestCase;
  11. class PHP_TimerTest extends TestCase
  12. {
  13. /**
  14. * @covers PHP_Timer::start
  15. * @covers PHP_Timer::stop
  16. */
  17. public function testStartStop()
  18. {
  19. $this->assertInternalType('float', PHP_Timer::stop());
  20. }
  21. /**
  22. * @covers PHP_Timer::secondsToTimeString
  23. * @dataProvider secondsProvider
  24. */
  25. public function testSecondsToTimeString($string, $seconds)
  26. {
  27. $this->assertEquals(
  28. $string,
  29. PHP_Timer::secondsToTimeString($seconds)
  30. );
  31. }
  32. /**
  33. * @covers PHP_Timer::timeSinceStartOfRequest
  34. */
  35. public function testTimeSinceStartOfRequest()
  36. {
  37. $this->assertStringMatchesFormat(
  38. '%f %s',
  39. PHP_Timer::timeSinceStartOfRequest()
  40. );
  41. }
  42. /**
  43. * @covers PHP_Timer::resourceUsage
  44. */
  45. public function testResourceUsage()
  46. {
  47. $this->assertStringMatchesFormat(
  48. 'Time: %s, Memory: %fMB',
  49. PHP_Timer::resourceUsage()
  50. );
  51. }
  52. public function secondsProvider()
  53. {
  54. return array(
  55. array('0 ms', 0),
  56. array('1 ms', .001),
  57. array('10 ms', .01),
  58. array('100 ms', .1),
  59. array('999 ms', .999),
  60. array('1 second', .9999),
  61. array('1 second', 1),
  62. array('2 seconds', 2),
  63. array('59.9 seconds', 59.9),
  64. array('59.99 seconds', 59.99),
  65. array('59.99 seconds', 59.999),
  66. array('1 minute', 59.9999),
  67. array('59 seconds', 59.001),
  68. array('59.01 seconds', 59.01),
  69. array('1 minute', 60),
  70. array('1.01 minutes', 61),
  71. array('2 minutes', 120),
  72. array('2.01 minutes', 121),
  73. array('59.99 minutes', 3599.9),
  74. array('59.99 minutes', 3599.99),
  75. array('59.99 minutes', 3599.999),
  76. array('1 hour', 3599.9999),
  77. array('59.98 minutes', 3599.001),
  78. array('59.98 minutes', 3599.01),
  79. array('1 hour', 3600),
  80. array('1 hour', 3601),
  81. array('1 hour', 3601.9),
  82. array('1 hour', 3601.99),
  83. array('1 hour', 3601.999),
  84. array('1 hour', 3601.9999),
  85. array('1.01 hours', 3659.9999),
  86. array('1.01 hours', 3659.001),
  87. array('1.01 hours', 3659.01),
  88. array('2 hours', 7199.9999),
  89. );
  90. }
  91. }