mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-14 04:00:57 +03:00
Migrated InitialApiKeyDelegatorTest to use PHPUnit mocks
This commit is contained in:
parent
7aa6afeb30
commit
b1f814e118
1 changed files with 15 additions and 19 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue