Repository navigation
Expand file tree
/
Copy pathSubprocessInstanceTest.php
More file actions
113 lines (95 loc) · 4.62 KB
/
Copy pathSubprocessInstanceTest.php
File metadata and controls
113 lines (95 loc) · 4.62 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
<?php
declare(strict_types=1);
/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\ProcessBundle\Tests\Model;
use CleverAge\ProcessBundle\Filesystem\TemporaryFile;
use CleverAge\ProcessBundle\Model\SubprocessInstance;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\KernelInterface;
#[CoversClass(SubprocessInstance::class)]
#[UsesClass(TemporaryFile::class)]
class SubprocessInstanceTest extends TestCase
{
private string $projectDir;
protected function setUp(): void
{
$this->projectDir = sys_get_temp_dir().'/subprocess_instance_test_'.uniqid('', true);
mkdir($this->projectDir.'/bin', 0o777, true);
touch($this->projectDir.'/bin/console');
}
protected function tearDown(): void
{
unlink($this->projectDir.'/bin/console');
rmdir($this->projectDir.'/bin');
rmdir($this->projectDir);
}
public function testCommandLine(): void
{
$subprocess = (new SubprocessInstance($this->projectDir.'/bin/console', 'child', 'input', ['key' => 'value'], [], 'prod'))
->buildProcess();
$commandLine = $subprocess->getProcess()->getCommandLine();
$this->assertStringContainsString("'{$this->projectDir}/bin/console' '--env=prod' 'cleverage:process:execute' '--input-from-stdin'", $commandLine);
$this->assertStringEndsWith("'--context=key:value' 'child'", $commandLine);
$this->assertSame('input', $subprocess->getProcess()->getInput());
}
public function testDefaultEnvironmentOfTheConsoleWithoutEnvironment(): void
{
$subprocess = (new SubprocessInstance($this->projectDir.'/bin/console', 'child', null))->buildProcess();
$this->assertStringNotContainsString('--env', $subprocess->getProcess()->getCommandLine());
}
public function testJsonBufferIsWrittenInTheTemporaryDirectory(): void
{
$subprocess = (new SubprocessInstance($this->projectDir.'/bin/console', 'child', null, [], [SubprocessInstance::OPTION_JSON_BUFFERING => true]))
->buildProcess();
$this->assertMatchesRegularExpression(
'#\'--output=('.preg_quote(sys_get_temp_dir(), '#').'/cdm_buffer_[0-9a-f]{32}\.json-stream)\' \'--output-format=json-stream\'#',
$subprocess->getProcess()->getCommandLine()
);
preg_match('#--output=([^\']+)#', $subprocess->getProcess()->getCommandLine(), $matches);
$bufferPath = $matches[1] ?? '';
// Created before the sub-process writes into it, readable by its owner only
$this->assertFileExists($bufferPath);
$this->assertSame(0o600, fileperms($bufferPath) & 0o777);
unlink($bufferPath);
}
public function testMissingConsoleThrows(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage("Unable to resolve path to symfony console '{$this->projectDir}/missing'");
(new SubprocessInstance($this->projectDir.'/missing', 'child', null))->buildProcess();
}
public function testPassingTheKernelIsDeprecated(): void
{
$kernel = $this->createStub(KernelInterface::class);
$kernel->method('getProjectDir')->willReturn($this->projectDir);
$kernel->method('getEnvironment')->willReturn('prod');
$kernel->method('getLogDir')->willReturn($this->projectDir.'/var/log');
$deprecations = [];
set_error_handler(static function (int $errno, string $errstr) use (&$deprecations): bool {
$deprecations[] = $errstr;
return true;
}, \E_USER_DEPRECATED);
try {
$subprocess = new SubprocessInstance($kernel, 'child', null);
} finally {
restore_error_handler();
}
$this->assertSame(
['Since cleverage/process-bundle 5.4: Passing a "Symfony\Component\HttpKernel\KernelInterface" to "CleverAge\ProcessBundle\Model\SubprocessInstance::__construct()" is deprecated, pass the path of the console and the environment instead. The argument will only accept a string in v6.0.'],
$deprecations
);
$commandLine = $subprocess->buildProcess()->getProcess()->getCommandLine();
$this->assertStringContainsString("'{$this->projectDir}/bin/console' '--env=prod'", $commandLine);
// The log directory of the kernel is no longer created
$this->assertDirectoryDoesNotExist($this->projectDir.'/var');
}
}