From 9e54a18f0e722fe1dfc78198cd8e9cbfac8485e2 Mon Sep 17 00:00:00 2001 From: rcsofttech85 Date: Tue, 19 Sep 2023 21:04:05 +0530 Subject: [PATCH] command to view csv file Signed-off-by: rahul --- bin/view-csv | 50 +++++++++++++++ composer.json | 3 +- src/CsvFileHandler.php | 2 +- src/DI/ServiceContainer.php | 18 ++++++ tests/Base/BaseTest.php | 9 +-- tests/Integration/FileDiffCommandTest.php | 6 +- tests/Integration/ViewCsvCommandTest.php | 74 +++++++++++++++++++++++ 7 files changed, 152 insertions(+), 10 deletions(-) create mode 100755 bin/view-csv create mode 100644 src/DI/ServiceContainer.php create mode 100644 tests/Integration/ViewCsvCommandTest.php diff --git a/bin/view-csv b/bin/view-csv new file mode 100755 index 0000000..56ac830 --- /dev/null +++ b/bin/view-csv @@ -0,0 +1,50 @@ +#!/usr/bin/env php +addArgument('csvFile', InputArgument::REQUIRED, 'csv file name') + ->setCode(function (InputInterface $input, OutputInterface $output): int { + $io = new SymfonyStyle($input, $output); + $csvFile = $input->getArgument('csvFile'); + + + if (!file_exists($csvFile)) { + $io->error("{$csvFile} does not exists"); + return Command::FAILURE; + } + + $serviceContainer = new ServiceContainer(); + /** @var CsvFileHandler $csvFileHandler */ + $csvFileHandler = $serviceContainer->getContainerBuilder()->get('csv_file_handler'); + + $io->title($csvFile); + $table = $io->createTable(); + $headers = $csvFileHandler->extractHeader($csvFile); + + if (!$headers) { + $io->error('could not extract headers'); + return Command::FAILURE; + } + + + $table->setHeaders($headers); + $table->setRows($csvFileHandler->toArray($csvFile)); + + $table->render(); + + $io->newLine(); + + + return Command::SUCCESS; + })->run(); diff --git a/composer.json b/composer.json index 8e5c333..bb06ad8 100644 --- a/composer.json +++ b/composer.json @@ -33,6 +33,7 @@ "symfony/var-dumper": "^6.3" }, "bin": [ - "bin/file-diff" + "bin/file-diff", + "bin/view-csv" ] } diff --git a/src/CsvFileHandler.php b/src/CsvFileHandler.php index ce308d1..8928637 100644 --- a/src/CsvFileHandler.php +++ b/src/CsvFileHandler.php @@ -107,7 +107,7 @@ public function findAndReplaceInCsv( * @return array|false */ - private function extractHeader(mixed $file): array|false + public function extractHeader(mixed $file): array|false { $headers = []; if (is_resource($file)) { diff --git a/src/DI/ServiceContainer.php b/src/DI/ServiceContainer.php new file mode 100644 index 0000000..651f959 --- /dev/null +++ b/src/DI/ServiceContainer.php @@ -0,0 +1,18 @@ +load('services.yaml'); + return $containerBuilder; + } +} diff --git a/tests/Base/BaseTest.php b/tests/Base/BaseTest.php index c0aa97c..7903386 100644 --- a/tests/Base/BaseTest.php +++ b/tests/Base/BaseTest.php @@ -3,9 +3,8 @@ namespace Base; use PHPUnit\Framework\TestCase; -use Symfony\Component\Config\FileLocator; +use rcsofttech85\FileHandler\DI\ServiceContainer; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; class BaseTest extends TestCase { @@ -19,10 +18,8 @@ class BaseTest extends TestCase public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); - self::$containerBuilder = new ContainerBuilder(); - $loader = new YamlFileLoader(self::$containerBuilder, new FileLocator(__DIR__ . '/../../src/config')); - $loader->load('services.yaml'); - + $serviceContainer = new ServiceContainer(); + self::$containerBuilder = $serviceContainer->getContainerBuilder(); $content = "Film,Genre,Lead Studio,Audience score %,Profitability,Rotten Tomatoes %,Worldwide Gross,Year\n" . "Zack and Miri Make a Porno,Romance,The Weinstein Company,70,1.747541667,64,$41.94 ,2008\n" . "Youth in Revolt,Comedy,The Weinstein Company,52,1.09,68,$19.62 ,2010\n" diff --git a/tests/Integration/FileDiffCommandTest.php b/tests/Integration/FileDiffCommandTest.php index 8f49507..0990987 100644 --- a/tests/Integration/FileDiffCommandTest.php +++ b/tests/Integration/FileDiffCommandTest.php @@ -3,9 +3,11 @@ namespace Integration; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; +#[Group("integration")] class FileDiffCommandTest extends TestCase { public static function tearDownAfterClass(): void @@ -57,7 +59,7 @@ public function fileDiffShowsCorrectChanges(string $oldFile, string $newFile, st $this->assertStringContainsString($expected, $actualOutput); - $this->assertEquals(0, $exitCode); + $this->assertSame(0, $exitCode); } #[Test] @@ -86,6 +88,6 @@ public function sameContentShouldNotBeDisplayedInTheResult(string $oldFile, stri $expected = "Old (Line 3)"; $this->assertStringNotContainsString($expected, $actualOutput); - $this->assertEquals(0, $exitCode); + $this->assertSame(0, $exitCode); } } diff --git a/tests/Integration/ViewCsvCommandTest.php b/tests/Integration/ViewCsvCommandTest.php new file mode 100644 index 0000000..0730e7b --- /dev/null +++ b/tests/Integration/ViewCsvCommandTest.php @@ -0,0 +1,74 @@ +> + */ + public static function fileProvider(): iterable + { + $file = "profile.csv"; + $csvData = <<> + */ + public static function invalidFileProvider(): iterable + { + $file = "invalidProfile.csv"; + $csvData = <<assertSame(0, $exitCode); + $this->assertStringContainsString($file, $actualOutput); + unlink($file); + } + + #[Test] + #[DataProvider('InvalidFileProvider')] + public function throwExceptionIfFileIsInvalid(string $file): void + { + $command = "php bin/view-csv {$file}"; + exec($command, $output, $exitCode); + $this->assertSame(1, $exitCode); + unlink($file); + } +}