Deleted old domain resolvers and added tests for new short url relation resolvers

This commit is contained in:
Alejandro Celaya 2020-11-07 09:49:09 +01:00
parent 2732b05834
commit 7c9f572eb1
7 changed files with 156 additions and 162 deletions

View File

@ -1,12 +0,0 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Domain\Resolver;
use Shlinkio\Shlink\Core\Entity\Domain;
interface DomainResolverInterface
{
public function resolveDomain(?string $domain): ?Domain;
}

View File

@ -1,29 +0,0 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Domain\Resolver;
use Doctrine\ORM\EntityManagerInterface;
use Shlinkio\Shlink\Core\Entity\Domain;
class PersistenceDomainResolver implements DomainResolverInterface
{
private EntityManagerInterface $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function resolveDomain(?string $domain): ?Domain
{
if ($domain === null) {
return null;
}
/** @var Domain|null $existingDomain */
$existingDomain = $this->em->getRepository(Domain::class)->findOneBy(['authority' => $domain]);
return $existingDomain ?? new Domain($domain);
}
}

View File

@ -1,15 +0,0 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Domain\Resolver;
use Shlinkio\Shlink\Core\Entity\Domain;
class SimpleDomainResolver implements DomainResolverInterface
{
public function resolveDomain(?string $domain): ?Domain
{
return $domain !== null ? new Domain($domain) : null;
}
}

View File

@ -1,65 +0,0 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Domain\Resolver;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectRepository;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Domain\Resolver\PersistenceDomainResolver;
use Shlinkio\Shlink\Core\Entity\Domain;
class PersistenceDomainResolverTest extends TestCase
{
use ProphecyTrait;
private PersistenceDomainResolver $domainResolver;
private ObjectProphecy $em;
public function setUp(): void
{
$this->em = $this->prophesize(EntityManagerInterface::class);
$this->domainResolver = new PersistenceDomainResolver($this->em->reveal());
}
/** @test */
public function returnsEmptyWhenNoDomainIsProvided(): void
{
$getRepository = $this->em->getRepository(Domain::class);
self::assertNull($this->domainResolver->resolveDomain(null));
$getRepository->shouldNotHaveBeenCalled();
}
/**
* @test
* @dataProvider provideFoundDomains
*/
public function findsOrCreatesDomainWhenValueIsProvided(?Domain $foundDomain, string $authority): void
{
$repo = $this->prophesize(ObjectRepository::class);
$findDomain = $repo->findOneBy(['authority' => $authority])->willReturn($foundDomain);
$getRepository = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
$result = $this->domainResolver->resolveDomain($authority);
if ($foundDomain !== null) {
self::assertSame($result, $foundDomain);
}
self::assertInstanceOf(Domain::class, $result);
self::assertEquals($authority, $result->getAuthority());
$findDomain->shouldHaveBeenCalledOnce();
$getRepository->shouldHaveBeenCalledOnce();
}
public function provideFoundDomains(): iterable
{
$authority = 'doma.in';
yield 'without found domain' => [null, $authority];
yield 'with found domain' => [new Domain($authority), $authority];
}
}

View File

@ -1,41 +0,0 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Domain\Resolver;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Domain\Resolver\SimpleDomainResolver;
use Shlinkio\Shlink\Core\Entity\Domain;
class SimpleDomainResolverTest extends TestCase
{
private SimpleDomainResolver $domainResolver;
public function setUp(): void
{
$this->domainResolver = new SimpleDomainResolver();
}
/**
* @test
* @dataProvider provideDomains
*/
public function resolvesExpectedDomain(?string $domain): void
{
$result = $this->domainResolver->resolveDomain($domain);
if ($domain === null) {
self::assertNull($result);
} else {
self::assertInstanceOf(Domain::class, $result);
self::assertEquals($domain, $result->getAuthority());
}
}
public function provideDomains(): iterable
{
yield 'with empty domain' => [null];
yield 'with non-empty domain' => ['domain.com'];
}
}

View File

@ -0,0 +1,100 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\ShortUrl\Resolver;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ObjectRepository;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Entity\Domain;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\PersistenceShortUrlRelationResolver;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
class PersistenceShortUrlRelationResolverTest extends TestCase
{
use ProphecyTrait;
private PersistenceShortUrlRelationResolver $resolver;
private ObjectProphecy $em;
public function setUp(): void
{
$this->em = $this->prophesize(EntityManagerInterface::class);
$this->resolver = new PersistenceShortUrlRelationResolver($this->em->reveal());
}
/** @test */
public function returnsEmptyWhenNoDomainIsProvided(): void
{
$getRepository = $this->em->getRepository(Domain::class);
self::assertNull($this->resolver->resolveDomain(null));
$getRepository->shouldNotHaveBeenCalled();
}
/**
* @test
* @dataProvider provideFoundDomains
*/
public function findsOrCreatesDomainWhenValueIsProvided(?Domain $foundDomain, string $authority): void
{
$repo = $this->prophesize(ObjectRepository::class);
$findDomain = $repo->findOneBy(['authority' => $authority])->willReturn($foundDomain);
$getRepository = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
$result = $this->resolver->resolveDomain($authority);
if ($foundDomain !== null) {
self::assertSame($result, $foundDomain);
}
self::assertInstanceOf(Domain::class, $result);
self::assertEquals($authority, $result->getAuthority());
$findDomain->shouldHaveBeenCalledOnce();
$getRepository->shouldHaveBeenCalledOnce();
}
public function provideFoundDomains(): iterable
{
$authority = 'doma.in';
yield 'not found domain' => [null, $authority];
yield 'found domain' => [new Domain($authority), $authority];
}
/** @test */
public function returnsEmptyWhenNoApiKeyIsProvided(): void
{
$getRepository = $this->em->getRepository(ApiKey::class);
self::assertNull($this->resolver->resolveApiKey(null));
$getRepository->shouldNotHaveBeenCalled();
}
/**
* @test
* @dataProvider provideFoundApiKeys
*/
public function triesToFindApiKeyWhenValueIsProvided(?ApiKey $foundApiKey, string $key): void
{
$repo = $this->prophesize(ObjectRepository::class);
$find = $repo->findOneBy(['key' => $key])->willReturn($foundApiKey);
$getRepository = $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
$result = $this->resolver->resolveApiKey($key);
self::assertSame($result, $foundApiKey);
$find->shouldHaveBeenCalledOnce();
$getRepository->shouldHaveBeenCalledOnce();
}
public function provideFoundApiKeys(): iterable
{
$key = 'abc123';
yield 'not found api key' => [null, $key];
yield 'found api key' => [new ApiKey(), $key];
}
}

View File

@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\ShortUrl\Resolver;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Entity\Domain;
use Shlinkio\Shlink\Core\ShortUrl\Resolver\SimpleShortUrlRelationResolver;
class SimpleShortUrlRelationResolverTest extends TestCase
{
private SimpleShortUrlRelationResolver $resolver;
public function setUp(): void
{
$this->resolver = new SimpleShortUrlRelationResolver();
}
/**
* @test
* @dataProvider provideDomains
*/
public function resolvesExpectedDomain(?string $domain): void
{
$result = $this->resolver->resolveDomain($domain);
if ($domain === null) {
self::assertNull($result);
} else {
self::assertInstanceOf(Domain::class, $result);
self::assertEquals($domain, $result->getAuthority());
}
}
public function provideDomains(): iterable
{
yield 'empty domain' => [null];
yield 'non-empty domain' => ['domain.com'];
}
/**
* @test
* @dataProvider provideKeys
*/
public function alwaysReturnsNullForApiKeys(?string $key): void
{
self::assertNull($this->resolver->resolveApiKey($key));
}
public function provideKeys(): iterable
{
yield 'empty api key' => [null];
yield 'non-empty api key' => ['abc123'];
}
}