1
0
Fork 0
mirror of https://github.com/shlinkio/shlink.git synced 2025-03-11 10:39:57 +03:00

Migrated UrlShortenerTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-23 11:32:13 +02:00
parent 36ab455a49
commit d2f5be1d18

View file

@ -5,11 +5,9 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\ShortUrl; namespace ShlinkioTest\Shlink\Core\ShortUrl;
use Cake\Chronos\Chronos; use Cake\Chronos\Chronos;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\EventDispatcher\EventDispatcherInterface; use Psr\EventDispatcher\EventDispatcherInterface;
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException; use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
@ -22,42 +20,29 @@ use Shlinkio\Shlink\Core\ShortUrl\UrlShortener;
class UrlShortenerTest extends TestCase class UrlShortenerTest extends TestCase
{ {
use ProphecyTrait;
private UrlShortener $urlShortener; private UrlShortener $urlShortener;
private ObjectProphecy $em; private MockObject $em;
private ObjectProphecy $titleResolutionHelper; private MockObject $titleResolutionHelper;
private ObjectProphecy $shortCodeHelper; private MockObject $shortCodeHelper;
protected function setUp(): void protected function setUp(): void
{ {
$this->titleResolutionHelper = $this->prophesize(ShortUrlTitleResolutionHelperInterface::class); $this->titleResolutionHelper = $this->createMock(ShortUrlTitleResolutionHelperInterface::class);
$this->titleResolutionHelper->processTitleAndValidateUrl(Argument::cetera())->willReturnArgument(); $this->shortCodeHelper = $this->createMock(ShortCodeUniquenessHelperInterface::class);
$this->em = $this->prophesize(EntityManagerInterface::class); // FIXME Should use the interface, but it doe snot define wrapInTransaction explicitly
$this->em->persist(Argument::any())->will(function ($arguments): void { $this->em = $this->createMock(EntityManager::class);
/** @var ShortUrl $shortUrl */ $this->em->method('persist')->willReturnCallback(fn (ShortUrl $shortUrl) => $shortUrl->setId('10'));
[$shortUrl] = $arguments; $this->em->method('wrapInTransaction')->with($this->isType('callable'))->willReturnCallback(
$shortUrl->setId('10'); fn (callable $callback) => $callback(),
}); );
$this->em->wrapInTransaction(Argument::type('callable'))->will(function (array $args) {
/** @var callable $callback */
[$callback] = $args;
return $callback();
});
$repo = $this->prophesize(ShortUrlRepository::class);
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$this->shortCodeHelper = $this->prophesize(ShortCodeUniquenessHelperInterface::class);
$this->shortCodeHelper->ensureShortCodeUniqueness(Argument::cetera())->willReturn(true);
$this->urlShortener = new UrlShortener( $this->urlShortener = new UrlShortener(
$this->titleResolutionHelper->reveal(), $this->titleResolutionHelper,
$this->em->reveal(), $this->em,
new SimpleShortUrlRelationResolver(), new SimpleShortUrlRelationResolver(),
$this->shortCodeHelper->reveal(), $this->shortCodeHelper,
$this->prophesize(EventDispatcherInterface::class)->reveal(), $this->createMock(EventDispatcherInterface::class),
); );
} }
@ -66,23 +51,31 @@ class UrlShortenerTest extends TestCase
{ {
$longUrl = 'http://foobar.com/12345/hello?foo=bar'; $longUrl = 'http://foobar.com/12345/hello?foo=bar';
$meta = ShortUrlCreation::fromRawData(['longUrl' => $longUrl]); $meta = ShortUrlCreation::fromRawData(['longUrl' => $longUrl]);
$this->titleResolutionHelper->expects($this->once())->method('processTitleAndValidateUrl')->with(
$this->equalTo($meta),
)->willReturnArgument(0);
$this->shortCodeHelper->method('ensureShortCodeUniqueness')->willReturn(true);
$shortUrl = $this->urlShortener->shorten($meta); $shortUrl = $this->urlShortener->shorten($meta);
self::assertEquals($longUrl, $shortUrl->getLongUrl()); self::assertEquals($longUrl, $shortUrl->getLongUrl());
$this->titleResolutionHelper->processTitleAndValidateUrl($meta)->shouldHaveBeenCalledOnce();
} }
/** @test */ /** @test */
public function exceptionIsThrownWhenNonUniqueSlugIsProvided(): void public function exceptionIsThrownWhenNonUniqueSlugIsProvided(): void
{ {
$ensureUniqueness = $this->shortCodeHelper->ensureShortCodeUniqueness(Argument::cetera())->willReturn(false); $meta = ShortUrlCreation::fromRawData(
['customSlug' => 'custom-slug', 'longUrl' => 'http://foobar.com/12345/hello?foo=bar'],
);
$this->shortCodeHelper->expects($this->once())->method('ensureShortCodeUniqueness')->willReturn(false);
$this->titleResolutionHelper->expects($this->once())->method('processTitleAndValidateUrl')->with(
$this->equalTo($meta),
)->willReturnArgument(0);
$ensureUniqueness->shouldBeCalledOnce();
$this->expectException(NonUniqueSlugException::class); $this->expectException(NonUniqueSlugException::class);
$this->urlShortener->shorten(ShortUrlCreation::fromRawData( $this->urlShortener->shorten($meta);
['customSlug' => 'custom-slug', 'longUrl' => 'http://foobar.com/12345/hello?foo=bar'],
));
} }
/** /**
@ -91,16 +84,16 @@ class UrlShortenerTest extends TestCase
*/ */
public function existingShortUrlIsReturnedWhenRequested(ShortUrlCreation $meta, ShortUrl $expected): void public function existingShortUrlIsReturnedWhenRequested(ShortUrlCreation $meta, ShortUrl $expected): void
{ {
$repo = $this->prophesize(ShortUrlRepository::class); $repo = $this->createMock(ShortUrlRepository::class);
$findExisting = $repo->findOneMatching(Argument::cetera())->willReturn($expected); $repo->expects($this->once())->method('findOneMatching')->willReturn($expected);
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); $this->em->expects($this->once())->method('getRepository')->with($this->equalTo(ShortUrl::class))->willReturn(
$repo,
);
$this->titleResolutionHelper->expects($this->never())->method('processTitleAndValidateUrl');
$this->shortCodeHelper->method('ensureShortCodeUniqueness')->willReturn(true);
$result = $this->urlShortener->shorten($meta); $result = $this->urlShortener->shorten($meta);
$findExisting->shouldHaveBeenCalledOnce();
$getRepo->shouldHaveBeenCalledOnce();
$this->em->persist(Argument::cetera())->shouldNotHaveBeenCalled();
$this->titleResolutionHelper->processTitleAndValidateUrl(Argument::cetera())->shouldNotHaveBeenCalled();
self::assertSame($expected, $result); self::assertSame($expected, $result);
} }