|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Project; |
| 4 | + |
| 5 | +beforeEach(function () { |
| 6 | + Project::cleanupStdinTempFile(); |
| 7 | +}); |
| 8 | + |
| 9 | +afterEach(function () { |
| 10 | + Project::cleanupStdinTempFile(); |
| 11 | +}); |
| 12 | + |
| 13 | +it('has stdin option', function () { |
| 14 | + $command = resolve(App\Commands\DefaultCommand::class); |
| 15 | + $definition = $command->getDefinition(); |
| 16 | + |
| 17 | + expect($definition->hasOption('stdin'))->toBeTrue() |
| 18 | + ->and($definition->getOption('stdin')->getDescription()) |
| 19 | + ->toBe('Read and format code from standard input'); |
| 20 | +}); |
| 21 | + |
| 22 | +it('outputs clean formatted code without summary', function () { |
| 23 | + $testCode = '<?php |
| 24 | +$variable="test";echo $variable;'; |
| 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); |
| 33 | + |
| 34 | + expect($output)->toContain('<?php') |
| 35 | + ->and($output)->toContain('$variable = "test";') |
| 36 | + ->and($output)->toContain('echo $variable;') |
| 37 | + ->and($output)->not->toContain('FIXED') |
| 38 | + ->and($output)->not->toContain('Laravel') |
| 39 | + ->and($output)->not->toContain('──────'); |
| 40 | +}); |
| 41 | + |
| 42 | +it('outputs formatted code with silent flag', function () { |
| 43 | + $testCode = '<?php |
| 44 | +$variable="test";'; |
| 45 | + |
| 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); |
| 53 | + |
| 54 | + expect($output)->toContain('<?php') |
| 55 | + ->and($output)->toContain('$variable = "test";') |
| 56 | + ->and($output)->not->toContain('FIXED') |
| 57 | + ->and($output)->not->toContain('Laravel'); |
| 58 | +}); |
| 59 | + |
| 60 | +it('returns exit code 1 in test mode with style issues', function () { |
| 61 | + $testCode = '<?php |
| 62 | +$variable="test";'; |
| 63 | + |
| 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); |
| 72 | + |
| 73 | + expect(trim($exitCode))->toBe('1') |
| 74 | + ->and($output)->toContain('<?php') |
| 75 | + ->and($output)->toContain('$variable="test";'); |
| 76 | +}); |
| 77 | + |
| 78 | +it('formats code with complex statements', function () { |
| 79 | + $testCode = '<?php |
| 80 | +$variable="test";if($variable=="test"){echo"Hello World";}'; |
| 81 | + |
| 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); |
| 89 | + |
| 90 | + expect($output)->toContain('<?php') |
| 91 | + ->and($output)->toContain('$variable = "test";') |
| 92 | + ->and($output)->toContain('if ($variable == "test") {') |
| 93 | + ->and($output)->toContain('echo"Hello World";') |
| 94 | + ->and($output)->not->toContain('FIXED') |
| 95 | + ->and($output)->not->toContain('Laravel'); |
| 96 | +}); |
| 97 | + |
| 98 | +it('accepts path argument for config resolution', function () { |
| 99 | + $testCode = '<?php |
| 100 | +$variable="test";'; |
| 101 | + |
| 102 | + $tempStdin = tempnam(sys_get_temp_dir(), 'test_stdin_'); |
| 103 | + $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 | + |
| 113 | + unlink($tempStdin); |
| 114 | + |
| 115 | + expect($output)->toContain('<?php') |
| 116 | + ->and($output)->toContain('$variable = "test";') |
| 117 | + ->and($output)->not->toContain('FIXED') |
| 118 | + ->and($output)->not->toContain('Laravel'); |
| 119 | +}); |
| 120 | + |
| 121 | +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 $?'); |
| 125 | + |
| 126 | + expect(trim($exitCode))->toBe('0') |
| 127 | + ->and($output)->toBeEmpty(); |
| 128 | +}); |
| 129 | + |
| 130 | +it('outputs both formatted code and json with format option', function () { |
| 131 | + $testCode = '<?php |
| 132 | +$variable="test";'; |
| 133 | + |
| 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); |
| 141 | + |
| 142 | + expect($output)->toContain('<?php') |
| 143 | + ->and($output)->toContain('$variable = "test";') |
| 144 | + ->and($output)->toContain('"files":['); |
| 145 | +}); |
0 commit comments