Migrated InitialApiKeyDelegatorTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-23 22:36:16 +02:00
parent 7aa6afeb30
commit b1f814e118

View File

@ -7,10 +7,8 @@ namespace ShlinkioTest\Shlink\Rest\ApiKey;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Mezzio\Application; use Mezzio\Application;
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\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Shlinkio\Shlink\Rest\ApiKey\InitialApiKeyDelegator; use Shlinkio\Shlink\Rest\ApiKey\InitialApiKeyDelegator;
use Shlinkio\Shlink\Rest\ApiKey\Repository\ApiKeyRepositoryInterface; use Shlinkio\Shlink\Rest\ApiKey\Repository\ApiKeyRepositoryInterface;
@ -18,15 +16,13 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
class InitialApiKeyDelegatorTest extends TestCase class InitialApiKeyDelegatorTest extends TestCase
{ {
use ProphecyTrait;
private InitialApiKeyDelegator $delegator; private InitialApiKeyDelegator $delegator;
private ObjectProphecy $container; private MockObject $container;
protected function setUp(): void protected function setUp(): void
{ {
$this->delegator = new InitialApiKeyDelegator(); $this->delegator = new InitialApiKeyDelegator();
$this->container = $this->prophesize(ContainerInterface::class); $this->container = $this->createMock(ContainerInterface::class);
} }
/** /**
@ -35,21 +31,21 @@ class InitialApiKeyDelegatorTest extends TestCase
*/ */
public function apiKeyIsInitializedWhenAppropriate(array $config, int $expectedCalls): void public function apiKeyIsInitializedWhenAppropriate(array $config, int $expectedCalls): void
{ {
$app = $this->prophesize(Application::class)->reveal(); $app = $this->createMock(Application::class);
$apiKeyRepo = $this->prophesize(ApiKeyRepositoryInterface::class); $apiKeyRepo = $this->createMock(ApiKeyRepositoryInterface::class);
$em = $this->prophesize(EntityManagerInterface::class); $apiKeyRepo->expects($this->exactly($expectedCalls))->method('createInitialApiKey');
$em = $this->createMock(EntityManagerInterface::class);
$em->expects($this->exactly($expectedCalls))->method('getRepository')->with(ApiKey::class)->willReturn(
$apiKeyRepo,
);
$this->container->expects($this->exactly($expectedCalls + 1))->method('get')->willReturnMap([
['config', $config],
[EntityManager::class, $em],
]);
$getConfig = $this->container->get('config')->willReturn($config); $result = ($this->delegator)($this->container, '', fn () => $app);
$getRepo = $em->getRepository(ApiKey::class)->willReturn($apiKeyRepo->reveal());
$getEm = $this->container->get(EntityManager::class)->willReturn($em->reveal());
$result = ($this->delegator)($this->container->reveal(), '', fn () => $app);
self::assertSame($result, $app); self::assertSame($result, $app);
$getConfig->shouldHaveBeenCalledOnce();
$getRepo->shouldHaveBeenCalledTimes($expectedCalls);
$getEm->shouldHaveBeenCalledTimes($expectedCalls);
$apiKeyRepo->createInitialApiKey(Argument::any())->shouldHaveBeenCalledTimes($expectedCalls);
} }
public function provideConfigs(): iterable public function provideConfigs(): iterable