Migrated ListTagsActionTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-23 22:19:14 +02:00
parent ab9ea887d2
commit 656083cb6f

View File

@ -7,10 +7,8 @@ namespace ShlinkioTest\Shlink\Rest\Action\Tag;
use Laminas\Diactoros\Response\JsonResponse; use Laminas\Diactoros\Response\JsonResponse;
use Laminas\Diactoros\ServerRequestFactory; use Laminas\Diactoros\ServerRequestFactory;
use Pagerfanta\Adapter\ArrayAdapter; use Pagerfanta\Adapter\ArrayAdapter;
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\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Common\Paginator\Paginator; use Shlinkio\Shlink\Common\Paginator\Paginator;
use Shlinkio\Shlink\Core\Tag\Entity\Tag; use Shlinkio\Shlink\Core\Tag\Entity\Tag;
@ -23,15 +21,13 @@ use function count;
class ListTagsActionTest extends TestCase class ListTagsActionTest extends TestCase
{ {
use ProphecyTrait;
private ListTagsAction $action; private ListTagsAction $action;
private ObjectProphecy $tagService; private MockObject $tagService;
protected function setUp(): void protected function setUp(): void
{ {
$this->tagService = $this->prophesize(TagServiceInterface::class); $this->tagService = $this->createMock(TagServiceInterface::class);
$this->action = new ListTagsAction($this->tagService->reveal()); $this->action = new ListTagsAction($this->tagService);
} }
/** /**
@ -42,9 +38,10 @@ class ListTagsActionTest extends TestCase
{ {
$tags = [new Tag('foo'), new Tag('bar')]; $tags = [new Tag('foo'), new Tag('bar')];
$tagsCount = count($tags); $tagsCount = count($tags);
$listTags = $this->tagService->listTags(Argument::any(), Argument::type(ApiKey::class))->willReturn( $this->tagService->expects($this->once())->method('listTags')->with(
new Paginator(new ArrayAdapter($tags)), $this->anything(),
); $this->isInstanceOf(ApiKey::class),
)->willReturn(new Paginator(new ArrayAdapter($tags)));
/** @var JsonResponse $resp */ /** @var JsonResponse $resp */
$resp = $this->action->handle($this->requestWithApiKey()->withQueryParams($query)); $resp = $this->action->handle($this->requestWithApiKey()->withQueryParams($query));
@ -62,7 +59,6 @@ class ListTagsActionTest extends TestCase
], ],
], ],
], $payload); ], $payload);
$listTags->shouldHaveBeenCalled();
} }
public function provideNoStatsQueries(): iterable public function provideNoStatsQueries(): iterable
@ -80,9 +76,10 @@ class ListTagsActionTest extends TestCase
new TagInfo('bar', 3, 10), new TagInfo('bar', 3, 10),
]; ];
$itemsCount = count($stats); $itemsCount = count($stats);
$tagsInfo = $this->tagService->tagsInfo(Argument::any(), Argument::type(ApiKey::class))->willReturn( $this->tagService->expects($this->once())->method('tagsInfo')->with(
new Paginator(new ArrayAdapter($stats)), $this->anything(),
); $this->isInstanceOf(ApiKey::class),
)->willReturn(new Paginator(new ArrayAdapter($stats)));
$req = $this->requestWithApiKey()->withQueryParams(['withStats' => 'true']); $req = $this->requestWithApiKey()->withQueryParams(['withStats' => 'true']);
/** @var JsonResponse $resp */ /** @var JsonResponse $resp */
@ -102,7 +99,6 @@ class ListTagsActionTest extends TestCase
], ],
], ],
], $payload); ], $payload);
$tagsInfo->shouldHaveBeenCalled();
} }
private function requestWithApiKey(): ServerRequestInterface private function requestWithApiKey(): ServerRequestInterface