-
Notifications
You must be signed in to change notification settings - Fork 16
/
TaskTestCase.php
59 lines (48 loc) 路 1.71 KB
/
TaskTestCase.php
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
<?php
namespace Castor\Tests;
use Castor\Tests\Helper\OutputCleaner;
use Castor\Tests\Helper\WebServerHelper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;
abstract class TaskTestCase extends TestCase
{
public static function setUpBeforeClass(): void
{
WebServerHelper::start();
}
public function runTask(array $args, ?string $cwd = null): Process
{
$coverage = $this->getTestResultObject()?->getCodeCoverage();
$castorBin = $_SERVER['CASTOR_BIN'] ?? __DIR__ . '/../bin/castor';
$extraEnv = [
'ENDPOINT' => $_SERVER['ENDPOINT'],
];
if ($coverage) {
$castorBin = __DIR__ . '/bin/castor';
$testName = debug_backtrace()[1]['class'] . '::' . debug_backtrace()[1]['function'];
$outputFilename = stream_get_meta_data(tmpfile())['uri'];
$extraEnv = [
'CC_OUTPUT_FILENAME' => $outputFilename,
'CC_TEST_NAME' => $testName,
];
}
$process = new Process(
[$castorBin, '--no-ansi', ...$args],
cwd: $cwd ? str_replace('{{ base }}', __DIR__ . '/..', $cwd) : __DIR__ . '/..',
env: [
'COLUMNS' => 120,
...$extraEnv,
],
);
$process->run();
if ($coverage) {
$coverage->merge(require $outputFilename);
}
return $process;
}
public static function assertStringEqualsFile(string $expectedFile, string $actualString, string $message = ''): void
{
$actualString = OutputCleaner::cleanOutput($actualString);
parent::assertStringEqualsFile($expectedFile, $actualString, $message);
}
}