Merge pull request #1581 from acelaya-forks/feature/phpunit-mocks

Feature/phpunit mocks
This commit is contained in:
Alejandro Celaya 2022-10-21 19:42:58 +02:00 committed by GitHub
commit 92ddd2eebe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 218 additions and 250 deletions

View file

@ -5,10 +5,8 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Action;
use Laminas\Diactoros\ServerRequest;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Common\Response\PixelResponse;
use Shlinkio\Shlink\Core\Action\PixelAction;
@ -19,32 +17,29 @@ use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
class PixelActionTest extends TestCase
{
use ProphecyTrait;
private PixelAction $action;
private ObjectProphecy $urlResolver;
private ObjectProphecy $requestTracker;
private MockObject $urlResolver;
private MockObject $requestTracker;
protected function setUp(): void
{
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
$this->requestTracker = $this->prophesize(RequestTrackerInterface::class);
$this->urlResolver = $this->createMock(ShortUrlResolverInterface::class);
$this->requestTracker = $this->createMock(RequestTrackerInterface::class);
$this->action = new PixelAction($this->urlResolver->reveal(), $this->requestTracker->reveal());
$this->action = new PixelAction($this->urlResolver, $this->requestTracker);
}
/** @test */
public function imageIsReturned(): void
{
$shortCode = 'abc123';
$this->urlResolver->resolveEnabledShortUrl(
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''),
)->willReturn(ShortUrl::withLongUrl('http://domain.com/foo/bar'))
->shouldBeCalledOnce();
$this->requestTracker->trackIfApplicable(Argument::cetera())->shouldBeCalledOnce();
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, '')),
)->willReturn(ShortUrl::withLongUrl('http://domain.com/foo/bar'));
$this->requestTracker->expects($this->once())->method('trackIfApplicable')->withAnyParameters();
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
$response = $this->action->process($request, $this->prophesize(RequestHandlerInterface::class)->reveal());
$response = $this->action->process($request, $this->createMock(RequestHandlerInterface::class));
self::assertInstanceOf(PixelResponse::class, $response);
self::assertEquals(200, $response->getStatusCode());

View file

