|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * Original file: https://github.com/symfony/symfony/blob/9c413a356ccb3c982add8fa2b19813927157aa0e/src/Symfony/Bridge/PhpUnit/ClockMock.php#L18 |
| 7 | + * |
| 8 | + * Copyright (c) 2004-present Fabien Potencier |
| 9 | + * |
| 10 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | + * of this software and associated documentation files (the "Software"), to deal |
| 12 | + * in the Software without restriction, including without limitation the rights |
| 13 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | + * copies of the Software, and to permit persons to whom the Software is furnished |
| 15 | + * to do so, subject to the following conditions: |
| 16 | + * |
| 17 | + * The above copyright notice and this permission notice shall be included in all |
| 18 | + * copies or substantial portions of the Software. |
| 19 | + * |
| 20 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 | + * THE SOFTWARE. |
| 27 | + */ |
| 28 | + |
| 29 | +namespace Sentry\Util; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Nicolas Grekas <p@tchwork.com> |
| 33 | + * @author Dominic Tubach <dominic.tubach@to.com> |
| 34 | + */ |
| 35 | +class ClockMock |
| 36 | +{ |
| 37 | + private static $now; |
| 38 | + |
| 39 | + public static function withClockMock($enable = null): ?bool |
| 40 | + { |
| 41 | + if ($enable === null) { |
| 42 | + return self::$now !== null; |
| 43 | + } |
| 44 | + |
| 45 | + self::$now = is_numeric($enable) ? (float) $enable : ($enable ? microtime(true) : null); |
| 46 | + |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + public static function time(): int |
| 51 | + { |
| 52 | + if (self::$now === null) { |
| 53 | + return time(); |
| 54 | + } |
| 55 | + |
| 56 | + return (int) self::$now; |
| 57 | + } |
| 58 | + |
| 59 | + public static function sleep($s): int |
| 60 | + { |
| 61 | + if (self::$now === null) { |
| 62 | + return sleep($s); |
| 63 | + } |
| 64 | + |
| 65 | + self::$now += (int) $s; |
| 66 | + |
| 67 | + return 0; |
| 68 | + } |
| 69 | + |
| 70 | + public static function usleep($us): void |
| 71 | + { |
| 72 | + if (self::$now === null) { |
| 73 | + usleep($us); |
| 74 | + } else { |
| 75 | + self::$now += $us / 1000000; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @return string|float |
| 81 | + */ |
| 82 | + public static function microtime($asFloat = false) |
| 83 | + { |
| 84 | + if (self::$now === null) { |
| 85 | + return microtime($asFloat); |
| 86 | + } |
| 87 | + |
| 88 | + if ($asFloat) { |
| 89 | + return self::$now; |
| 90 | + } |
| 91 | + |
| 92 | + return \sprintf('%0.6f00 %d', self::$now - (int) self::$now, (int) self::$now); |
| 93 | + } |
| 94 | + |
| 95 | + public static function date($format, $timestamp = null): string |
| 96 | + { |
| 97 | + if ($timestamp === null) { |
| 98 | + $timestamp = self::time(); |
| 99 | + } |
| 100 | + |
| 101 | + return date($format, $timestamp); |
| 102 | + } |
| 103 | + |
| 104 | + public static function gmdate($format, $timestamp = null): string |
| 105 | + { |
| 106 | + if ($timestamp === null) { |
| 107 | + $timestamp = self::time(); |
| 108 | + } |
| 109 | + |
| 110 | + return gmdate($format, $timestamp); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @return array|int|float |
| 115 | + */ |
| 116 | + public static function hrtime($asNumber = false) |
| 117 | + { |
| 118 | + $ns = (self::$now - (int) self::$now) * 1000000000; |
| 119 | + |
| 120 | + if ($asNumber) { |
| 121 | + $number = \sprintf('%d%d', (int) self::$now, $ns); |
| 122 | + |
| 123 | + return \PHP_INT_SIZE === 8 ? (int) $number : (float) $number; |
| 124 | + } |
| 125 | + |
| 126 | + return [(int) self::$now, (int) $ns]; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @return false|int |
| 131 | + */ |
| 132 | + public static function strtotime(string $datetime, ?int $timestamp = null) |
| 133 | + { |
| 134 | + if ($timestamp === null) { |
| 135 | + $timestamp = self::time(); |
| 136 | + } |
| 137 | + |
| 138 | + return strtotime($datetime, $timestamp); |
| 139 | + } |
| 140 | + |
| 141 | + public static function register($class): void |
| 142 | + { |
| 143 | + $self = static::class; |
| 144 | + |
| 145 | + $mockedNs = [substr($class, 0, strrpos($class, '\\'))]; |
| 146 | + if (strpos($class, '\\Tests\\') > 0) { |
| 147 | + $ns = str_replace('\\Tests\\', '\\', $class); |
| 148 | + $mockedNs[] = substr($ns, 0, strrpos($ns, '\\')); |
| 149 | + } elseif (str_starts_with($class, 'Tests\\')) { |
| 150 | + $mockedNs[] = substr($class, 6, strrpos($class, '\\') - 6); |
| 151 | + } |
| 152 | + foreach ($mockedNs as $ns) { |
| 153 | + if (\function_exists($ns . '\time')) { |
| 154 | + continue; |
| 155 | + } |
| 156 | + eval(<<<EOPHP |
| 157 | +namespace $ns; |
| 158 | +
|
| 159 | +function time() |
| 160 | +{ |
| 161 | + return \\$self::time(); |
| 162 | +} |
| 163 | +
|
| 164 | +function microtime(\$asFloat = false) |
| 165 | +{ |
| 166 | + return \\$self::microtime(\$asFloat); |
| 167 | +} |
| 168 | +
|
| 169 | +function sleep(\$s) |
| 170 | +{ |
| 171 | + return \\$self::sleep(\$s); |
| 172 | +} |
| 173 | +
|
| 174 | +function usleep(\$us) |
| 175 | +{ |
| 176 | + \\$self::usleep(\$us); |
| 177 | +} |
| 178 | +
|
| 179 | +function date(\$format, \$timestamp = null) |
| 180 | +{ |
| 181 | + return \\$self::date(\$format, \$timestamp); |
| 182 | +} |
| 183 | +
|
| 184 | +function gmdate(\$format, \$timestamp = null) |
| 185 | +{ |
| 186 | + return \\$self::gmdate(\$format, \$timestamp); |
| 187 | +} |
| 188 | +
|
| 189 | +function hrtime(\$asNumber = false) |
| 190 | +{ |
| 191 | + return \\$self::hrtime(\$asNumber); |
| 192 | +} |
| 193 | +
|
| 194 | +function strtotime(\$datetime, \$timestamp = null) |
| 195 | +{ |
| 196 | + return \\$self::strtotime(\$datetime, \$timestamp); |
| 197 | +} |
| 198 | +EOPHP |
| 199 | + ); |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments