Skip to content
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
47 changes: 19 additions & 28 deletions src/Command/CommandExtractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace App\Command;

use App\Imports\Themes\ExtractService;
use App\Imports\Themes\ThemeReader;
use Doctrine\ORM\EntityManagerInterface;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Symfony\Component\Console\Attribute\AsCommand;
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\Style\SymfonyStyle;
Expand All @@ -19,6 +21,7 @@ class CommandExtractService extends Command
{
private $projectDir;
private $entityManager;
private ?Worksheet $sheet = null;

public function __construct(string $projectDir, EntityManagerInterface $entityManager)
{
Expand All @@ -27,42 +30,30 @@ public function __construct(string $projectDir, EntityManagerInterface $entityMa
parent::__construct();
}

protected function configure(): void
{
$this
->addArgument('extracthemes', InputArgument::OPTIONAL, 'extract and save themes into database themes');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$extracthemes = $input->getArgument('extracthemes');
$extract_service = new ExtractService($this->entityManager);

if ($extracthemes) {
$excel_file = $this->projectDir.'/var/import-data/emissions_GES_structure.xlsx';
$excle_file = $this->projectDir.'/var/import-data/emissions_GES_structure.xlsx';
if (!file_exists($excle_file)) {
$io->error('file does not exist');

if (!file_exists($excel_file)) {
$io->error('file does not exist');

return Command::FAILURE;
}
return Command::FAILURE;
}

try {
$extracted_themes = $extract_service->GetThemesFromExcelFile($excel_file);
$io->info(count($extracted_themes).' themes extracted');
$prepared_themes = $extract_service->PrepareThemesForDatabase($extracted_themes);
$saved_themes_count = $extract_service->SaveThemesOnDatabase($prepared_themes);
$spreadsheet = IOFactory::load($excle_file);

$io->info("$saved_themes_count themes were upserted successfuly");
} catch (\Exception $e) {
$io->error('File Excel failed to read : '.$e->getMessage());
$this->sheet = $spreadsheet->getActiveSheet();
$io->info('Sheet loaded ...');
$readtheme = new ThemeReader($this->sheet);
$themes = $readtheme->extract();

return Command::FAILURE;
}
$io->info(count($themes).' themes extracte successfully');
$extract_service = new ExtractService($this->entityManager, $this->sheet);
$extracted_themes = $extract_service->PrepareThemesForDatabase($themes);

return Command::SUCCESS;
}
$saved_themes_count = $extract_service->SaveThemesOnDatabase($extracted_themes);
$io->info("$saved_themes_count themes were saved successfuly");

return Command::SUCCESS;
}
Expand Down
38 changes: 4 additions & 34 deletions src/Imports/Themes/ExtractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,19 @@

use App\Entity\Theme;
use Doctrine\ORM\EntityManagerInterface;
use PhpOffice\PhpSpreadsheet\IOFactory;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class ExtractService
{
private $entityManager;
private $themeRepository;
private $worksheet;

public function __construct(EntityManagerInterface $entityManager)
public function __construct(EntityManagerInterface $entityManager, Worksheet $worksheet)
{
$this->entityManager = $entityManager;
$this->themeRepository = $entityManager->getRepository(Theme::class);
}

/**
* Get Themes From Excel File.
*
* @return array themes import from the excel file
*/
public function GetThemesFromExcelFile(string $excel_file): array
{
$themes = [];
if (!file_exists($excel_file)) {
throw new FileNotFoundException(sprintf('Excel file "%s" not found', $excel_file));
}
$spreadsheet = IOFactory::load($excel_file);
$sheet = $spreadsheet->getActiveSheet();

foreach ($sheet->getRowIterator() as $row) {
$rowIndex = $row->getRowIndex();
$name = $sheet->getCell('A'.$rowIndex)->getValue();
$external_id = $sheet->getCell('B'.$rowIndex)->getValue();
if (null !== $name && null !== $external_id) {
if ($rowIndex > 2) {
$themes[] = [
'externalId' => $external_id,
'name' => $name,
];
}
}
}

return $themes;
$this->worksheet = $worksheet;
}

private function getParentExternalId(string $externalId): ?string
Expand Down
34 changes: 34 additions & 0 deletions src/Imports/Themes/ThemeReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Imports\Themes;

use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class ThemeReader
{
public function __construct(
private readonly Worksheet $sheet,
) {
}

public function extract(): array
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maintenant tu peux tester extract meme sans ton fichier originel: https://github.com/unflores/di_example/blob/main/src/test/ThemeReaderTest.php#L22-L27

{
$themes = [];
foreach ($this->sheet->getRowIterator() as $row) {
$rowIndex = $row->getRowIndex();
$name = $this->sheet->getCell('A'.$rowIndex)->getValue();
$external_id = $this->sheet->getCell('B'.$rowIndex)->getValue();

if (null !== $external_id && null !== $name) {
if ($rowIndex > 2) {
$themes[] = [
'externalId' => $external_id,
'name' => $name,
];
}
}
}

return $themes;
}
}
22 changes: 16 additions & 6 deletions tests/Imports/Themes/ExtractServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

use App\Entity\Theme;
use App\Imports\Themes\ExtractService;
use App\Imports\Themes\ThemeReader;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class ExtractServiceTest extends KernelTestCase
{
private $entityManager;
private $themeRepository;
private $projectDir;
private ?Worksheet $sheet;
private $extract_service;

protected function setUp(): void
{
Expand All @@ -20,6 +25,13 @@ protected function setUp(): void
$this->entityManager = $container->get('doctrine.orm.entity_manager');
$this->themeRepository = $this->entityManager->getRepository(Theme::class);
$this->projectDir = $container->getParameter('kernel.project_dir');
$excel_file = $this->projectDir.'/tests/Imports/Themes/test-themes.xlsx';
if (!file_exists($excel_file)) {
throw new \Exception('file does not exist');
}
$spreadsheet = IOFactory::load($excel_file);
$this->sheet = $spreadsheet->getActiveSheet();
$this->extract_service = new ExtractService($this->entityManager, $this->sheet);
}

protected function tearDown(): void
Expand All @@ -34,12 +46,10 @@ protected function tearDown(): void

public function testImportThemeSave(): void
{
$ExtractServices = new ExtractService($this->entityManager);
// TODO: change this to inject the spreadsheet so that we can test the integration more easily.
$excel_file = $this->projectDir.'/tests/Imports/Themes/test-themes.xlsx';
$themes = $ExtractServices->GetThemesFromExcelFile($excel_file);
$preparedThemes = $ExtractServices->PrepareThemesForDatabase($themes);
$savedThemesCount = $ExtractServices->SaveThemesOnDatabase($preparedThemes);
$read_themes = new ThemeReader($this->sheet);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tu ne dois pas melanger les tests. Normalment tu testerait theme reader seul, extract service seul et tu auras un test d'integration pour les deux. Ce que j'avais en tete etait plutot: https://github.com/unflores/di_example/blob/main/src/test/ThemeReaderTest.php#L22-L32

Typiquement ce dont je parle s'appelle un test unitaire, parce qu'il y a un seul unit(ou module) de code testé. Brèf, tu pourrais le changer plutard si tu as envie mais pour l'instant on peut avancer je pense...

$themes = $read_themes->extract();
$preparedThemes = $this->extract_service->PrepareThemesForDatabase($themes);
$savedThemesCount = $this->extract_service->SaveThemesOnDatabase($preparedThemes);

$this->assertEquals(
$savedThemesCount,
Expand Down