@ -7,10 +7,8 @@ namespace ShlinkioTest\Shlink\Core\Action;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequest;
use Laminas\Diactoros\ServerRequestFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\NullLogger;
@ -29,50 +27,43 @@ use function imagecreatefromstring;
class QrCodeActionTest extends TestCase
{
use ProphecyTrait;
private const WHITE = 0xFFFFFF;
private const BLACK = 0x0;
private ObjectProphecy $urlResolver;
private MockObject $urlResolver;
protected function setUp(): void
{
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
$this->urlResolver = $this->createMock(ShortUrlResolverInterface::class);
}
/** @test */
public function aNotFoundShortCodeWillDelegateIntoNextMiddleware(): void
{
$shortCode = 'abc123';
$this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''))
->willThrow(ShortUrlNotFoundException::class)
->shouldBeCalledOnce();
$delegate = $this->prophesize(RequestHandlerInterface::class);
$process = $delegate->handle(Argument::any())->willReturn(new Response());
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, '')),
)->willThrowException(ShortUrlNotFoundException::fromNotFound(ShortUrlIdentifier::fromShortCodeAndDomain('')));
$delegate = $this->createMock(RequestHandlerInterface::class);
$delegate->expects($this->once())->method('handle')->withAnyParameters()->willReturn(new Response());
$this->action()->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate->reveal());
$process->shouldHaveBeenCalledOnce();
$this->action()->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate);
}
/** @test */
public function aCorrectRequestReturnsTheQrCodeResponse(): void
{
$shortCode = 'abc123';
$this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''))
->willReturn(ShortUrl::createEmpty())
->shouldBeCalledOnce();
$delegate = $this->prophesize(RequestHandlerInterface::class);
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, '')),
)->willReturn(ShortUrl::createEmpty());
$delegate = $this->createMock(RequestHandlerInterface::class);
$delegate->expects($this->never())->method('handle');
$resp = $this->action()->process(
(new ServerRequest())->withAttribute('shortCode', $shortCode),
$delegate->reveal(),
);
$resp = $this->action()->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate);
self::assertInstanceOf(QrCodeResponse::class, $resp);
self::assertEquals(200, $resp->getStatusCode());
$delegate->handle(Argument::any())->shouldHaveBeenCalledTimes(0);
}
/**
@ -85,13 +76,13 @@ class QrCodeActionTest extends TestCase
string $expectedContentType,
): void {
$code = 'abc123';
$this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($code, ''))->willReturn(
ShortUrl::createEmpty(),
);
$delegate = $this->prophesize(RequestHandlerInterface::class);
$this->urlResolver->method('resolveEnabledShortUrl')->with(
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($code, '')),
)->willReturn(ShortUrl::createEmpty());
$delegate = $this->createMock(RequestHandlerInterface::class);
$req = (new ServerRequest())->withAttribute('shortCode', $code)->withQueryParams($query);
$resp = $this->action(new QrCodeOptions(format: $defaultFormat))->process($req, $delegate->reveal());
$resp = $this->action(new QrCodeOptions(format: $defaultFormat))->process($req, $delegate);
self::assertEquals($expectedContentType, $resp->getHeaderLine('Content-Type'));
}
@ -118,12 +109,12 @@ class QrCodeActionTest extends TestCase
int $expectedSize,
): void {
$code = 'abc123';
$this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($code, ''))->willReturn(
ShortUrl::createEmpty(),
);
$delegate = $this->prophesize(RequestHandlerInterface::class);
$this->urlResolver->method('resolveEnabledShortUrl')->with(
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($code, '')),
)->willReturn(ShortUrl::createEmpty());
$delegate = $this->createMock(RequestHandlerInterface::class);
$resp = $this->action($defaultOptions)->process($req->withAttribute('shortCode', $code), $delegate->reveal());
$resp = $this->action($defaultOptions)->process($req->withAttribute('shortCode', $code), $delegate);
[$size] = getimagesizefromstring($resp->getBody()->__toString());
self::assertEquals($expectedSize, $size);
@ -209,12 +200,12 @@ class QrCodeActionTest extends TestCase
->withQueryParams(['size' => 250, 'roundBlockSize' => $roundBlockSize])
->withAttribute('shortCode', $code);
$this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($code, ''))->willReturn(
ShortUrl::withLongUrl('https://shlink.io'),
);
$delegate = $this->prophesize(RequestHandlerInterface::class);
$this->urlResolver->method('resolveEnabledShortUrl')->with(
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($code, '')),
)->willReturn(ShortUrl::withLongUrl('https://shlink.io'));
$delegate = $this->createMock(RequestHandlerInterface::class);
$resp = $this->action($defaultOptions)->process($req, $delegate->reveal());
$resp = $this->action($defaultOptions)->process($req, $delegate);
$image = imagecreatefromstring($resp->getBody()->__toString());
$color = imagecolorat($image, 1, 1);
@ -246,7 +237,7 @@ class QrCodeActionTest extends TestCase
public function action(?QrCodeOptions $options = null): QrCodeAction
{
return new QrCodeAction(
$this->urlResolver->reveal(),
$this->urlResolver,
new ShortUrlStringifier(['domain' => 'doma.in']),
new NullLogger(),
$options ?? new QrCodeOptions(),

View file

@ -6,10 +6,8 @@ namespace ShlinkioTest\Shlink\Core\Action;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequest;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Core\Action\RedirectAction;
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
@ -22,29 +20,27 @@ use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
class RedirectActionTest extends TestCase
{
use ProphecyTrait;
private const LONG_URL = 'https://domain.com/foo/bar?some=thing';
private RedirectAction $action;
private ObjectProphecy $urlResolver;
private ObjectProphecy $requestTracker;
private ObjectProphecy $redirectRespHelper;
private MockObject $urlResolver;
private MockObject $requestTracker;
private MockObject $redirectRespHelper;
protected function setUp(): void
{
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
$this->requestTracker = $this->prophesize(RequestTrackerInterface::class);
$this->redirectRespHelper = $this->prophesize(RedirectResponseHelperInterface::class);
$this->urlResolver = $this->createMock(ShortUrlResolverInterface::class);
$this->requestTracker = $this->createMock(RequestTrackerInterface::class);
$this->redirectRespHelper = $this->createMock(RedirectResponseHelperInterface::class);
$redirectBuilder = $this->prophesize(ShortUrlRedirectionBuilderInterface::class);
$redirectBuilder->buildShortUrlRedirect(Argument::cetera())->willReturn(self::LONG_URL);
$redirectBuilder = $this->createMock(ShortUrlRedirectionBuilderInterface::class);
$redirectBuilder->method('buildShortUrlRedirect')->withAnyParameters()->willReturn(self::LONG_URL);
$this->action = new RedirectAction(
$this->urlResolver->reveal(),
$this->requestTracker->reveal(),
$redirectBuilder->reveal(),
$this->redirectRespHelper->reveal(),
$this->urlResolver,
$this->requestTracker,
$redirectBuilder,
$this->redirectRespHelper,
);
}
@ -53,38 +49,34 @@ class RedirectActionTest extends TestCase
{
$shortCode = 'abc123';
$shortUrl = ShortUrl::withLongUrl(self::LONG_URL);
$shortCodeToUrl = $this->urlResolver->resolveEnabledShortUrl(
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''),
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, '')),
)->willReturn($shortUrl);
$track = $this->requestTracker->trackIfApplicable(Argument::cetera())->will(function (): void {
});
$this->requestTracker->expects($this->once())->method('trackIfApplicable');
$expectedResp = new Response\RedirectResponse(self::LONG_URL);
$buildResp = $this->redirectRespHelper->buildRedirectResponse(self::LONG_URL)->willReturn($expectedResp);
$this->redirectRespHelper->expects($this->once())->method('buildRedirectResponse')->with(
$this->equalTo(self::LONG_URL),
)->willReturn($expectedResp);
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
$response = $this->action->process($request, $this->prophesize(RequestHandlerInterface::class)->reveal());
$response = $this->action->process($request, $this->createMock(RequestHandlerInterface::class));
self::assertSame($expectedResp, $response);
$buildResp->shouldHaveBeenCalledOnce();
$shortCodeToUrl->shouldHaveBeenCalledOnce();
$track->shouldHaveBeenCalledOnce();
}
/** @test */
public function nextMiddlewareIsInvokedIfLongUrlIsNotFound(): void
{
$shortCode = 'abc123';
$this->urlResolver->resolveEnabledShortUrl(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, ''))
->willThrow(ShortUrlNotFoundException::class)
->shouldBeCalledOnce();
$this->requestTracker->trackIfApplicable(Argument::cetera())->shouldNotBeCalled();
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->with(
$this->equalTo(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode, '')),
)->willThrowException(ShortUrlNotFoundException::fromNotFound(ShortUrlIdentifier::fromShortCodeAndDomain('')));
$this->requestTracker->expects($this->never())->method('trackIfApplicable');
$handler = $this->prophesize(RequestHandlerInterface::class);
$handle = $handler->handle(Argument::any())->willReturn(new Response());
$handler = $this->createMock(RequestHandlerInterface::class);
$handler->expects($this->once())->method('handle')->withAnyParameters()->willReturn(new Response());
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
$this->action->process($request, $handler->reveal());
$handle->shouldHaveBeenCalledOnce();
$this->action->process($request, $handler);
}
}

