|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\Api; |
|
6
|
|
|
|
|
7
|
|
|
use Cake\Chronos\Chronos; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Prophecy\Argument; |
|
10
|
|
|
use Prophecy\PhpUnit\ProphecyTrait; |
|
11
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
12
|
|
|
use Shlinkio\Shlink\CLI\Command\Api\GenerateKeyCommand; |
|
13
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey; |
|
14
|
|
|
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; |
|
15
|
|
|
use Symfony\Component\Console\Application; |
|
16
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
|
17
|
|
|
|
|
18
|
|
|
class GenerateKeyCommandTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
use ProphecyTrait; |
|
21
|
|
|
|
|
22
|
|
|
private CommandTester $commandTester; |
|
23
|
|
|
private ObjectProphecy $apiKeyService; |
|
24
|
|
|
|
|
25
|
|
|
public function setUp(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$this->apiKeyService = $this->prophesize(ApiKeyServiceInterface::class); |
|
28
|
|
|
$command = new GenerateKeyCommand($this->apiKeyService->reveal()); |
|
29
|
|
|
$app = new Application(); |
|
30
|
|
|
$app->add($command); |
|
31
|
|
|
$this->commandTester = new CommandTester($command); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** @test */ |
|
35
|
|
|
public function noExpirationDateIsDefinedIfNotProvided(): void |
|
36
|
|
|
{ |
|
37
|
|
|
$create = $this->apiKeyService->create(null)->willReturn(new ApiKey()); |
|
38
|
|
|
|
|
39
|
|
|
$this->commandTester->execute([]); |
|
40
|
|
|
$output = $this->commandTester->getDisplay(); |
|
41
|
|
|
|
|
42
|
|
|
self::assertStringContainsString('Generated API key: ', $output); |
|
43
|
|
|
$create->shouldHaveBeenCalledOnce(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** @test */ |
|
47
|
|
|
public function expirationDateIsDefinedIfProvided(): void |
|
48
|
|
|
{ |
|
49
|
|
|
$this->apiKeyService->create(Argument::type(Chronos::class))->shouldBeCalledOnce() |
|
50
|
|
|
->willReturn(new ApiKey()); |
|
51
|
|
|
$this->commandTester->execute([ |
|
52
|
|
|
'--expirationDate' => '2016-01-01', |
|
53
|
|
|
]); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|