This repository was archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.php
More file actions
141 lines (110 loc) · 4.38 KB
/
Util.php
File metadata and controls
141 lines (110 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
declare(strict_types=1);
/**
* Copyright (c) 2020 Daniel Bannert
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/testomat/phpunit
*/
namespace Testomat\PHPUnit\Common;
use Closure;
use PHPUnit\Runner\Version;
use PHPUnit\TextUI\TestRunner;
use Testomat\PHPUnit\Common\Configuration\Configuration;
use Testomat\PHPUnit\Common\Configuration\Loader;
use Testomat\PHPUnit\Common\Contract\Exception\RuntimeException;
use Throwable;
final class Util
{
/** @var null|array<string, mixed> */
private static $phpunitArgumentsCache;
/** @var null|\PHPUnit\TextUI\Configuration\Configuration|\PHPUnit\Util\Configuration */
private static $phpunitConfigurationCache;
/** @var null|Configuration */
private static $testomatConfigurationCache;
/**
* @internal
*/
public static function reset(): void
{
self::$phpunitArgumentsCache = null;
self::$phpunitConfigurationCache = null;
self::$testomatConfigurationCache = null;
}
public static function getPHPUnitTestRunnerArguments(): array
{
if (self::$phpunitArgumentsCache !== null) {
return self::$phpunitArgumentsCache;
}
foreach (debug_backtrace() as $trace) {
if (isset($trace['object']) && $trace['object'] instanceof TestRunner) {
return self::$phpunitArgumentsCache = $trace['args'][1];
}
}
throw new RuntimeException(\Safe\sprintf('Failed to get a PHPUnit arguments from [%s] instance.', TestRunner::class));
}
/**
* @throws \Testomat\PHPUnit\Common\Contract\Exception\RuntimeException if loading of the phpunit.xml failed
*
* @return \PHPUnit\TextUI\Configuration\Configuration|\PHPUnit\Util\Configuration
*/
public static function getPHPUnitConfiguration()
{
if (self::$phpunitConfigurationCache !== null) {
return self::$phpunitConfigurationCache;
}
$arguments = self::getPHPUnitTestRunnerArguments();
/** @var \PHPUnit\TextUI\Configuration\Configuration|\PHPUnit\Util\Configuration $configuration */
$configuration = $arguments['configuration'] ?? null;
if ($configuration === null) {
throw new RuntimeException('Failed to load the PHPUnit configuration instance.');
}
return self::$phpunitConfigurationCache = $configuration;
}
public static function getTestomatConfiguration(): Configuration
{
if (self::$testomatConfigurationCache !== null) {
return self::$testomatConfigurationCache;
}
$loader = new Loader();
try {
if ((int) (substr(Version::id(), 0, 1)) === 8) {
$configurationPath = self::getPHPUnitConfiguration()->getFilename();
} else {
$configurationPath = self::getPHPUnitConfiguration()->filename();
}
$dir = \dirname($configurationPath);
$configFile = $dir . \DIRECTORY_SEPARATOR . 'testomat.xml';
if (! is_file($configFile)) {
$configFile = $dir . \DIRECTORY_SEPARATOR . 'testomat.xml.dist';
}
$configuration = $loader->load($configFile);
} catch (Throwable $exception) {
echo 'Loading default configuration for testomat' . \PHP_EOL;
$configuration = $loader->load(__DIR__ . \DIRECTORY_SEPARATOR . 'testomat.xml');
}
return self::$testomatConfigurationCache = $configuration;
}
public static function getPreparedTimeString(float $time, bool $withOutMs = false): string
{
$timeString = Timer::secondsToTimeString($time);
$space = ' ';
if (strpos($timeString, 'ms') !== false) {
$space = ' ';
if ((int) filter_var($timeString, \FILTER_SANITIZE_NUMBER_INT) >= 10) {
$space = ' ';
}
} elseif (strpos($timeString, 'sec') !== false) {
$space = ' ';
if ((int) filter_var($timeString, \FILTER_SANITIZE_NUMBER_INT) >= 10) {
$space = ' ';
}
}
if ($withOutMs && ($length = strpos($timeString, ',')) !== false) {
return sprintf(' [%s]%s', substr($timeString, 0, $length), $space);
}
return sprintf(' [%s]%s', $timeString, $space);
}
}