Skip to content

Commit

Permalink
Merge pull request #131 from findologic/PLENTY-325
Browse files Browse the repository at this point in the history
PLENTY-325 export to specific file if full file path is given to the …
  • Loading branch information
howard committed Apr 22, 2021
2 parents 7669467 + f8233ac commit 6ba98ae
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/FINDOLOGIC/Export/CSV/CSVExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public function serializeItemsToFile(
int $total = 0
): string {
$csvString = $this->serializeItems($items, $start, $count, $total);
$targetPath = sprintf('%s/findologic.csv', $targetDirectory);

$targetPath = sprintf('%s/%s.csv', $targetDirectory, $this->fileNamePrefix);

// Clear CSV contents if a new export starts. Don't do this for further pagination steps, to prevent
// overriding the file itself, causing it to clear all contents except the new items.
Expand Down
21 changes: 21 additions & 0 deletions src/FINDOLOGIC/Export/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ abstract class Exporter
*/
public const TYPE_CSV = 1;

protected const DEFAULT_FILE_NAME_PREFIX = 'findologic';

protected $itemsPerPage;

/**
* @var string
*/
protected $fileNamePrefix = self::DEFAULT_FILE_NAME_PREFIX;

protected function __construct($itemsPerPage)
{
$this->itemsPerPage = $itemsPerPage;
Expand Down Expand Up @@ -59,6 +66,20 @@ public static function create(int $type, int $itemsPerPage = 20, array $csvPrope
return $exporter;
}

/**
* Can be used to alter the file name of the serialization output. Default: "findologic".
*
* E.g.
* * XMLExporter: `<path>/<fileNamePrefix>_<start>_<count>.xml`
* * CSVExporter: `<path>/<fileNamePrefix>.csv`
*/
public function setFileNamePrefix(string $fileNamePrefix = self::DEFAULT_FILE_NAME_PREFIX): self
{
$this->fileNamePrefix = $fileNamePrefix;

return $this;
}

/**
* Turns the provided items into their serialized form.
*
Expand Down
2 changes: 1 addition & 1 deletion src/FINDOLOGIC/Export/XML/XMLExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function serializeItemsToFile(
int $total
): string {
$xmlString = $this->serializeItems($items, $start, $count, $total);
$targetPath = sprintf('%s/findologic_%d_%d.xml', $targetDirectory, $start, $count);
$targetPath = sprintf('%s/%s_%d_%d.xml', $targetDirectory, $this->fileNamePrefix, $start, $count);

file_put_contents($targetPath, $xmlString);

Expand Down
18 changes: 18 additions & 0 deletions tests/FINDOLOGIC/Export/Tests/CSVSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,24 @@ public function testCsvWillOverrideItselfWhenPassingAnInitialCount(): void
$this->assertCount(2, file(self::CSV_PATH));
}

public function testCsvFileNamePrefixCanBeAltered(): void
{
$fileNamePrefix = 'findologic.new';
$expectedOutputPath = '/tmp/findologic.new.csv';

$item = $this->getMinimalItem();
$expectedCsvContent = $this->exporter->serializeItems([$item], 0, 1, 1);

$this->exporter->setFileNamePrefix($fileNamePrefix);
$this->exporter->serializeItemsToFile('/tmp', [$item], 0, 1, 1);

$this->assertEquals($expectedCsvContent, file_get_contents($expectedOutputPath));
$this->assertCount(2, file($expectedOutputPath));

// Remove CSV after test.
unlink($expectedOutputPath);
}

public function testKitchenSink(): void
{
$expectedId = '123';
Expand Down
17 changes: 17 additions & 0 deletions tests/FINDOLOGIC/Export/Tests/XmlSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,23 @@ public function testXmlCanBeWrittenDirectlyToFile(): void
self::assertEquals($expectedXml, file_get_contents('/tmp/findologic_0_1.xml'));
}

public function testXmlFileNamePrefixCanBeAltered(): void
{
$fileNamePrefix = 'blub.ber.gurken';
$expectedOutputPath = '/tmp/blub.ber.gurken_0_1.xml';

$item = $this->getMinimalItem();

$this->exporter->setFileNamePrefix($fileNamePrefix);
$expectedXml = $this->exporter->serializeItems([$item], 0, 1, 1);
$this->exporter->serializeItemsToFile('/tmp', [$item], 0, 1, 1);

self::assertEquals($expectedXml, file_get_contents($expectedOutputPath));

// Remove created XML file.
unlink($expectedOutputPath);
}

public function testAttemptingToGetCsvFromAnXmlItemResultsInAnException(): void
{
$item = new XMLItem(123);
Expand Down

0 comments on commit 6ba98ae

Please sign in to comment.