diff --git a/src/Bundles/Exchange/CSV/CSVExchange.php b/src/Bundles/Exchange/CSV/CSVExchange.php index a7f89cdc..9d657012 100644 --- a/src/Bundles/Exchange/CSV/CSVExchange.php +++ b/src/Bundles/Exchange/CSV/CSVExchange.php @@ -43,6 +43,15 @@ public function getName(): string return 'csv'; } + /** + * @return string + */ + public function getCsvDelimiter(): string + { + return $this->csvDelimiter; + } + + /** * @return CommandOption[] */ @@ -59,9 +68,11 @@ public function getOptions(): array */ public function setOptionValues(array $options): void { - $this->csvDelimiter = (string)$options['csv-delimiter']; + $this->csvDelimiter = isset($options['csv-delimiter']) ? (string)$options['csv-delimiter'] : ''; + + $this->csvDelimiter = trim($this->csvDelimiter); - if ($this->csvDelimiter === '' || $this->csvDelimiter === '0') { + if ($this->csvDelimiter === '') { $this->csvDelimiter = ','; } } diff --git a/tests/phpunit/Bundles/Exchange/CSV/CSVExchangeTest.php b/tests/phpunit/Bundles/Exchange/CSV/CSVExchangeTest.php index 2939d8b0..26aaa16b 100644 --- a/tests/phpunit/Bundles/Exchange/CSV/CSVExchangeTest.php +++ b/tests/phpunit/Bundles/Exchange/CSV/CSVExchangeTest.php @@ -2,9 +2,12 @@ namespace phpunit\Bundles\Exchange\CSV; +use Exception; use PHPUnit\Framework\TestCase; use phpunit\Utils\Fakes\FakeCSVWriter; use PHPUnuhi\Bundles\Exchange\CSV\CSVExchange; +use PHPUnuhi\Exceptions\TranslationNotFoundException; +use PHPUnuhi\Models\Command\CommandOption; use PHPUnuhi\Models\Configuration\Filter; use PHPUnuhi\Models\Configuration\Protection; use PHPUnuhi\Models\Translation\Locale; @@ -13,17 +16,88 @@ class CSVExchangeTest extends TestCase { + /** + * @var FakeCSVWriter + */ + private $fakeWriter; + + /** + * @var CSVExchange + */ + private $csv; + + + /** + * @return void + */ + public function setUp(): void + { + $this->fakeWriter = new FakeCSVWriter(); + + $this->csv = new CSVExchange($this->fakeWriter); + } + + + /** + * @return void + */ + public function testGetName(): void + { + $this->assertEquals('csv', $this->csv->getName()); + } + + /** + * @return void + */ + public function testPossibleOptions(): void + { + $expected = [ + new CommandOption('csv-delimiter', true), + ]; + + $this->assertEquals($expected, $this->csv->getOptions()); + } + + /** + * @throws Exception + * @return void + */ + public function testSetOptionsWithMissingDelimiterUsesDefaultDelimiter(): void + { + $options = [ + 'csv-delimiter' => ' ' + ]; + + $this->csv->setOptionValues($options); + + $this->assertEquals(',', $this->csv->getCsvDelimiter()); + } + + /** + * @throws Exception + * @return void + */ + public function testSetOptions(): void + { + $options = [ + 'csv-delimiter' => 'A' + ]; + + $this->csv->setOptionValues($options); + + $this->assertEquals('A', $this->csv->getCsvDelimiter()); + } + + /** * This test verifies that we can correctly export a CSV. * The export does not consider groups + * + * @throws TranslationNotFoundException * @return void */ public function testExportWithoutGroups(): void { - $fakeWriter = new FakeCSVWriter(); - - $csv = new CSVExchange($fakeWriter); - $localesDE = new Locale('de-DE', '', ''); $localesDE->addTranslation('btnCancel', 'Abbrechen', ''); $localesDE->addTranslation('btnOK', 'OK', ''); @@ -43,7 +117,7 @@ public function testExportWithoutGroups(): void [] ); - $csv->export($set, '', false); + $this->csv->export($set, '', false); $expected = [ [ @@ -63,20 +137,18 @@ public function testExportWithoutGroups(): void ] ]; - $this->assertEquals($expected, $fakeWriter->getWrittenLines()); + $this->assertEquals($expected, $this->fakeWriter->getWrittenLines()); } /** * This test verifies that we can correctly export a CSV. * The export considers groups + * + * @throws TranslationNotFoundException * @return void */ public function testExportWithGroups(): void { - $fakeWriter = new FakeCSVWriter(); - - $csv = new CSVExchange($fakeWriter); - $localesDE = new Locale('de-DE', '', ''); $localesDE->addTranslation('title', 'T-Shirt', 'ProductA'); $localesDE->addTranslation('size', 'Mittel', 'ProductA'); @@ -97,7 +169,7 @@ public function testExportWithGroups(): void [] ); - $csv->export($set, '', false); + $this->csv->export($set, '', false); $expected = [ [ @@ -126,6 +198,66 @@ public function testExportWithGroups(): void ] ]; - $this->assertEquals($expected, $fakeWriter->getWrittenLines()); + $this->assertEquals($expected, $this->fakeWriter->getWrittenLines()); + } + + + /** + * This test verifies that we can correctly export a CSV. + * In this case we only export empty translations + * + * @throws TranslationNotFoundException + * @return void + */ + public function testExportOnlyEmpty(): void + { + $de = new Locale('de-DE', '', ''); + $en = new Locale('en-GB', '', ''); + + # must not be exported + $de->addTranslation('title', 'Titel', ''); + $en->addTranslation('title', 'Title', ''); + + # should be exported because 1 translation is missing + $de->addTranslation('size', '', ''); + $en->addTranslation('size', 'Medium', ''); + + # should be exported because 1 translation is missing (vice-versa) + $de->addTranslation('subtitle', 'Untertitel', ''); + $en->addTranslation('subtitle', '', ''); + + + $set = new TranslationSet( + '', + 'json', + new Protection(), + [$de, $en], + new Filter(), + [], + [], + [] + ); + + $this->csv->export($set, '', true); + + $expected = [ + [ + 'Key', + 'de-DE', + 'en-GB', + ], + [ + 'size', + '', + 'Medium', + ], + [ + 'subtitle', + 'Untertitel', + '', + ], + ]; + + $this->assertEquals($expected, $this->fakeWriter->getWrittenLines()); } }