Skip to content

Commit

Permalink
Merge pull request #110 from enesaktay/fix-excel-cli-export
Browse files Browse the repository at this point in the history
Fix CLI Excel export creating files with zero bytes
  • Loading branch information
lsmith77 authored Jun 25, 2018
2 parents 8ca8f7c + 9515d2c commit 5e2ae17
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions spec/Writer/CsvWriterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
2 changes: 2 additions & 0 deletions spec/Writer/ExcelWriterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
1 change: 1 addition & 0 deletions src/Command/ExportDataCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$service->setExportFile($file);

$service->export($idsToExport);
$service->finish();

$message = sprintf(
"<info>Exported %d item(s) to '%s' via the %s exporter</info>",
Expand Down
8 changes: 8 additions & 0 deletions src/Exporter/JsonResourceExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@ public function setExportFile(string $filename): void
{
$this->filename = $filename;
}

/**
* {@inheritdoc}
*/
public function finish(): void
{
// no finish needed
}
}
8 changes: 8 additions & 0 deletions src/Exporter/ResourceExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
5 changes: 5 additions & 0 deletions src/Exporter/ResourceExporterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
10 changes: 9 additions & 1 deletion src/Writer/CsvWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
10 changes: 9 additions & 1 deletion src/Writer/ExcelWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -86,4 +86,12 @@ private function prepare(): void
$this->writer->prepare();
}
}

/**
* {@inheritdoc}
*/
public function finish(): void
{
$this->writer->finish();
}
}
5 changes: 5 additions & 0 deletions src/Writer/WriterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion tests/Behat/Context/CliBaseContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
1 change: 0 additions & 1 deletion tests/Behat/Resources/fixtures/countries_export.json

This file was deleted.

0 comments on commit 5e2ae17

Please sign in to comment.