mirror of
https://github.com/shlinkio/shlink.git
synced 2024-10-22 20:25:35 +03:00
Created InitialApiKeyDelegatorTest
This commit is contained in:
parent
997289da02
commit
0e54ed691d
2 changed files with 63 additions and 0 deletions
|
@ -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' => [
|
||||
|
|
61
module/Rest/test/ApiKey/InitialApiKeyDelegatorTest.php
Normal file
61
module/Rest/test/ApiKey/InitialApiKeyDelegatorTest.php
Normal 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];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue