mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-12 01:54:41 +08:00
Simplified transactional URL shortening
This commit is contained in:
@@ -8,7 +8,6 @@ use Cake\Chronos\Chronos;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\ORMException;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
@@ -42,16 +41,18 @@ class UrlShortenerTest extends TestCase
|
||||
|
||||
$this->em = $this->prophesize(EntityManagerInterface::class);
|
||||
$conn = $this->prophesize(Connection::class);
|
||||
$conn->isTransactionActive()->willReturn(false);
|
||||
$this->em->getConnection()->willReturn($conn->reveal());
|
||||
$this->em->flush()->willReturn(null);
|
||||
$this->em->commit()->willReturn(null);
|
||||
$this->em->beginTransaction()->willReturn(null);
|
||||
$this->em->persist(Argument::any())->will(function ($arguments): void {
|
||||
/** @var ShortUrl $shortUrl */
|
||||
[$shortUrl] = $arguments;
|
||||
$shortUrl->setId('10');
|
||||
});
|
||||
$this->em->transactional(Argument::type('callable'))->will(function (array $args) {
|
||||
/** @var callable $callback */
|
||||
[$callback] = $args;
|
||||
|
||||
return $callback();
|
||||
});
|
||||
$repo = $this->prophesize(ShortUrlRepository::class);
|
||||
$repo->shortCodeIsInUse(Argument::cetera())->willReturn(false);
|
||||
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
||||
@@ -70,7 +71,7 @@ class UrlShortenerTest extends TestCase
|
||||
/** @test */
|
||||
public function urlIsProperlyShortened(): void
|
||||
{
|
||||
$shortUrl = $this->urlShortener->urlToShortCode(
|
||||
$shortUrl = $this->urlShortener->shorten(
|
||||
'http://foobar.com/12345/hello?foo=bar',
|
||||
[],
|
||||
ShortUrlMeta::createEmpty(),
|
||||
@@ -87,32 +88,13 @@ class UrlShortenerTest extends TestCase
|
||||
$ensureUniqueness->shouldBeCalledOnce();
|
||||
$this->expectException(NonUniqueSlugException::class);
|
||||
|
||||
$this->urlShortener->urlToShortCode(
|
||||
$this->urlShortener->shorten(
|
||||
'http://foobar.com/12345/hello?foo=bar',
|
||||
[],
|
||||
ShortUrlMeta::fromRawData(['customSlug' => 'custom-slug']),
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function transactionIsRolledBackAndExceptionRethrownWhenExceptionIsThrown(): void
|
||||
{
|
||||
$conn = $this->prophesize(Connection::class);
|
||||
$conn->isTransactionActive()->willReturn(true);
|
||||
$this->em->getConnection()->willReturn($conn->reveal());
|
||||
$this->em->rollback()->shouldBeCalledOnce();
|
||||
$this->em->close()->shouldBeCalledOnce();
|
||||
|
||||
$this->em->flush()->willThrow(new ORMException());
|
||||
|
||||
$this->expectException(ORMException::class);
|
||||
$this->urlShortener->urlToShortCode(
|
||||
'http://foobar.com/12345/hello?foo=bar',
|
||||
[],
|
||||
ShortUrlMeta::createEmpty(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideExistingShortUrls
|
||||
@@ -127,7 +109,7 @@ class UrlShortenerTest extends TestCase
|
||||
$findExisting = $repo->findOneMatching(Argument::cetera())->willReturn($expected);
|
||||
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
||||
|
||||
$result = $this->urlShortener->urlToShortCode($url, $tags, $meta);
|
||||
$result = $this->urlShortener->shorten($url, $tags, $meta);
|
||||
|
||||
$findExisting->shouldHaveBeenCalledOnce();
|
||||
$getRepo->shouldHaveBeenCalledOnce();
|
||||
|
||||
Reference in New Issue
Block a user