shlink/module/CLI/test/Command/Api/GenerateKeyCommandTest.php

76 lines
2.4 KiB
PHP
Raw Normal View History

<?php
2019-10-05 18:26:10 +03:00
2017-10-12 11:13:20 +03:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\Command\Api;
use Cake\Chronos\Chronos;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\CLI\ApiKey\RoleResolverInterface;
use Shlinkio\Shlink\CLI\Command\Api\GenerateKeyCommand;
2018-07-31 20:59:41 +03:00
use Shlinkio\Shlink\Rest\Entity\ApiKey;
2019-12-30 00:27:00 +03:00
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Tester\CommandTester;
class GenerateKeyCommandTest extends TestCase
{
use CliTestUtilsTrait;
2020-11-02 13:50:19 +03:00
2019-12-30 00:27:00 +03:00
private CommandTester $commandTester;
2022-10-24 20:53:13 +03:00
private MockObject & ApiKeyServiceInterface $apiKeyService;
protected function setUp(): void
{
$this->apiKeyService = $this->createMock(ApiKeyServiceInterface::class);
$roleResolver = $this->createMock(RoleResolverInterface::class);
$roleResolver->method('determineRoles')->with($this->isInstanceOf(InputInterface::class))->willReturn([]);
$command = new GenerateKeyCommand($this->apiKeyService, $roleResolver);
$this->commandTester = $this->testerForCommand($command);
}
#[Test]
2020-01-01 22:48:31 +03:00
public function noExpirationDateIsDefinedIfNotProvided(): void
{
$this->apiKeyService->expects($this->once())->method('create')->with(
$this->isNull(),
$this->isNull(),
)->willReturn(ApiKey::create());
2018-11-17 19:36:22 +03:00
$this->commandTester->execute([]);
2018-11-17 19:36:22 +03:00
$output = $this->commandTester->getDisplay();
2020-10-04 01:35:14 +03:00
self::assertStringContainsString('Generated API key: ', $output);
}
#[Test]
2020-01-01 22:48:31 +03:00
public function expirationDateIsDefinedIfProvided(): void
{
$this->apiKeyService->expects($this->once())->method('create')->with(
$this->isInstanceOf(Chronos::class),
$this->isNull(),
)->willReturn(ApiKey::create());
2021-03-06 20:27:34 +03:00
$this->commandTester->execute([
'--expiration-date' => '2016-01-01',
]);
}
2021-03-06 20:27:34 +03:00
#[Test]
2021-03-06 20:27:34 +03:00
public function nameIsDefinedIfProvided(): void
{
$this->apiKeyService->expects($this->once())->method('create')->with(
$this->isNull(),
$this->isType('string'),
)->willReturn(ApiKey::create());
2021-03-06 20:27:34 +03:00
$this->commandTester->execute([
'--name' => 'Alice',
]);
}
}