View file

@ -9,10 +9,8 @@ use Laminas\Diactoros\ServerRequestFactory;
use Laminas\Diactoros\Uri;
use Mezzio\Router\Route;
use Mezzio\Router\RouteResult;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use Psr\Http\Server\MiddlewareInterface;
@ -25,15 +23,13 @@ use Shlinkio\Shlink\Core\Util\RedirectResponseHelperInterface;
class NotFoundRedirectResolverTest extends TestCase
{
use ProphecyTrait;
private NotFoundRedirectResolver $resolver;
private ObjectProphecy $helper;
private MockObject $helper;
protected function setUp(): void
{
$this->helper = $this->prophesize(RedirectResponseHelperInterface::class);
$this->resolver = new NotFoundRedirectResolver($this->helper->reveal(), new NullLogger());
$this->helper = $this->createMock(RedirectResponseHelperInterface::class);
$this->resolver = new NotFoundRedirectResolver($this->helper, new NullLogger());
}
/**
@ -47,12 +43,13 @@ class NotFoundRedirectResolverTest extends TestCase
string $expectedRedirectTo,
): void {
$expectedResp = new Response();
$buildResp = $this->helper->buildRedirectResponse($expectedRedirectTo)->willReturn($expectedResp);
$this->helper->expects($this->once())->method('buildRedirectResponse')->with(
$this->equalTo($expectedRedirectTo),
)->willReturn($expectedResp);
$resp = $this->resolver->resolveRedirectResponse($notFoundType, $redirectConfig, $uri);
self::assertSame($expectedResp, $resp);
$buildResp->shouldHaveBeenCalledOnce();
}
public function provideRedirects(): iterable
@ -119,11 +116,11 @@ class NotFoundRedirectResolverTest extends TestCase
public function noResponseIsReturnedIfNoConditionsMatch(): void
{
$notFoundType = $this->notFoundType($this->requestForRoute('foo'));
$this->helper->expects($this->never())->method('buildRedirectResponse');
$result = $this->resolver->resolveRedirectResponse($notFoundType, new NotFoundRedirectOptions(), new Uri());
self::assertNull($result);
$this->helper->buildRedirectResponse(Argument::cetera())->shouldNotHaveBeenCalled();
}
private function notFoundType(ServerRequestInterface $req): NotFoundType
@ -139,7 +136,7 @@ class NotFoundRedirectResolverTest extends TestCase
RouteResult::fromRoute(
new Route(
'',
$this->prophesize(MiddlewareInterface::class)->reveal(),
$this->createMock(MiddlewareInterface::class),
['GET'],
$routeName,
),

View file

@ -5,39 +5,35 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Crawling;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Crawling\CrawlingHelper;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepositoryInterface;
class CrawlingHelperTest extends TestCase
{
use ProphecyTrait;
private CrawlingHelper $helper;
private ObjectProphecy $em;
private MockObject $em;
protected function setUp(): void
{
$this->em = $this->prophesize(EntityManagerInterface::class);
$this->helper = new CrawlingHelper($this->em->reveal());
$this->em = $this->createMock(EntityManagerInterface::class);
$this->helper = new CrawlingHelper($this->em);
}
/** @test */
public function listCrawlableShortCodesDelegatesIntoRepository(): void
{
$repo = $this->prophesize(ShortUrlRepositoryInterface::class);
$findCrawlableShortCodes = $repo->findCrawlableShortCodes()->willReturn([]);
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
$repo = $this->createMock(ShortUrlRepositoryInterface::class);
$repo->expects($this->once())->method('findCrawlableShortCodes')->willReturn([]);
$this->em->expects($this->once())->method('getRepository')->with($this->equalTo(ShortUrl::class))->willReturn(
$repo,
);
$result = $this->helper->listCrawlableShortCodes();
foreach ($result as $shortCode) {
// Result is a generator and therefore, it needs to be iterated
// $result is a generator and therefore, it needs to be iterated
}
$findCrawlableShortCodes->shouldHaveBeenCalledOnce();
$getRepo->shouldHaveBeenCalledOnce();
}
}

