Skip to content

Commit 9e54a18

Browse files
authored
command to view csv file
Signed-off-by: rahul <[email protected]>
1 parent d7cfc4d commit 9e54a18

File tree

7 files changed

+152
-10
lines changed

7 files changed

+152
-10
lines changed

bin/view-csv

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
require 'vendor/autoload.php';
5+
6+
use rcsofttech85\FileHandler\CsvFileHandler;
7+
use rcsofttech85\FileHandler\DI\ServiceContainer;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Input\InputArgument;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
use Symfony\Component\Console\SingleCommandApplication;
13+
use Symfony\Component\Console\Style\SymfonyStyle;
14+
15+
$command = (new SingleCommandApplication())
16+
->addArgument('csvFile', InputArgument::REQUIRED, 'csv file name')
17+
->setCode(function (InputInterface $input, OutputInterface $output): int {
18+
$io = new SymfonyStyle($input, $output);
19+
$csvFile = $input->getArgument('csvFile');
20+
21+
22+
if (!file_exists($csvFile)) {
23+
$io->error("{$csvFile} does not exists");
24+
return Command::FAILURE;
25+
}
26+
27+
$serviceContainer = new ServiceContainer();
28+
/** @var CsvFileHandler $csvFileHandler */
29+
$csvFileHandler = $serviceContainer->getContainerBuilder()->get('csv_file_handler');
30+
31+
$io->title($csvFile);
32+
$table = $io->createTable();
33+
$headers = $csvFileHandler->extractHeader($csvFile);
34+
35+
if (!$headers) {
36+
$io->error('could not extract headers');
37+
return Command::FAILURE;
38+
}
39+
40+
41+
$table->setHeaders($headers);
42+
$table->setRows($csvFileHandler->toArray($csvFile));
43+
44+
$table->render();
45+
46+
$io->newLine();
47+
48+
49+
return Command::SUCCESS;
50+
})->run();

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"symfony/var-dumper": "^6.3"
3434
},
3535
"bin": [
36-
"bin/file-diff"
36+
"bin/file-diff",
37+
"bin/view-csv"
3738
]
3839
}

src/CsvFileHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function findAndReplaceInCsv(
107107
* @return array<string>|false
108108
*/
109109

110-
private function extractHeader(mixed $file): array|false
110+
public function extractHeader(mixed $file): array|false
111111
{
112112
$headers = [];
113113
if (is_resource($file)) {

src/DI/ServiceContainer.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace rcsofttech85\FileHandler\DI;
4+
5+
use Symfony\Component\Config\FileLocator;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
8+
9+
class ServiceContainer
10+
{
11+
public function getContainerBuilder(): ContainerBuilder
12+
{
13+
$containerBuilder = new ContainerBuilder();
14+
$loader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__ . '/../../src/config'));
15+
$loader->load('services.yaml');
16+
return $containerBuilder;
17+
}
18+
}

tests/Base/BaseTest.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
namespace Base;
44

55
use PHPUnit\Framework\TestCase;
6-
use Symfony\Component\Config\FileLocator;
6+
use rcsofttech85\FileHandler\DI\ServiceContainer;
77
use Symfony\Component\DependencyInjection\ContainerBuilder;
8-
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
98

109
class BaseTest extends TestCase
1110
{
@@ -19,10 +18,8 @@ class BaseTest extends TestCase
1918
public static function setUpBeforeClass(): void
2019
{
2120
parent::setUpBeforeClass();
22-
self::$containerBuilder = new ContainerBuilder();
23-
$loader = new YamlFileLoader(self::$containerBuilder, new FileLocator(__DIR__ . '/../../src/config'));
24-
$loader->load('services.yaml');
25-
21+
$serviceContainer = new ServiceContainer();
22+
self::$containerBuilder = $serviceContainer->getContainerBuilder();
2623
$content = "Film,Genre,Lead Studio,Audience score %,Profitability,Rotten Tomatoes %,Worldwide Gross,Year\n"
2724
. "Zack and Miri Make a Porno,Romance,The Weinstein Company,70,1.747541667,64,$41.94 ,2008\n"
2825
. "Youth in Revolt,Comedy,The Weinstein Company,52,1.09,68,$19.62 ,2010\n"

tests/Integration/FileDiffCommandTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace Integration;
44

55
use PHPUnit\Framework\Attributes\DataProvider;
6+
use PHPUnit\Framework\Attributes\Group;
67
use PHPUnit\Framework\Attributes\Test;
78
use PHPUnit\Framework\TestCase;
89

10+
#[Group("integration")]
911
class FileDiffCommandTest extends TestCase
1012
{
1113
public static function tearDownAfterClass(): void
@@ -57,7 +59,7 @@ public function fileDiffShowsCorrectChanges(string $oldFile, string $newFile, st
5759
$this->assertStringContainsString($expected, $actualOutput);
5860

5961

60-
$this->assertEquals(0, $exitCode);
62+
$this->assertSame(0, $exitCode);
6163
}
6264

6365
#[Test]
@@ -86,6 +88,6 @@ public function sameContentShouldNotBeDisplayedInTheResult(string $oldFile, stri
8688
$expected = "Old (Line 3)";
8789

8890
$this->assertStringNotContainsString($expected, $actualOutput);
89-
$this->assertEquals(0, $exitCode);
91+
$this->assertSame(0, $exitCode);
9092
}
9193
}
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)