-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: rahul <rcsofttech85@gmail.com>
- Loading branch information
1 parent
d7cfc4d
commit 9e54a18
Showing
7 changed files
with
152 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
use rcsofttech85\FileHandler\CsvFileHandler; | ||
use rcsofttech85\FileHandler\DI\ServiceContainer; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\SingleCommandApplication; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
$command = (new SingleCommandApplication()) | ||
->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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
"symfony/var-dumper": "^6.3" | ||
}, | ||
"bin": [ | ||
"bin/file-diff" | ||
"bin/file-diff", | ||
"bin/view-csv" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace rcsofttech85\FileHandler\DI; | ||
|
||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; | ||
|
||
class ServiceContainer | ||
{ | ||
public function getContainerBuilder(): ContainerBuilder | ||
{ | ||
$containerBuilder = new ContainerBuilder(); | ||
$loader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__ . '/../../src/config')); | ||
$loader->load('services.yaml'); | ||
return $containerBuilder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Integration; | ||
|
||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\Attributes\Group; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[Group("integration")] | ||
class ViewCsvCommandTest extends TestCase | ||
{ | ||
/** | ||
* @return iterable<array<string>> | ||
*/ | ||
public static function fileProvider(): iterable | ||
{ | ||
$file = "profile.csv"; | ||
$csvData = <<<CSV | ||
Name,Age,Location,Occupation | ||
John,30,New York,Engineer | ||
Alice,25,Los Angeles,Designer | ||
Bob,35,Chicago,Teacher | ||
Emma,28,San Francisco,Doctor | ||
Michael,40,Houston,Accountant | ||
Olivia,22,Miami,Student | ||
William,32,Seattle,Developer | ||
Sophia,27,Austin,Marketing | ||
Liam,33,Denver,Manager | ||
Ava,29,Phoenix,Writer | ||
CSV; | ||
file_put_contents($file, $csvData); | ||
|
||
yield [$file]; | ||
} | ||
|
||
/** | ||
* @return iterable<array<string>> | ||
*/ | ||
public static function invalidFileProvider(): iterable | ||
{ | ||
$file = "invalidProfile.csv"; | ||
$csvData = <<<CSV | ||
Name | ||
Name Age | ||
CSV; | ||
file_put_contents($file, $csvData); | ||
|
||
yield [$file]; | ||
} | ||
|
||
#[Test] | ||
#[DataProvider('fileProvider')] | ||
public function viewCsvFileDisplayInformationCorrectly(string $file): void | ||
{ | ||
$command = "php bin/view-csv {$file}"; | ||
exec($command, $output, $exitCode); | ||
$actualOutput = implode("\n", $output); | ||
|
||
$this->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); | ||
} | ||
} |