forked from shlinkio/shlink-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitCommandTest.php
More file actions
117 lines (102 loc) · 4.44 KB
/
InitCommandTest.php
File metadata and controls
117 lines (102 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Installer\Command;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Installer\Command\InitCommand;
use Shlinkio\Shlink\Installer\Service\InstallationCommandsRunnerInterface;
use Shlinkio\Shlink\Installer\Util\InstallationCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use function count;
class InitCommandTest extends TestCase
{
private CommandTester $tester;
private MockObject & InstallationCommandsRunnerInterface $commandsRunner;
public function setUp(): void
{
$this->commandsRunner = $this->createMock(InstallationCommandsRunnerInterface::class);
$app = new Application();
$command = new InitCommand($this->commandsRunner);
$app->addCommand($command);
$this->tester = new CommandTester($command);
}
#[Test, DataProvider('provideInputs')]
public function expectedCommandsAreRunBasedOnInput(array $input, array $commands, bool $interactive): void
{
$this->commandsRunner->expects($this->exactly(count($commands)))->method('execPhpCommand')->willReturnCallback(
function (string $commandName, $_, bool $isInteractive, array $args) use ($commands, $interactive): bool {
Assert::assertContains($commandName, $commands);
Assert::assertEquals($interactive, $isInteractive);
Assert::assertEquals($commandName === InstallationCommand::API_KEY_CREATE->value ? ['foo'] : [], $args);
return true;
},
);
$this->tester->execute($input, ['interactive' => $interactive]);
}
public static function provideInputs(): iterable
{
yield 'default' => [[], [
InstallationCommand::DB_CREATE_SCHEMA->value,
InstallationCommand::DB_MIGRATE->value,
InstallationCommand::ORM_PROXIES->value,
InstallationCommand::GEOLITE_DOWNLOAD_DB->value,
], true];
yield 'non-interactive' => [[], [
InstallationCommand::DB_CREATE_SCHEMA->value,
InstallationCommand::DB_MIGRATE->value,
InstallationCommand::ORM_PROXIES->value,
InstallationCommand::GEOLITE_DOWNLOAD_DB->value,
], false];
yield 'skips' => [['--skip-initialize-db' => true, '--skip-download-geolite' => true], [
InstallationCommand::DB_MIGRATE->value,
InstallationCommand::ORM_PROXIES->value,
], true];
yield 'all' => [[
'--clear-db-cache' => true,
'--initial-api-key' => null,
'--download-rr-binary' => true,
], [
InstallationCommand::DB_CREATE_SCHEMA->value,
InstallationCommand::DB_MIGRATE->value,
InstallationCommand::ORM_PROXIES->value,
InstallationCommand::ORM_CLEAR_CACHE->value,
InstallationCommand::GEOLITE_DOWNLOAD_DB->value,
InstallationCommand::API_KEY_GENERATE->value,
InstallationCommand::ROAD_RUNNER_BINARY_DOWNLOAD->value,
], true];
yield 'mixed' => [[
'--initial-api-key' => null,
'--skip-download-geolite' => true,
], [
InstallationCommand::DB_CREATE_SCHEMA->value,
InstallationCommand::DB_MIGRATE->value,
InstallationCommand::ORM_PROXIES->value,
InstallationCommand::API_KEY_GENERATE->value,
], true];
yield 'api key value' => [[
'--initial-api-key' => 'foo',
], [
InstallationCommand::DB_CREATE_SCHEMA->value,
InstallationCommand::DB_MIGRATE->value,
InstallationCommand::ORM_PROXIES->value,
InstallationCommand::API_KEY_CREATE->value,
InstallationCommand::GEOLITE_DOWNLOAD_DB->value,
], true];
}
#[Test, DataProvider('provideExitCodes')]
public function properExitCodeIsReturnedBasedOnCommandsExecution(bool $result, int $expectedExitCode): void
{
$this->commandsRunner->method('execPhpCommand')->willReturn($result);
$exitCode = $this->tester->execute([]);
self::assertEquals($expectedExitCode, $exitCode);
}
public static function provideExitCodes(): iterable
{
yield 'success' => [true, 0];
yield 'error' => [false, -1];
}
}