Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
boxblinkracer committed Jan 3, 2024
1 parent 9877075 commit f6f8b6d
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 14 deletions.
15 changes: 13 additions & 2 deletions src/Bundles/Exchange/CSV/CSVExchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public function getName(): string
return 'csv';
}

/**
* @return string
*/
public function getCsvDelimiter(): string
{
return $this->csvDelimiter;
}


/**
* @return CommandOption[]
*/
Expand All @@ -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 = ',';
}
}
Expand Down
156 changes: 144 additions & 12 deletions tests/phpunit/Bundles/Exchange/CSV/CSVExchangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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', '');
Expand All @@ -43,7 +117,7 @@ public function testExportWithoutGroups(): void
[]
);

$csv->export($set, '', false);
$this->csv->export($set, '', false);

$expected = [
[
Expand All @@ -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');
Expand All @@ -97,7 +169,7 @@ public function testExportWithGroups(): void
[]
);

$csv->export($set, '', false);
$this->csv->export($set, '', false);

$expected = [
[
Expand Down Expand Up @@ -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());
}
}

0 comments on commit f6f8b6d

Please sign in to comment.