Created unit tests for new Domain-related elements

This commit is contained in:
Alejandro Celaya 2020-09-27 10:11:41 +02:00
parent 76d6d9a7a9
commit 24aab5cc0e
2 changed files with 107 additions and 0 deletions

View File

@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Domain;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Domain\DomainService;
use Shlinkio\Shlink\Core\Domain\Repository\DomainRepositoryInterface;
use Shlinkio\Shlink\Core\Entity\Domain;
class DomainServiceTest extends TestCase
{
private DomainService $domainService;
private ObjectProphecy $em;
public function setUp(): void
{
$this->em = $this->prophesize(EntityManagerInterface::class);
$this->domainService = new DomainService($this->em->reveal());
}
/**
* @test
* @dataProvider provideExcludedDomains
*/
public function listDomainsWithoutDelegatesIntoRepository(?string $excludedDomain, array $expectedResult): void
{
$repo = $this->prophesize(DomainRepositoryInterface::class);
$getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
$findDomains = $repo->findDomainsWithout($excludedDomain)->willReturn($expectedResult);
$result = $this->domainService->listDomainsWithout($excludedDomain);
self::assertEquals($expectedResult, $result);
$getRepo->shouldHaveBeenCalledOnce();
$findDomains->shouldHaveBeenCalledOnce();
}
public function provideExcludedDomains(): iterable
{
yield 'no excluded domain' => [null, []];
yield 'foo.com excluded domain' => ['foo.com', []];
yield 'bar.com excluded domain' => ['bar.com', [new Domain('bar.com')]];
yield 'baz.com excluded domain' => ['baz.com', [new Domain('foo.com'), new Domain('bar.com')]];
}
}

View File

@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\Domain;
use Laminas\Diactoros\Response\JsonResponse;
use Laminas\Diactoros\ServerRequestFactory;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
use Shlinkio\Shlink\Core\Entity\Domain;
use Shlinkio\Shlink\Rest\Action\Domain\ListDomainsAction;
class ListDomainsActionTest extends TestCase
{
private ListDomainsAction $action;
private ObjectProphecy $domainService;
public function setUp(): void
{
$this->domainService = $this->prophesize(DomainServiceInterface::class);
$this->action = new ListDomainsAction($this->domainService->reveal(), 'foo.com');
}
/** @test */
public function domainsAreProperlyListed(): void
{
$listDomains = $this->domainService->listDomainsWithout('foo.com')->willReturn([
new Domain('bar.com'),
new Domain('baz.com'),
]);
/** @var JsonResponse $resp */
$resp = $this->action->handle(ServerRequestFactory::fromGlobals());
$payload = $resp->getPayload();
self::assertEquals([
'domains' => [
'data' => [
[
'domain' => 'foo.com',
'isDefault' => true,
],
[
'domain' => 'bar.com',
'isDefault' => false,
],
[
'domain' => 'baz.com',
'isDefault' => false,
],
],
],
], $payload);
$listDomains->shouldHaveBeenCalledOnce();
}
}