|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Integration; |
| 4 | + |
| 5 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 6 | +use PHPUnit\Framework\Attributes\Group; |
| 7 | +use PHPUnit\Framework\Attributes\Test; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +#[Group("integration")] |
| 11 | +class ViewCsvCommandTest extends TestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @return iterable<array<string>> |
| 15 | + */ |
| 16 | + public static function fileProvider(): iterable |
| 17 | + { |
| 18 | + $file = "profile.csv"; |
| 19 | + $csvData = <<<CSV |
| 20 | + Name,Age,Location,Occupation |
| 21 | + John,30,New York,Engineer |
| 22 | + Alice,25,Los Angeles,Designer |
| 23 | + Bob,35,Chicago,Teacher |
| 24 | + Emma,28,San Francisco,Doctor |
| 25 | + Michael,40,Houston,Accountant |
| 26 | + Olivia,22,Miami,Student |
| 27 | + William,32,Seattle,Developer |
| 28 | + Sophia,27,Austin,Marketing |
| 29 | + Liam,33,Denver,Manager |
| 30 | + Ava,29,Phoenix,Writer |
| 31 | + CSV; |
| 32 | + file_put_contents($file, $csvData); |
| 33 | + |
| 34 | + yield [$file]; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @return iterable<array<string>> |
| 39 | + */ |
| 40 | + public static function invalidFileProvider(): iterable |
| 41 | + { |
| 42 | + $file = "invalidProfile.csv"; |
| 43 | + $csvData = <<<CSV |
| 44 | + Name |
| 45 | + Name Age |
| 46 | + CSV; |
| 47 | + file_put_contents($file, $csvData); |
| 48 | + |
| 49 | + yield [$file]; |
| 50 | + } |
| 51 | + |
| 52 | + #[Test] |
| 53 | + #[DataProvider('fileProvider')] |
| 54 | + public function viewCsvFileDisplayInformationCorrectly(string $file): void |
| 55 | + { |
| 56 | + $command = "php bin/view-csv {$file}"; |
| 57 | + exec($command, $output, $exitCode); |
| 58 | + $actualOutput = implode("\n", $output); |
| 59 | + |
| 60 | + $this->assertSame(0, $exitCode); |
| 61 | + $this->assertStringContainsString($file, $actualOutput); |
| 62 | + unlink($file); |
| 63 | + } |
| 64 | + |
| 65 | + #[Test] |
| 66 | + #[DataProvider('InvalidFileProvider')] |
| 67 | + public function throwExceptionIfFileIsInvalid(string $file): void |
| 68 | + { |
| 69 | + $command = "php bin/view-csv {$file}"; |
| 70 | + exec($command, $output, $exitCode); |
| 71 | + $this->assertSame(1, $exitCode); |
| 72 | + unlink($file); |
| 73 | + } |
| 74 | +} |
0 commit comments