Migrated MercureInfoActionTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-23 22:04:00 +02:00
parent 66ed152358
commit 896b7f2d73

View File

@ -7,23 +7,19 @@ namespace ShlinkioTest\Shlink\Rest\Action;
use Cake\Chronos\Chronos; use Cake\Chronos\Chronos;
use Laminas\Diactoros\Response\JsonResponse; use Laminas\Diactoros\Response\JsonResponse;
use Laminas\Diactoros\ServerRequestFactory; use Laminas\Diactoros\ServerRequestFactory;
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 Shlinkio\Shlink\Common\Mercure\JwtProviderInterface; use Shlinkio\Shlink\Common\Mercure\JwtProviderInterface;
use Shlinkio\Shlink\Rest\Action\MercureInfoAction; use Shlinkio\Shlink\Rest\Action\MercureInfoAction;
use Shlinkio\Shlink\Rest\Exception\MercureException; use Shlinkio\Shlink\Rest\Exception\MercureException;
class MercureInfoActionTest extends TestCase class MercureInfoActionTest extends TestCase
{ {
use ProphecyTrait; private MockObject $provider;
private ObjectProphecy $provider;
protected function setUp(): void protected function setUp(): void
{ {
$this->provider = $this->prophesize(JwtProviderInterface::class); $this->provider = $this->createMock(JwtProviderInterface::class);
} }
/** /**
@ -32,12 +28,11 @@ class MercureInfoActionTest extends TestCase
*/ */
public function throwsExceptionWhenConfigDoesNotHavePublicHost(array $mercureConfig): void public function throwsExceptionWhenConfigDoesNotHavePublicHost(array $mercureConfig): void
{ {
$buildToken = $this->provider->buildSubscriptionToken(Argument::any())->willReturn('abc.123'); $this->provider->expects($this->never())->method('buildSubscriptionToken');
$action = new MercureInfoAction($this->provider->reveal(), $mercureConfig); $action = new MercureInfoAction($this->provider, $mercureConfig);
$this->expectException(MercureException::class); $this->expectException(MercureException::class);
$buildToken->shouldNotBeCalled();
$action->handle(ServerRequestFactory::fromGlobals()); $action->handle(ServerRequestFactory::fromGlobals());
} }
@ -60,9 +55,9 @@ class MercureInfoActionTest extends TestCase
*/ */
public function returnsExpectedInfoWhenEverythingIsOk(?int $days): void public function returnsExpectedInfoWhenEverythingIsOk(?int $days): void
{ {
$buildToken = $this->provider->buildSubscriptionToken(Argument::any())->willReturn('abc.123'); $this->provider->expects($this->once())->method('buildSubscriptionToken')->willReturn('abc.123');
$action = new MercureInfoAction($this->provider->reveal(), [ $action = new MercureInfoAction($this->provider, [
'public_hub_url' => 'http://foobar.com', 'public_hub_url' => 'http://foobar.com',
'jwt_days_duration' => $days, 'jwt_days_duration' => $days,
]); ]);
@ -79,7 +74,6 @@ class MercureInfoActionTest extends TestCase
Chronos::now()->addDays($days ?? 1)->startOfDay(), Chronos::now()->addDays($days ?? 1)->startOfDay(),
Chronos::parse($payload['jwtExpiration'])->startOfDay(), Chronos::parse($payload['jwtExpiration'])->startOfDay(),
); );
$buildToken->shouldHaveBeenCalledOnce();
} }
public function provideDays(): iterable public function provideDays(): iterable