Skip to content

Commit de9316b

Browse files
committed
fix test
1 parent 2ebf20b commit de9316b

File tree

2 files changed

+88
-51
lines changed

2 files changed

+88
-51
lines changed

tests/Feature/StdinTest.php

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,10 @@
2323
$testCode = '<?php
2424
$variable="test";echo $variable;';
2525

26-
$tempStdin = tempnam(sys_get_temp_dir(), 'test_stdin_');
27-
file_put_contents($tempStdin, $testCode);
28-
29-
$command = sprintf('cat %s | ./pint --stdin --preset=psr12', escapeshellarg($tempStdin));
30-
$output = shell_exec($command);
31-
32-
unlink($tempStdin);
26+
[$statusCode, $output] = runWithStdin($testCode, [
27+
'--stdin' => true,
28+
'--preset' => 'psr12',
29+
]);
3330

3431
expect($output)->toContain('<?php')
3532
->and($output)->toContain('$variable = "test";')
@@ -43,13 +40,11 @@
4340
$testCode = '<?php
4441
$variable="test";';
4542

46-
$tempStdin = tempnam(sys_get_temp_dir(), 'test_stdin_');
47-
file_put_contents($tempStdin, $testCode);
48-
49-
$command = sprintf('cat %s | ./pint --stdin --silent --preset=psr12', escapeshellarg($tempStdin));
50-
$output = shell_exec($command);
51-
52-
unlink($tempStdin);
43+
[$statusCode, $output] = runWithStdin($testCode, [
44+
'--stdin' => true,
45+
'--silent' => true,
46+
'--preset' => 'psr12',
47+
]);
5348

5449
expect($output)->toContain('<?php')
5550
->and($output)->toContain('$variable = "test";')
@@ -61,16 +56,13 @@
6156
$testCode = '<?php
6257
$variable="test";';
6358

64-
$tempStdin = tempnam(sys_get_temp_dir(), 'test_stdin_');
65-
file_put_contents($tempStdin, $testCode);
66-
67-
$command = sprintf('cat %s | ./pint --stdin --test --preset=psr12 2>&1', escapeshellarg($tempStdin));
68-
$output = shell_exec($command);
69-
$exitCode = shell_exec(sprintf('cat %s | ./pint --stdin --test --preset=psr12 >/dev/null 2>&1; echo $?', escapeshellarg($tempStdin)));
70-
71-
unlink($tempStdin);
59+
[$statusCode, $output] = runWithStdin($testCode, [
60+
'--stdin' => true,
61+
'--test' => true,
62+
'--preset' => 'psr12',
63+
]);
7264

73-
expect(trim($exitCode))->toBe('1')
65+
expect($statusCode)->toBe(1)
7466
->and($output)->toContain('<?php')
7567
->and($output)->toContain('$variable="test";');
7668
});
@@ -79,13 +71,10 @@
7971
$testCode = '<?php
8072
$variable="test";if($variable=="test"){echo"Hello World";}';
8173

82-
$tempStdin = tempnam(sys_get_temp_dir(), 'test_stdin_');
83-
file_put_contents($tempStdin, $testCode);
84-
85-
$command = sprintf('cat %s | ./pint --stdin --preset=psr12', escapeshellarg($tempStdin));
86-
$output = shell_exec($command);
87-
88-
unlink($tempStdin);
74+
[$statusCode, $output] = runWithStdin($testCode, [
75+
'--stdin' => true,
76+
'--preset' => 'psr12',
77+
]);
8978

9079
expect($output)->toContain('<?php')
9180
->and($output)->toContain('$variable = "test";')
@@ -99,18 +88,13 @@
9988
$testCode = '<?php
10089
$variable="test";';
10190

102-
$tempStdin = tempnam(sys_get_temp_dir(), 'test_stdin_');
10391
$testFilePath = 'app/Models/User.php';
104-
file_put_contents($tempStdin, $testCode);
105-
106-
$command = sprintf(
107-
'cat %s | ./pint %s --stdin --preset=psr12',
108-
escapeshellarg($tempStdin),
109-
escapeshellarg($testFilePath)
110-
);
111-
$output = shell_exec($command);
11292

