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\EntityManagerInterface;
use Mezzio\Application;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Container\ContainerInterface;
use Shlinkio\Shlink\Rest\ApiKey\InitialApiKeyDelegator;
use Shlinkio\Shlink\Rest\ApiKey\Repository\ApiKeyRepositoryInterface;
@ -18,15 +16,13 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
class InitialApiKeyDelegatorTest extends TestCase
{
use ProphecyTrait;
private InitialApiKeyDelegator $delegator;
private ObjectProphecy $container;
private MockObject $container;
protected function setUp(): void
{
$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
{
$app = $this->prophesize(Application::class)->reveal();
$apiKeyRepo = $this->prophesize(ApiKeyRepositoryInterface::class);
$em = $this->prophesize(EntityManagerInterface::class);
$app = $this->createMock(Application::class);
$apiKeyRepo = $this->createMock(ApiKeyRepositoryInterface::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);
$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);
$result = ($this->delegator)($this->container, '', fn () => $app);
self::assertSame($result, $app);
$getConfig->shouldHaveBeenCalledOnce();
$getRepo->shouldHaveBeenCalledTimes($expectedCalls);
$getEm->shouldHaveBeenCalledTimes($expectedCalls);
$apiKeyRepo->createInitialApiKey(Argument::any())->shouldHaveBeenCalledTimes($expectedCalls);
}
public function provideConfigs(): iterable