Skip to content

command to view csv file #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions bin/view-csv
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();
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"symfony/var-dumper": "^6.3"
},
"bin": [
"bin/file-diff"
"bin/file-diff",
"bin/view-csv"
]
}
2 changes: 1 addition & 1 deletion src/CsvFileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function findAndReplaceInCsv(
* @return array<string>|false
*/

private function extractHeader(mixed $file): array|false
public function extractHeader(mixed $file): array|false
{
$headers = [];
if (is_resource($file)) {
Expand Down
18 changes: 18 additions & 0 deletions src/DI/ServiceContainer.php
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;
}
}
9 changes: 3 additions & 6 deletions tests/Base/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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"
Expand Down
6 changes: 4 additions & 2 deletions tests/Integration/FileDiffCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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);
}
}
74 changes: 74 additions & 0 deletions tests/Integration/ViewCsvCommandTest.php
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);
}
}