|
23 | 23 | $testCode = '<?php
|
24 | 24 | $variable="test";echo $variable;';
|
25 | 25 |
|
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 | + ]); |
33 | 30 |
|
34 | 31 | expect($output)->toContain('<?php')
|
35 | 32 | ->and($output)->toContain('$variable = "test";')
|
|
43 | 40 | $testCode = '<?php
|
44 | 41 | $variable="test";';
|
45 | 42 |
|
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 | + ]); |
53 | 48 |
|
54 | 49 | expect($output)->toContain('<?php')
|
55 | 50 | ->and($output)->toContain('$variable = "test";')
|
|
61 | 56 | $testCode = '<?php
|
62 | 57 | $variable="test";';
|
63 | 58 |
|
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 | + ]); |
72 | 64 |
|
73 |
| - expect(trim($exitCode))->toBe('1') |
| 65 | + expect($statusCode)->toBe(1) |
74 | 66 | ->and($output)->toContain('<?php')
|
75 | 67 | ->and($output)->toContain('$variable="test";');
|
76 | 68 | });
|
|
79 | 71 | $testCode = '<?php
|
80 | 72 | $variable="test";if($variable=="test"){echo"Hello World";}';
|
81 | 73 |
|
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 | + ]); |
89 | 78 |
|
90 | 79 | expect($output)->toContain('<?php')
|
91 | 80 | ->and($output)->toContain('$variable = "test";')
|
|
99 | 88 | $testCode = '<?php
|
100 | 89 | $variable="test";';
|
101 | 90 |
|
102 |
| - $tempStdin = tempnam(sys_get_temp_dir(), 'test_stdin_'); |
103 | 91 | $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); |
112 | 92 |
|
113 |
| - unlink($tempStdin); |
| 93 | + [$statusCode, $output] = runWithStdin($testCode, [ |
| 94 | + 'path' => [$testFilePath], |
| 95 | + '--stdin' => true, |
| 96 | + '--preset' => 'psr12', |
| 97 | + ]); |
114 | 98 |
|
115 | 99 | expect($output)->toContain('<?php')
|
116 | 100 | ->and($output)->toContain('$variable = "test";')
|
|
119 | 103 | });
|
120 | 104 |
|
121 | 105 | 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 | + ]); |
125 | 110 |
|
126 |
| - expect(trim($exitCode))->toBe('0') |
| 111 | + expect($statusCode)->toBe(0) |
127 | 112 | ->and($output)->toBeEmpty();
|
128 | 113 | });
|
129 | 114 |
|
130 | 115 | it('outputs both formatted code and json with format option', function () {
|
131 | 116 | $testCode = '<?php
|
132 | 117 | $variable="test";';
|
133 | 118 |
|
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 | + ]); |
141 | 124 |
|
142 | 125 | expect($output)->toContain('<?php')
|
143 | 126 | ->and($output)->toContain('$variable = "test";')
|
|
0 commit comments