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/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/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/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/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 7891dbab..8bbc6434 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; } + + /** + * {@inheritdoc} + */ + public function finish(): void + { + $this->writer->finish(); + } } diff --git a/src/Writer/ExcelWriter.php b/src/Writer/ExcelWriter.php index 6335ff4f..c9c875ca 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(); } } + + /** + * {@inheritdoc} + */ + public function finish(): void + { + $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; } 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