From 8af8c7c6aae4f7dda3e46365a83408a1f9be33cf Mon Sep 17 00:00:00 2001 From: enesaktay Date: Tue, 29 May 2018 16:21:25 +0200 Subject: [PATCH 1/3] fixed the excel export --- src/Command/ExportDataCommand.php | 1 + src/Exporter/ResourceExporter.php | 8 ++++++++ src/Writer/CsvWriter.php | 10 +++++++++- src/Writer/ExcelWriter.php | 10 +++++++++- src/Writer/WriterInterface.php | 5 +++++ 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Command/ExportDataCommand.php b/src/Command/ExportDataCommand.php index 5af10f8c..979a53ed 100644 --- a/src/Command/ExportDataCommand.php +++ b/src/Command/ExportDataCommand.php @@ -92,6 +92,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $service->setExportFile($file); $service->export($idsToExport); + $service->finish(); $message = sprintf( "Exported %d item(s) to '%s' via the %s exporter", diff --git a/src/Exporter/ResourceExporter.php b/src/Exporter/ResourceExporter.php index 4f170e17..3cab8015 100644 --- a/src/Exporter/ResourceExporter.php +++ b/src/Exporter/ResourceExporter.php @@ -107,4 +107,12 @@ protected function getDataForId(string $id): array return $data; } + + /** + * Wrap up the writer after all items have been written + */ + public function finish(): void + { + $this->writer->finish(); + } } diff --git a/src/Writer/CsvWriter.php b/src/Writer/CsvWriter.php index 7891dbab..53b59186 100644 --- a/src/Writer/CsvWriter.php +++ b/src/Writer/CsvWriter.php @@ -52,8 +52,16 @@ public function getFileContent(): string rewind($this->writer->getStream()); $contents = stream_get_contents($this->writer->getStream()); - $this->writer->finish(); + $this->finish(); return $contents; } + + /** + * Wrap up the writer after all items have been written + */ + public function finish() + { + $this->writer->finish(); + } } diff --git a/src/Writer/ExcelWriter.php b/src/Writer/ExcelWriter.php index 6335ff4f..fde4fa32 100644 --- a/src/Writer/ExcelWriter.php +++ b/src/Writer/ExcelWriter.php @@ -66,7 +66,7 @@ public function setFile(string $filename): void */ public function getFileContent(): string { - $this->writer->finish(); + $this->finish(); $contents = file_get_contents($this->filename); @@ -86,4 +86,12 @@ private function prepare(): void $this->writer->prepare(); } } + + /** + * Wrap up the writer after all items have been written + */ + public function finish() + { + $this->writer->finish(); + } } diff --git a/src/Writer/WriterInterface.php b/src/Writer/WriterInterface.php index 62477260..3d36447c 100644 --- a/src/Writer/WriterInterface.php +++ b/src/Writer/WriterInterface.php @@ -20,4 +20,9 @@ public function setFile(string $filename): void; * @return string */ public function getFileContent(): string; + + /** + * Wrap up the writer after all items have been written + */ + public function finish(): void; } From 21710fdb2a9abe8ee10715df1a75960140b25504 Mon Sep 17 00:00:00 2001 From: enesaktay Date: Tue, 29 May 2018 16:31:45 +0200 Subject: [PATCH 2/3] phpstan fix --- src/Exporter/ResourceExporterInterface.php | 5 +++++ src/Writer/CsvWriter.php | 4 ++-- src/Writer/ExcelWriter.php | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Exporter/ResourceExporterInterface.php b/src/Exporter/ResourceExporterInterface.php index d8138671..1f878e61 100644 --- a/src/Exporter/ResourceExporterInterface.php +++ b/src/Exporter/ResourceExporterInterface.php @@ -20,4 +20,9 @@ public function setExportFile(string $filename): void; * @return string */ public function getExportedData(): string; + + /** + * Wrap up the writer after all items have been written + */ + public function finish(): void; } diff --git a/src/Writer/CsvWriter.php b/src/Writer/CsvWriter.php index 53b59186..8bbc6434 100644 --- a/src/Writer/CsvWriter.php +++ b/src/Writer/CsvWriter.php @@ -58,9 +58,9 @@ public function getFileContent(): string } /** - * Wrap up the writer after all items have been written + * {@inheritdoc} */ - public function finish() + public function finish(): void { $this->writer->finish(); } diff --git a/src/Writer/ExcelWriter.php b/src/Writer/ExcelWriter.php index fde4fa32..c9c875ca 100644 --- a/src/Writer/ExcelWriter.php +++ b/src/Writer/ExcelWriter.php @@ -88,9 +88,9 @@ private function prepare(): void } /** - * Wrap up the writer after all items have been written + * {@inheritdoc} */ - public function finish() + public function finish(): void { $this->writer->finish(); } From 9515d2c86e8005d13cabddd100ce655736183269 Mon Sep 17 00:00:00 2001 From: enesaktay Date: Tue, 19 Jun 2018 13:55:49 +0200 Subject: [PATCH 3/3] add phpspec tests --- .gitignore | 4 +++- spec/Writer/CsvWriterSpec.php | 8 ++++++++ spec/Writer/ExcelWriterSpec.php | 2 ++ src/Exporter/JsonResourceExporter.php | 8 ++++++++ tests/Behat/Context/CliBaseContext.php | 2 +- tests/Behat/Resources/fixtures/countries_export.json | 1 - .../fixtures/{countries_export.xlsx => export/.gitkeep} | 0 7 files changed, 22 insertions(+), 3 deletions(-) delete mode 100644 tests/Behat/Resources/fixtures/countries_export.json rename tests/Behat/Resources/fixtures/{countries_export.xlsx => export/.gitkeep} (100%) diff --git a/.gitignore b/.gitignore index 49472186..2e07870b 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,6 @@ !/etc/build/.gitkeep /tests/Application/yarn.lock -/tests/Behat/Resources/fixtures/countries_export.csv + +/tests/Behat/Resources/fixtures/export/* +!/tests/Behat/Resources/fixtures/export/.gitkeep diff --git a/spec/Writer/CsvWriterSpec.php b/spec/Writer/CsvWriterSpec.php index 34eca56d..c3a4c087 100644 --- a/spec/Writer/CsvWriterSpec.php +++ b/spec/Writer/CsvWriterSpec.php @@ -36,4 +36,12 @@ function it_delegates_the_data_to_the_wrapped_writer(Writer $csvWriter) $csvWriter->writeItem($data)->shouldBeCalled(); $this->write($data); } + + function it_finishes_the_file_creation_when_we_get_the_contents(Writer $csvWriter) + { + $csvWriter->setCloseStreamOnFinish(true)->shouldBeCalled(); + $csvWriter->getStream()->willReturn(fopen('php://memory', 'w')); + $csvWriter->finish()->shouldBeCalled(); + $this->getFileContent(); + } } diff --git a/spec/Writer/ExcelWriterSpec.php b/spec/Writer/ExcelWriterSpec.php index 3cf12678..f6cb336d 100644 --- a/spec/Writer/ExcelWriterSpec.php +++ b/spec/Writer/ExcelWriterSpec.php @@ -36,6 +36,8 @@ function it_delegates_the_data_to_the_wrapped_writer(\Port\Excel\ExcelWriter $ex ]; $excelWriter->prepare()->shouldBeCalled(); $excelWriter->writeItem($data)->shouldBeCalled(); + $excelWriter->finish()->shouldBeCalled(); $this->write($data); + $this->finish(); } } diff --git a/src/Exporter/JsonResourceExporter.php b/src/Exporter/JsonResourceExporter.php index 6fbd0c2f..248bf7d1 100644 --- a/src/Exporter/JsonResourceExporter.php +++ b/src/Exporter/JsonResourceExporter.php @@ -64,4 +64,12 @@ public function setExportFile(string $filename): void { $this->filename = $filename; } + + /** + * {@inheritdoc} + */ + public function finish(): void + { + // no finish needed + } } diff --git a/tests/Behat/Context/CliBaseContext.php b/tests/Behat/Context/CliBaseContext.php index 66029b82..7589dedb 100644 --- a/tests/Behat/Context/CliBaseContext.php +++ b/tests/Behat/Context/CliBaseContext.php @@ -133,6 +133,6 @@ public function iExportDataToSpecificFiletypeFileWithTheCliCommand($exporterType $this->application->add($command); $this->command = $this->application->find('sylius:export'); $this->tester = new CommandTester($this->command); - $this->tester->execute(['command' => 'sylius:export', 'exporter' => $exporterType, 'file' => $this->filePath . '/' . $filename, '--format' => $format]); + $this->tester->execute(['command' => 'sylius:export', 'exporter' => $exporterType, 'file' => $this->filePath . '/export/' . $filename, '--format' => $format]); } } diff --git a/tests/Behat/Resources/fixtures/countries_export.json b/tests/Behat/Resources/fixtures/countries_export.json deleted file mode 100644 index 0637a088..00000000 --- a/tests/Behat/Resources/fixtures/countries_export.json +++ /dev/null @@ -1 +0,0 @@ -[] \ No newline at end of file diff --git a/tests/Behat/Resources/fixtures/countries_export.xlsx b/tests/Behat/Resources/fixtures/export/.gitkeep similarity index 100% rename from tests/Behat/Resources/fixtures/countries_export.xlsx rename to tests/Behat/Resources/fixtures/export/.gitkeep