ClockMock.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Bridge\PhpUnit;
  11. /**
  12. * @author Nicolas Grekas <p@tchwork.com>
  13. * @author Dominic Tubach <dominic.tubach@to.com>
  14. */
  15. class ClockMock
  16. {
  17. private static $now;
  18. public static function withClockMock($enable = null)
  19. {
  20. if (null === $enable) {
  21. return null !== self::$now;
  22. }
  23. self::$now = is_numeric($enable) ? (float) $enable : ($enable ? microtime(true) : null);
  24. return null;
  25. }
  26. public static function time()
  27. {
  28. if (null === self::$now) {
  29. return \time();
  30. }
  31. return (int) self::$now;
  32. }
  33. public static function sleep($s)
  34. {
  35. if (null === self::$now) {
  36. return \sleep($s);
  37. }
  38. self::$now += (int) $s;
  39. return 0;
  40. }
  41. public static function usleep($us)
  42. {
  43. if (null === self::$now) {
  44. \usleep($us);
  45. } else {
  46. self::$now += $us / 1000000;
  47. }
  48. }
  49. public static function microtime($asFloat = false)
  50. {
  51. if (null === self::$now) {
  52. return \microtime($asFloat);
  53. }
  54. if ($asFloat) {
  55. return self::$now;
  56. }
  57. return sprintf('%0.6f00 %d', self::$now - (int) self::$now, (int) self::$now);
  58. }
  59. public static function date($format, $timestamp = null)
  60. {
  61. if (null === $timestamp) {
  62. $timestamp = self::time();
  63. }
  64. return \date($format, $timestamp);
  65. }
  66. public static function gmdate($format, $timestamp = null)
  67. {
  68. if (null === $timestamp) {
  69. $timestamp = self::time();
  70. }
  71. return \gmdate($format, $timestamp);
  72. }
  73. public static function register($class)
  74. {
  75. $self = \get_called_class();
  76. $mockedNs = [substr($class, 0, strrpos($class, '\\'))];
  77. if (0 < strpos($class, '\\Tests\\')) {
  78. $ns = str_replace('\\Tests\\', '\\', $class);
  79. $mockedNs[] = substr($ns, 0, strrpos($ns, '\\'));
  80. } elseif (0 === strpos($class, 'Tests\\')) {
  81. $mockedNs[] = substr($class, 6, strrpos($class, '\\') - 6);
  82. }
  83. foreach ($mockedNs as $ns) {
  84. if (\function_exists($ns.'\time')) {
  85. continue;
  86. }
  87. eval(<<<EOPHP
  88. namespace $ns;
  89. function time()
  90. {
  91. return \\$self::time();
  92. }
  93. function microtime(\$asFloat = false)
  94. {
  95. return \\$self::microtime(\$asFloat);
  96. }
  97. function sleep(\$s)
  98. {
  99. return \\$self::sleep(\$s);
  100. }
  101. function usleep(\$us)
  102. {
  103. \\$self::usleep(\$us);
  104. }
  105. function date(\$format, \$timestamp = null)
  106. {
  107. return \\$self::date(\$format, \$timestamp);
  108. }
  109. function gmdate(\$format, \$timestamp = null)
  110. {
  111. return \\$self::gmdate(\$format, \$timestamp);
  112. }
  113. EOPHP
  114. );
  115. }
  116. }
  117. }