View file

@ -5,10 +5,8 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Domain;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Config\EmptyNotFoundRedirectConfig;
use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
use Shlinkio\Shlink\Core\Domain\DomainService;
@ -22,15 +20,13 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
class DomainServiceTest extends TestCase
{
use ProphecyTrait;
private DomainService $domainService;
private ObjectProphecy $em;
private MockObject $em;
protected function setUp(): void
{
$this->em = $this->prophesize(EntityManagerInterface::class);
$this->domainService = new DomainService($this->em->reveal(), 'default.com');
$this->em = $this->createMock(EntityManagerInterface::class);
$this->domainService = new DomainService($this->em, 'default.com');
}
/**
@ -39,15 +35,15 @@ class DomainServiceTest extends TestCase
*/
public function listDomainsDelegatesIntoRepository(array $domains, array $expectedResult, ?ApiKey $apiKey): void
{
$repo = $this->prophesize(DomainRepositoryInterface::class);
$getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
$findDomains = $repo->findDomains($apiKey)->willReturn($domains);
$repo = $this->createMock(DomainRepositoryInterface::class);
$repo->expects($this->once())->method('findDomains')->with($this->equalTo($apiKey))->willReturn($domains);
$this->em->expects($this->once())->method('getRepository')->with($this->equalTo(Domain::class))->willReturn(
$repo,
);
$result = $this->domainService->listDomains($apiKey);
self::assertEquals($expectedResult, $result);
$getRepo->shouldHaveBeenCalledOnce();
$findDomains->shouldHaveBeenCalledOnce();
}
public function provideExcludedDomains(): iterable
@ -109,10 +105,12 @@ class DomainServiceTest extends TestCase
/** @test */
public function getDomainThrowsExceptionWhenDomainIsNotFound(): void
{
$find = $this->em->find(Domain::class, '123')->willReturn(null);
$this->em->expects($this->once())->method('find')->with(
$this->equalTo(Domain::class),
$this->equalTo('123'),
)->willReturn(null);
$this->expectException(DomainNotFoundException::class);
$find->shouldBeCalledOnce();
$this->domainService->getDomain('123');
}
@ -121,12 +119,14 @@ class DomainServiceTest extends TestCase
public function getDomainReturnsEntityWhenFound(): void
{
$domain = Domain::withAuthority('');
$find = $this->em->find(Domain::class, '123')->willReturn($domain);
$this->em->expects($this->once())->method('find')->with(
$this->equalTo(Domain::class),
$this->equalTo('123'),
)->willReturn($domain);
$result = $this->domainService->getDomain('123');
self::assertSame($domain, $result);
$find->shouldHaveBeenCalledOnce();
}
/**
@ -136,20 +136,23 @@ class DomainServiceTest extends TestCase
public function getOrCreateAlwaysPersistsDomain(?Domain $foundDomain, ?ApiKey $apiKey): void
{
$authority = 'example.com';
$repo = $this->prophesize(DomainRepositoryInterface::class);
$repo->findOneByAuthority($authority, $apiKey)->willReturn($foundDomain);
$getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
$persist = $this->em->persist($foundDomain ?? Argument::type(Domain::class));
$flush = $this->em->flush();
$repo = $this->createMock(DomainRepositoryInterface::class);
$repo->method('findOneByAuthority')->with($this->equalTo($authority), $this->equalTo($apiKey))->willReturn(
$foundDomain,
);
$this->em->expects($this->once())->method('getRepository')->with($this->equalTo(Domain::class))->willReturn(
$repo,
);
$this->em->expects($this->once())->method('persist')->with(
$foundDomain !== null ? $this->equalTo($foundDomain) : $this->isInstanceOf(Domain::class),
);
$this->em->expects($this->once())->method('flush');
$result = $this->domainService->getOrCreate($authority, $apiKey);
if ($foundDomain !== null) {
self::assertSame($result, $foundDomain);
}
$getRepo->shouldHaveBeenCalledOnce();
$persist->shouldHaveBeenCalledOnce();
$flush->shouldHaveBeenCalledOnce();
}
/** @test */
@ -158,14 +161,17 @@ class DomainServiceTest extends TestCase
$authority = 'example.com';
$domain = Domain::withAuthority($authority)->setId('1');
$apiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forDomain($domain)));
$repo = $this->prophesize(DomainRepositoryInterface::class);
$repo->findOneByAuthority($authority, $apiKey)->willReturn(null);
$getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
$repo = $this->createMock(DomainRepositoryInterface::class);
$repo->method('findOneByAuthority')->with($this->equalTo($authority), $this->equalTo($apiKey))->willReturn(
null,
);
$this->em->expects($this->once())->method('getRepository')->with($this->equalTo(Domain::class))->willReturn(
$repo,
);
$this->em->expects($this->never())->method('persist');
$this->em->expects($this->never())->method('flush');
$this->expectException(DomainNotFoundException::class);
$getRepo->shouldBeCalledOnce();
$this->em->persist(Argument::cetera())->shouldNotBeCalled();
$this->em->flush()->shouldNotBeCalled();
$this->domainService->getOrCreate($authority, $apiKey);
}
@ -177,11 +183,17 @@ class DomainServiceTest extends TestCase
public function configureNotFoundRedirectsConfiguresFetchedDomain(?Domain $foundDomain, ?ApiKey $apiKey): void
{
$authority = 'example.com';
$repo = $this->prophesize(DomainRepositoryInterface::class);
$repo->findOneByAuthority($authority, $apiKey)->willReturn($foundDomain);
$getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
$persist = $this->em->persist($foundDomain ?? Argument::type(Domain::class));
$flush = $this->em->flush();
$repo = $this->createMock(DomainRepositoryInterface::class);
$repo->method('findOneByAuthority')->with($this->equalTo($authority), $this->equalTo($apiKey))->willReturn(
$foundDomain,
);
$this->em->expects($this->once())->method('getRepository')->with($this->equalTo(Domain::class))->willReturn(
$repo,
);
$this->em->expects($this->once())->method('persist')->with(
$foundDomain !== null ? $this->equalTo($foundDomain) : $this->isInstanceOf(Domain::class),
);
$this->em->expects($this->once())->method('flush');
$result = $this->domainService->configureNotFoundRedirects($authority, NotFoundRedirects::withRedirects(
'foo.com',
@ -195,9 +207,6 @@ class DomainServiceTest extends TestCase
self::assertEquals('foo.com', $result->baseUrlRedirect());
self::assertEquals('bar.com', $result->regular404Redirect());
self::assertEquals('baz.com', $result->invalidShortUrlRedirect());
$getRepo->shouldHaveBeenCalledOnce();
$persist->shouldHaveBeenCalledOnce();
$flush->shouldHaveBeenCalledOnce();
}
public function provideFoundDomains(): iterable

View file

@ -6,10 +6,8 @@ namespace ShlinkioTest\Shlink\Core\ErrorHandler;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequestFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use Psr\Http\Server\RequestHandlerInterface;
@ -22,31 +20,25 @@ use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
class NotFoundRedirectHandlerTest extends TestCase
{
use ProphecyTrait;
private NotFoundRedirectHandler $middleware;
private NotFoundRedirectOptions $redirectOptions;
private ObjectProphecy $resolver;
private ObjectProphecy $domainService;
private ObjectProphecy $next;
private MockObject $resolver;
private MockObject $domainService;
private MockObject $next;
private ServerRequestInterface $req;
protected function setUp(): void
{
$this->redirectOptions = new NotFoundRedirectOptions();
$this->resolver = $this->prophesize(NotFoundRedirectResolverInterface::class);
$this->domainService = $this->prophesize(DomainServiceInterface::class);
$this->resolver = $this->createMock(NotFoundRedirectResolverInterface::class);
$this->domainService = $this->createMock(DomainServiceInterface::class);
$this->middleware = new NotFoundRedirectHandler(
$this->redirectOptions,
$this->resolver->reveal(),
$this->domainService->reveal(),
);
$this->middleware = new NotFoundRedirectHandler($this->redirectOptions, $this->resolver, $this->domainService);
$this->next = $this->prophesize(RequestHandlerInterface::class);
$this->next = $this->createMock(RequestHandlerInterface::class);
$this->req = ServerRequestFactory::fromGlobals()->withAttribute(
NotFoundType::class,
$this->prophesize(NotFoundType::class)->reveal(),
$this->createMock(NotFoundType::class),
);
}
@ -59,40 +51,49 @@ class NotFoundRedirectHandlerTest extends TestCase
$expectedResp = new Response();
$setUp($this->domainService, $this->resolver);
$handle = $this->next->handle($this->req)->willReturn($expectedResp);
$this->next->expects($this->once())->method('handle')->with($this->equalTo($this->req))->willReturn(
$expectedResp,
);
$result = $this->middleware->process($this->req, $this->next->reveal());
$result = $this->middleware->process($this->req, $this->next);
self::assertSame($expectedResp, $result);
$handle->shouldHaveBeenCalledOnce();
}
public function provideNonRedirectScenarios(): iterable
{
yield 'no domain' => [function (ObjectProphecy $domainService, ObjectProphecy $resolver): void {
$domainService->findByAuthority(Argument::cetera())
->willReturn(null)
->shouldBeCalledOnce();
$resolver->resolveRedirectResponse(
Argument::type(NotFoundType::class),
Argument::type(NotFoundRedirectOptions::class),
Argument::type(UriInterface::class),
)->willReturn(null)->shouldBeCalledOnce();
yield 'no domain' => [function (
MockObject&DomainServiceInterface $domainService,
MockObject&NotFoundRedirectResolverInterface $resolver,
): void {
$domainService->expects($this->once())->method('findByAuthority')->withAnyParameters()->willReturn(
null,
);
$resolver->expects($this->once())->method('resolveRedirectResponse')->with(
$this->isInstanceOf(NotFoundType::class),
$this->isInstanceOf(NotFoundRedirectOptions::class),
$this->isInstanceOf(UriInterface::class),
)->willReturn(null);
}];
yield 'non-redirecting domain' => [function (ObjectProphecy $domainService, ObjectProphecy $resolver): void {
$domainService->findByAuthority(Argument::cetera())
->willReturn(Domain::withAuthority(''))
->shouldBeCalledOnce();
$resolver->resolveRedirectResponse(
Argument::type(NotFoundType::class),
Argument::type(NotFoundRedirectOptions::class),
Argument::type(UriInterface::class),
)->willReturn(null)->shouldBeCalledOnce();
$resolver->resolveRedirectResponse(
Argument::type(NotFoundType::class),
Argument::type(Domain::class),
Argument::type(UriInterface::class),
)->willReturn(null)->shouldBeCalledOnce();
yield 'non-redirecting domain' => [function (
MockObject&DomainServiceInterface $domainService,
MockObject&NotFoundRedirectResolverInterface $resolver,
): void {
$domainService->expects($this->once())->method('findByAuthority')->withAnyParameters()->willReturn(
Domain::withAuthority(''),
);
$resolver->expects($this->exactly(2))->method('resolveRedirectResponse')->withConsecutive(
[
$this->isInstanceOf(NotFoundType::class),
$this->isInstanceOf(Domain::class),
$this->isInstanceOf(UriInterface::class),
],
[
$this->isInstanceOf(NotFoundType::class),
$this->isInstanceOf(NotFoundRedirectOptions::class),
$this->isInstanceOf(UriInterface::class),
],
)->willReturn(null);
}];
}
@ -101,19 +102,17 @@ class NotFoundRedirectHandlerTest extends TestCase
{
$expectedResp = new Response();
$findDomain = $this->domainService->findByAuthority(Argument::cetera())->willReturn(null);
$resolveRedirect = $this->resolver->resolveRedirectResponse(
Argument::type(NotFoundType::class),
$this->redirectOptions,
Argument::type(UriInterface::class),
$this->domainService->expects($this->once())->method('findByAuthority')->withAnyParameters()->willReturn(null);
$this->resolver->expects($this->once())->method('resolveRedirectResponse')->with(
$this->isInstanceOf(NotFoundType::class),
$this->equalTo($this->redirectOptions),
$this->isInstanceOf(UriInterface::class),
)->willReturn($expectedResp);
$this->next->expects($this->never())->method('handle');
$result = $this->middleware->process($this->req, $this->next->reveal());
$result = $this->middleware->process($this->req, $this->next);
self::assertSame($expectedResp, $result);
$findDomain->shouldHaveBeenCalledOnce();
$resolveRedirect->shouldHaveBeenCalledOnce();
$this->next->handle(Argument::cetera())->shouldNotHaveBeenCalled();
}
/** @test */
@ -122,18 +121,18 @@ class NotFoundRedirectHandlerTest extends TestCase
$expectedResp = new Response();
$domain = Domain::withAuthority('');
$findDomain = $this->domainService->findByAuthority(Argument::cetera())->willReturn($domain);
$resolveRedirect = $this->resolver->resolveRedirectResponse(
Argument::type(NotFoundType::class),
$this->domainService->expects($this->once())->method('findByAuthority')->withAnyParameters()->willReturn(
$domain,
Argument::type(UriInterface::class),
);
$this->resolver->expects($this->once())->method('resolveRedirectResponse')->with(
$this->isInstanceOf(NotFoundType::class),
$this->equalTo($domain),
$this->isInstanceOf(UriInterface::class),
)->willReturn($expectedResp);
$this->next->expects($this->never())->method('handle');
$result = $this->middleware->process($this->req, $this->next->reveal());
$result = $this->middleware->process($this->req, $this->next);
self::assertSame($expectedResp, $result);
$findDomain->shouldHaveBeenCalledOnce();
$resolveRedirect->shouldHaveBeenCalledOnce();
$this->next->handle(Argument::cetera())->shouldNotHaveBeenCalled();
}
}

View file

@ -56,7 +56,7 @@ class NotFoundTemplateHandlerTest extends TestCase
RouteResult::fromRoute(
new Route(
'',
$this->prophesize(MiddlewareInterface::class)->reveal(),
$this->createMock(MiddlewareInterface::class),
['GET'],
RedirectAction::class,
),

View file

@ -4,12 +4,9 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\ErrorHandler;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequestFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
@ -18,35 +15,31 @@ use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
class NotFoundTrackerMiddlewareTest extends TestCase
{
use ProphecyTrait;
private NotFoundTrackerMiddleware $middleware;
private ServerRequestInterface $request;
private ObjectProphecy $requestTracker;
private ObjectProphecy $notFoundType;
private ObjectProphecy $handler;
private MockObject $handler;
private MockObject $requestTracker;
protected function setUp(): void
{
$this->notFoundType = $this->prophesize(NotFoundType::class);
$this->handler = $this->prophesize(RequestHandlerInterface::class);
$this->handler->handle(Argument::cetera())->willReturn(new Response());
$this->requestTracker = $this->prophesize(RequestTrackerInterface::class);
$this->middleware = new NotFoundTrackerMiddleware($this->requestTracker->reveal());
$this->handler = $this->createMock(RequestHandlerInterface::class);
$this->requestTracker = $this->createMock(RequestTrackerInterface::class);
$this->middleware = new NotFoundTrackerMiddleware($this->requestTracker);
$this->request = ServerRequestFactory::fromGlobals()->withAttribute(
NotFoundType::class,
$this->notFoundType->reveal(),
$this->createMock(NotFoundType::class),
);
}
/** @test */
public function delegatesIntoRequestTracker(): void
{
$this->middleware->process($this->request, $this->handler->reveal());
$this->handler->expects($this->once())->method('handle')->with($this->equalTo($this->request));
$this->requestTracker->expects($this->once())->method('trackNotFoundIfApplicable')->with(
$this->equalTo($this->request),
);
$this->requestTracker->trackNotFoundIfApplicable($this->request)->shouldHaveBeenCalledOnce();
$this->handler->handle($this->request)->shouldHaveBeenCalledOnce();
$this->middleware->process($this->request, $this->handler);
}
}

View file

@ -7,10 +7,8 @@ namespace ShlinkioTest\Shlink\Core\ErrorHandler;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequestFactory;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
@ -18,30 +16,28 @@ use Shlinkio\Shlink\Core\ErrorHandler\NotFoundTypeResolverMiddleware;
class NotFoundTypeResolverMiddlewareTest extends TestCase
{
use ProphecyTrait;
private NotFoundTypeResolverMiddleware $middleware;
private ObjectProphecy $handler;
private MockObject $handler;
protected function setUp(): void
{
$this->middleware = new NotFoundTypeResolverMiddleware('');
$this->handler = $this->prophesize(RequestHandlerInterface::class);
$this->handler = $this->createMock(RequestHandlerInterface::class);
}
/** @test */
public function notFoundTypeIsAddedToRequest(): void
{
$request = ServerRequestFactory::fromGlobals();
$handle = $this->handler->handle(Argument::that(function (ServerRequestInterface $req) {
Assert::assertArrayHasKey(NotFoundType::class, $req->getAttributes());
$this->handler->expects($this->once())->method('handle')->with(
$this->callback(function (ServerRequestInterface $req): bool {
Assert::assertArrayHasKey(NotFoundType::class, $req->getAttributes());
return true;
}),
)->willReturn(new Response());
return true;
}))->willReturn(new Response());
$this->middleware->process($request, $this->handler->reveal());
$this->middleware->process($request, $this->handler);
self::assertArrayNotHasKey(NotFoundType::class, $request->getAttributes());
$handle->shouldHaveBeenCalledOnce();
}
}