Added errorhandling for individual imported URLs, so that one failing doe snot make the whole process fail

This commit is contained in:
Alejandro Celaya
2022-04-18 14:45:37 +02:00
parent 622f0217fa
commit 9f6ffc7186
4 changed files with 51 additions and 12 deletions

View File

@@ -11,6 +11,7 @@ use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use RuntimeException;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Importer\ImportedLinksProcessor;
@@ -81,6 +82,37 @@ class ImportedLinksProcessorTest extends TestCase
$this->io->text(Argument::type('string'))->shouldHaveBeenCalledTimes($expectedCalls);
}
/** @test */
public function newUrlsWithErrorsAreSkipped(): void
{
$urls = [
new ImportedShlinkUrl('', 'foo', [], Chronos::now(), null, 'foo', null),
new ImportedShlinkUrl('', 'bar', [], Chronos::now(), null, 'bar', 'foo'),
new ImportedShlinkUrl('', 'baz', [], Chronos::now(), null, 'baz', null),
];
$importedUrlExists = $this->repo->findOneByImportedUrl(Argument::cetera())->willReturn(null);
$ensureUniqueness = $this->shortCodeHelper->ensureShortCodeUniqueness(Argument::cetera())->willReturn(true);
$persist = $this->em->persist(Argument::type(ShortUrl::class))->will(function (array $args): void {
/** @var ShortUrl $shortUrl */
[$shortUrl] = $args;
if ($shortUrl->getShortCode() === 'baz') {
throw new RuntimeException('Whatever error');
}
});
$this->processor->process($this->io->reveal(), $urls, $this->buildParams());
$importedUrlExists->shouldHaveBeenCalledTimes(3);
$ensureUniqueness->shouldHaveBeenCalledTimes(3);
$persist->shouldHaveBeenCalledTimes(3);
$this->io->text(Argument::containingString('<info>Imported</info>'))->shouldHaveBeenCalledTimes(2);
$this->io->text(
Argument::containingString('<comment>Skipped</comment>. Reason: Whatever error'),
)->shouldHaveBeenCalledOnce();
}
/** @test */
public function alreadyImportedUrlsAreSkipped(): void
{