Repository navigation
Expand file tree
/
Copy pathTestCommand.php
More file actions
81 lines (68 loc) · 2.47 KB
/
Copy pathTestCommand.php
File metadata and controls
81 lines (68 loc) · 2.47 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
<?php
namespace ActiveRecord\Scripts;
use Composer\Script\Event;
use Symfony\Component\Process\Process;
class TestCommand
{
public static function runTest(Event $event)
{
try {
$args = $event->getArguments();
$fileName = count($args) >= 1 ? $args[0] : null;
$filter = count($args) >= 2 ? $args[1] : null;
if (null !== $fileName && !str_starts_with($fileName, '--')) {
$args = TestCommand::buildArgs($fileName, $filter);
$str = 'Running: ' . implode(' ', $args) . "\n";
if (1 === count($args)) {
$str .= "To run just the tests in test/CallbackTest.php, try: composer test callback\n";
}
if (1 === count($args) || 2 === count($args)) {
$str .= "To run a specific test in test/DateTimeTest.php, try: composer test dateTime testSetIsoDate\n";
}
echo $str;
} else {
array_unshift($args, 'vendor/bin/phpunit');
}
$process = new Process($args);
$process->setTimeout(1200);
if (Process::isTtySupported()) {
$process->setTty(true);
$process->run();
} else {
$process->run(function ($type, $buffer): void {
echo $buffer;
});
}
return 0;
} catch (\Exception $e) {
echo "\n" . $e->getMessage() . "\n";
}
}
private static function buildArgs(?string $fileName, ?string $filter): array
{
$args = ['vendor/bin/phpunit'];
if (null === $fileName) {
return $args;
}
if (str_starts_with($fileName, 'test/')) {
$fileName = substr($fileName, strlen('test/'));
}
if (str_ends_with($fileName, '.php')) {
$fileName = substr($fileName, 0, -strlen('.php'));
}
if (str_ends_with($fileName, 'Test')) {
$fileName = substr($fileName, 0, -strlen('Test'));
}
$fileName = ucfirst($fileName);
$fileName = "test/{$fileName}Test.php";
if (!file_exists($fileName)) {
throw new \Exception("{$fileName} does not exist. Did you mispell it?");
}
if (null != $filter) {
array_push($args, '--filter');
array_push($args, $filter);
}
array_push($args, $fileName);
return $args;
}
}