Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added detect linebreak and improve duplicate detection #15

Merged
merged 1 commit into from
Oct 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Import/Reader/CsvReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class CsvReader implements ReaderInterface
*/
public function read($fileName)
{
ini_set('auto_detect_line_endings', true); // For mac's office excel csv

$csv = new SplFileObject($fileName);
$csv->setCsvControl();
$csv->setFlags(SplFileObject::READ_CSV);
Expand Down
14 changes: 4 additions & 10 deletions Import/Reader/ReaderItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ReaderItem
/**
* @var string
*/
private $content;
private $lineContent;

/**
* @var array
Expand All @@ -36,18 +36,12 @@ class ReaderItem
*/
private $exception;

/**
* @param int $lineNumber
* @param string $lineContent
* @param array $item
* @param \Exception $exception
*/
public function __construct($lineNumber, $lineContent, array $item = null, \Exception $exception = null)
public function __construct($lineNumber, $lineContent, array $data = null, \Exception $exception = null)
{
$this->lineNumber = $lineNumber;
$this->data = $item;
$this->exception = $exception;
$this->lineContent = $lineContent;
$this->data = $data;
$this->exception = $exception;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Import/Writer/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct(RedirectRouteManagerInterface $manager, EntityManage
public function write(RedirectRouteInterface $entity)
{
$this->validate($entity);
$this->sources[] = $entity->getSource();
$this->sources[] = strtolower($entity->getSource());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem here is that the unique key of mysql is case-insensitive by default. we could add this here or mark the unique key as BINARY


try {
$this->save($entity);
Expand Down Expand Up @@ -116,7 +116,7 @@ private function validate(RedirectRouteInterface $entity)
throw new TargetIsEmptyException($entity);
}

if (in_array($entity->getSource(), $this->sources)) {
if (in_array(strtolower($entity->getSource()), $this->sources)) {
throw new DuplicatedSourceException($entity);
}
}
Expand Down
22 changes: 22 additions & 0 deletions Tests/Unit/Import/Writer/WriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,28 @@ public function testWriteDuplicated()
$this->redirectRouteManager->save($entities[1]->reveal())->shouldNotBeCalled();
}

public function testWriteDuplicatedCaseInSensitive()
{
$this->setExpectedException(DuplicatedSourceException::class);

$entities = [
$this->prophesize(RedirectRouteInterface::class),
$this->prophesize(RedirectRouteInterface::class),
];

$entities[0]->getSource()->willReturn('/source');
$entities[0]->getTarget()->willReturn('/target');

$entities[1]->getSource()->willReturn('/Source');
$entities[1]->getTarget()->willReturn('/target');

$this->writer->write($entities[0]->reveal());
$this->writer->write($entities[1]->reveal());

$this->redirectRouteManager->save($entities[0]->reveal())->shouldBeCalled();
$this->redirectRouteManager->save($entities[1]->reveal())->shouldNotBeCalled();
}

public function testWriteAlreadyExisting()
{
$this->setExpectedException(DuplicatedSourceException::class);
Expand Down