113-
unlink($tempStdin);
93+
[$statusCode, $output] = runWithStdin($testCode, [
94+
'path' => [$testFilePath],
95+
'--stdin' => true,
96+
'--preset' => 'psr12',
97+
]);
11498

11599
expect($output)->toContain('<?php')
116100
->and($output)->toContain('$variable = "test";')
@@ -119,25 +103,24 @@
119103
});
120104

121105
it('exits successfully with empty stdin', function () {
122-
$command = 'echo "" | ./pint --stdin --preset=psr12 2>&1';
123-
$output = shell_exec($command);
124-
$exitCode = shell_exec('echo "" | ./pint --stdin --preset=psr12 >/dev/null 2>&1; echo $?');
106+
[$statusCode, $output] = runWithStdin('', [
107+
'--stdin' => true,
108+
'--preset' => 'psr12',
109+
]);
125110

126-
expect(trim($exitCode))->toBe('0')
111+
expect($statusCode)->toBe(0)
127112
->and($output)->toBeEmpty();
128113
});
129114

130115
it('outputs both formatted code and json with format option', function () {
131116
$testCode = '<?php
132117
$variable="test";';
133118

134-
$tempStdin = tempnam(sys_get_temp_dir(), 'test_stdin_');
135-
file_put_contents($tempStdin, $testCode);
136-
137-
$command = sprintf('cat %s | ./pint --stdin --preset=psr12 --format=json 2>&1', escapeshellarg($tempStdin));
138-
$output = shell_exec($command);
139-
140-
unlink($tempStdin);
119+
[$statusCode, $output] = runWithStdin($testCode, [
120+
'--stdin' => true,
121+
'--preset' => 'psr12',
122+
'--format' => 'json',
123+
]);
141124

142125
expect($output)->toContain('<?php')
143126
->and($output)->toContain('$variable = "test";')

tests/Pest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,57 @@ function run($command, $arguments)
8282

8383
return [$statusCode, $output];
8484
}
85+
86+
/**
87+
* Runs the command with mocked stdin input.
88+
*
89+
* @param string $stdinContent
90+
* @param array<string, string> $arguments
91+
* @return array{int, string}
92+
*/
93+
function runWithStdin($stdinContent, $arguments)
94+
{
95+
$tempFile = tempnam(sys_get_temp_dir(), 'pint_test_stdin_');
96+
file_put_contents($tempFile, $stdinContent);
97+
98+
$pintExecutable = file_exists('./pint') ? './pint' : 'vendor/bin/pint';
99+
100+
$command = [$pintExecutable];
101+
102+
if (isset($arguments['path'])) {
103+
$paths = is_array($arguments['path']) ? $arguments['path'] : [$arguments['path']];
104+
$command = array_merge($command, $paths);
105+
unset($arguments['path']);
106+
}
107+
108+
foreach ($arguments as $key => $value) {
109+
if (str_starts_with($key, '--')) {
110+
if ($value === true) {
111+
$command[] = $key;
112+
} else {
113+
$command[] = $key.'='.$value;
114+
}
115+
}
116+
}
117+
118+
$descriptors = [
119+
0 => ['file', $tempFile, 'r'],
120+
1 => ['pipe', 'w'],
121+
2 => ['pipe', 'w'],
122+
];
123+
124+
$process = proc_open($command, $descriptors, $pipes);
125+
126+
$output = stream_get_contents($pipes[1]);
127+
$stderr = stream_get_contents($pipes[2]);
128+
fclose($pipes[1]);
129+
fclose($pipes[2]);
130+
131+
$statusCode = proc_close($process);
132+
133+
unlink($tempFile);
134+
135+
$output = preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $output.$stderr);
136+
137+
return [$statusCode, $output];
138+
}

0 commit comments

Comments
 (0)