2022-09-11 13:10:21 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Rest\ApiKey;
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManager;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Mezzio\Application;
|
2022-10-23 23:36:16 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2022-09-11 13:10:21 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psr\Container\ContainerInterface;
|
|
|
|
use Shlinkio\Shlink\Rest\ApiKey\InitialApiKeyDelegator;
|
|
|
|
use Shlinkio\Shlink\Rest\ApiKey\Repository\ApiKeyRepositoryInterface;
|
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
|
|
|
|
|
|
|
class InitialApiKeyDelegatorTest extends TestCase
|
|
|
|
{
|
|
|
|
private InitialApiKeyDelegator $delegator;
|
2022-10-23 23:36:16 +03:00
|
|
|
private MockObject $container;
|
2022-09-11 13:10:21 +03:00
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
$this->delegator = new InitialApiKeyDelegator();
|
2022-10-23 23:36:16 +03:00
|
|
|
$this->container = $this->createMock(ContainerInterface::class);
|
2022-09-11 13:10:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideConfigs
|
|
|
|
*/
|
|
|
|
public function apiKeyIsInitializedWhenAppropriate(array $config, int $expectedCalls): void
|
|
|
|
{
|
2022-10-23 23:36:16 +03:00
|
|
|
$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],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$result = ($this->delegator)($this->container, '', fn () => $app);
|
2022-09-11 13:10:21 +03:00
|
|
|
|
|
|
|
self::assertSame($result, $app);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideConfigs(): iterable
|
|
|
|
{
|
2022-09-18 10:29:38 +03:00
|
|
|
yield 'no api key' => [[], 0];
|
|
|
|
yield 'null api key' => [['initial_api_key' => null], 0];
|
|
|
|
yield 'empty api key' => [['initial_api_key' => ''], 0];
|
|
|
|
yield 'valid api key' => [['initial_api_key' => 'the_initial_key'], 1];
|
2022-09-11 13:10:21 +03:00
|
|
|
}
|
|
|
|
}
|