Created InitialApiKeyDelegatorTest

This commit is contained in:
Alejandro Celaya 2022-09-11 12:10:21 +02:00
parent 997289da02
commit 0e54ed691d
2 changed files with 63 additions and 0 deletions

View file

@ -11,6 +11,8 @@ use const PHP_SAPI;
return [
// We will try to load the initial API key only for openswoole and RoadRunner.
// For php-fpm, the check against the database would happen on every request, resulting in a very bad performance.
'initial_api_key' => PHP_SAPI !== 'cli' ? null : EnvVars::INITIAL_API_KEY->loadFromEnv(),
'dependencies' => [

View file

@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\ApiKey;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Mezzio\Application;
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;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
class InitialApiKeyDelegatorTest extends TestCase
{
use ProphecyTrait;
private InitialApiKeyDelegator $delegator;
private ObjectProphecy $container;
protected function setUp(): void
{
$this->delegator = new InitialApiKeyDelegator();
$this->container = $this->prophesize(ContainerInterface::class);
}
/**
* @test
* @dataProvider provideConfigs
*/
public function apiKeyIsInitializedWhenAppropriate(array $config, int $expectedCalls): void
{
$app = $this->prophesize(Application::class)->reveal();
$apiKeyRepo = $this->prophesize(ApiKeyRepositoryInterface::class);
$em = $this->prophesize(EntityManagerInterface::class);
$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);
self::assertSame($result, $app);
$getConfig->shouldHaveBeenCalledOnce();
$getRepo->shouldHaveBeenCalledTimes($expectedCalls);
$getEm->shouldHaveBeenCalledTimes($expectedCalls);
$apiKeyRepo->createInitialApiKey(Argument::any())->shouldHaveBeenCalledTimes($expectedCalls);
}
public function provideConfigs(): iterable
{
yield [[], 0];
yield [['initial_api_key' => null], 0];
yield [['initial_api_key' => 'the_initial_key'], 1];
}
}