-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ajoute la commande app:storage_area_generate
- Loading branch information
1 parent
1d91c38
commit 9e911bb
Showing
2 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
src/Infrastructure/Symfony/Command/StorageAreaGenerateCommand.php
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,103 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Infrastructure\Symfony\Command; | ||
|
||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
#[AsCommand( | ||
name: 'app:storage_area:generate', | ||
description: 'Generate migration lines for data/aires_de_stockage.csv', | ||
hidden: false, | ||
)] | ||
class StorageAreaGenerateCommand extends Command | ||
{ | ||
public function __construct( | ||
private readonly string $projectDir, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
private function parseCsvAssociative(string $csv): array | ||
{ | ||
$lines = explode(PHP_EOL, trim($csv)); | ||
$fieldNames = str_getcsv($lines[0], ','); | ||
$rows = []; | ||
|
||
foreach ($lines as $line) { | ||
$values = str_getcsv($line, ','); | ||
$row = []; | ||
|
||
foreach ($fieldNames as $index => $name) { | ||
$row[$name] = $values[$index]; | ||
} | ||
|
||
$rows[] = $row; | ||
} | ||
|
||
return $rows; | ||
} | ||
|
||
private function makePhp(array $rows): string | ||
{ | ||
$lines = []; | ||
|
||
$columns = [ | ||
'uuid', | ||
'description', | ||
'administrator', | ||
'road_number', | ||
'from_point_number', | ||
'from_side', | ||
'from_abscissa', | ||
'to_point_number', | ||
'to_side', | ||
'to_abscissa', | ||
'geometry', | ||
]; | ||
|
||
$valuesList = []; | ||
|
||
foreach ($rows as $row) { | ||
$values = [ | ||
'uuid' => 'uuid_generate_v4()', | ||
'description' => "''", | ||
'administrator' => "''", | ||
'road_number' => "''", | ||
'from_point_number' => "'10'", | ||
'from_side' => "'D'", | ||
'from_abscissa' => '0', | ||
'to_point_number' => "'11'", | ||
'to_side' => "'D'", | ||
'to_abscissa' => '0', | ||
'geometry' => "ST_MakePoint('0 1')", | ||
]; | ||
|
||
$valuesList[] = \sprintf(' (%s)', implode(', ', $values)); | ||
} | ||
|
||
$statement = \sprintf( | ||
'INSERT INTO storage_area (%s) VALUES%s%s', | ||
implode(', ', $columns), | ||
PHP_EOL, | ||
implode(\sprintf(',%s', PHP_EOL), $valuesList), | ||
); | ||
|
||
$lines[] = \sprintf('$this->addSql(\'%s\');', $statement); | ||
|
||
return implode(PHP_EOL, $lines); | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$csv = file_get_contents(\sprintf('%s/data/aires_de_stockage.csv', $this->projectDir)); | ||
$rows = $this->parseCsvAssociative($csv); | ||
$output->writeln($this->makePhp($rows)); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
tests/Integration/Infrastructure/Symfony/Command/StorageAreaGenerateCommandTest.php
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Integration\Infrastructure\Symfony\Command; | ||
|
||
use App\Infrastructure\Symfony\Command\StorageAreaGenerateCommand; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
/** | ||
* @group only | ||
*/ | ||
final class StorageAreaGenerateCommandTest extends KernelTestCase | ||
{ | ||
public function testExecute(): void | ||
{ | ||
self::bootKernel(); | ||
$container = static::getContainer(); | ||
|
||
$command = $container->get(StorageAreaGenerateCommand::class); | ||
$commandTester = new CommandTester($command); | ||
|
||
$commandTester->execute([]); | ||
$commandTester->assertCommandIsSuccessful(); | ||
|
||
$this->assertEmpty($commandTester->getDisplay()); | ||
} | ||
} |