Migrated TagVisitsActionTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-23 22:28:29 +02:00
parent d414496a3c
commit 7aa6afeb30

View File

@ -6,10 +6,8 @@ namespace ShlinkioTest\Shlink\Rest\Action\Visit;
use Laminas\Diactoros\ServerRequestFactory;
use Pagerfanta\Adapter\ArrayAdapter;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Common\Paginator\Paginator;
use Shlinkio\Shlink\Core\Visit\Model\VisitsParams;
use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface;
@ -18,15 +16,13 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
class TagVisitsActionTest extends TestCase
{
use ProphecyTrait;
private TagVisitsAction $action;
private ObjectProphecy $visitsHelper;
private MockObject $visitsHelper;
protected function setUp(): void
{
$this->visitsHelper = $this->prophesize(VisitsStatsHelperInterface::class);
$this->action = new TagVisitsAction($this->visitsHelper->reveal());
$this->visitsHelper = $this->createMock(VisitsStatsHelperInterface::class);
$this->action = new TagVisitsAction($this->visitsHelper);
}
/** @test */
@ -34,15 +30,16 @@ class TagVisitsActionTest extends TestCase
{
$tag = 'foo';
$apiKey = ApiKey::create();
$getVisits = $this->visitsHelper->visitsForTag($tag, Argument::type(VisitsParams::class), $apiKey)->willReturn(
new Paginator(new ArrayAdapter([])),
);
$this->visitsHelper->expects($this->once())->method('visitsForTag')->with(
$tag,
$this->isInstanceOf(VisitsParams::class),
$apiKey,
)->willReturn(new Paginator(new ArrayAdapter([])));
$response = $this->action->handle(
ServerRequestFactory::fromGlobals()->withAttribute('tag', $tag)->withAttribute(ApiKey::class, $apiKey),
);
self::assertEquals(200, $response->getStatusCode());
$getVisits->shouldHaveBeenCalledOnce();
}
}