TimerTest.php 2.7 KB

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