Skip to content

Commit

Permalink
Created CsvImporterTest
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Feb 6, 2021
1 parent 6f55e6a commit e17e11b
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"test": "phpdbg -qrr vendor/bin/phpunit --order-by=random --testdox --colors=always",
"test:ci": "@test --coverage-clover=build/clover.xml --coverage-xml=build/coverage-xml --log-junit=build/junit.xml",
"test:pretty": "@test --coverage-html build/coverage-html",
"infect": "infection --threads=4 --min-msi=70 --log-verbosity=default --only-covered",
"infect": "infection --threads=4 --min-msi=75 --log-verbosity=default --only-covered",
"infect:ci": "@infect --coverage=build --skip-initial-tests",
"infect:show": "@infect --show-mutations",
"infect:show:ci": "@infect --show-mutations --coverage=build",
Expand Down
10 changes: 9 additions & 1 deletion src/Sources/Csv/CsvImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Shlinkio\Shlink\Importer\Sources\Csv;

use DateTimeImmutable;
use DateTimeInterface;
use League\Csv\Reader;
use Shlinkio\Shlink\Importer\Exception\ImportException;
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl;
Expand All @@ -22,14 +23,21 @@ class CsvImporter implements ImporterStrategyInterface
{
private const TAG_SEPARATOR = '|';

private ?DateTimeInterface $date;

public function __construct(?DateTimeInterface $date = null)
{
$this->date = $date;
}

/**
* @return ImportedShlinkUrl[]
* @throws ImportException
*/
public function import(array $rawParams): iterable
{
$params = CsvParams::fromRawParams($rawParams);
$now = new DateTimeImmutable();
$now = $this->date ?? new DateTimeImmutable();

$csvReader = Reader::createFromStream($params->stream())->setDelimiter($params->delimiter())
->setHeaderOffset(0);
Expand Down
132 changes: 132 additions & 0 deletions test/Sources/Csv/CsvImporterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

declare(strict_types=1);

namespace ShlinkioTest\Shlink\Importer\Sources\Csv;

use DateTimeImmutable;
use DateTimeInterface;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl;
use Shlinkio\Shlink\Importer\Sources\Csv\CsvImporter;
use Shlinkio\Shlink\Importer\Sources\ImportSources;

use function fopen;
use function fwrite;
use function rewind;

class CsvImporterTest extends TestCase
{
private CsvImporter $importer;

protected function setUp(): void
{
$this->importer = new CsvImporter($this->getDate());
}

/**
* @test
* @dataProvider provideCSVs
*/
public function csvIsProperlyImported(string $csv, string $delimiter, array $expectedList): void
{
$rawOptions = ['delimiter' => $delimiter, 'stream' => $this->createCsvStream($csv)];

$result = $this->importer->import($rawOptions);

$urls = [];
foreach ($result as $item) {
$urls[] = $item;
}

self::assertEquals($expectedList, $urls);
}

public function provideCSVs(): iterable
{
yield 'comma separator' => [
<<<CSV
Long URL,Tags,Domain ,Short code, Title
https://shlink.io,foo|bar|baz,,123,
https://facebook.com,,example.com,456,my title
CSV,
',',
[
new ImportedShlinkUrl(
ImportSources::CSV,
'https://shlink.io',
['foo', 'bar', 'baz'],
$this->getDate(),
null,
'123',
null,
),
new ImportedShlinkUrl(
ImportSources::CSV,
'https://facebook.com',
[],
$this->getDate(),
'example.com',
'456',
'my title',
),
],
];
yield 'semicolon separator' => [
<<<CSV
longURL;tags;domain;short code;Title
https://alejandrocelaya.blog;;;abc;
https://facebook.com;foo|baz;example.com;def;
https://shlink.io/documentation;;example.com;ghi;the title
CSV,
';',
[
new ImportedShlinkUrl(
ImportSources::CSV,
'https://alejandrocelaya.blog',
[],
$this->getDate(),
null,
'abc',
null,
),
new ImportedShlinkUrl(
ImportSources::CSV,
'https://facebook.com',
['foo', 'baz'],
$this->getDate(),
'example.com',
'def',
null,
),
new ImportedShlinkUrl(
ImportSources::CSV,
'https://shlink.io/documentation',
[],
$this->getDate(),
'example.com',
'ghi',
'the title',
),
],
];
}

/**
* @return resource
*/
private function createCsvStream(string $csv)
{
$stream = fopen('php://memory', 'rb+');
fwrite($stream, $csv);
rewind($stream);

return $stream;
}

private function getDate(): DateTimeInterface
{
static $date;
return $date ?? ($date = new DateTimeImmutable());
}
}

0 comments on commit e17e11b

Please sign in to comment.