forked from shlinkio/shlink-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetOptionCommandTest.php
More file actions
102 lines (87 loc) · 3.95 KB
/
SetOptionCommandTest.php
File metadata and controls
102 lines (87 loc) · 3.95 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
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Installer\Command;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Installer\Command\SetOptionCommand;
use Shlinkio\Shlink\Installer\Config\ConfigOptionsManagerInterface;
use Shlinkio\Shlink\Installer\Config\Option\ConfigOptionInterface;
use Shlinkio\Shlink\Installer\Exception\InvalidShlinkPathException;
use Shlinkio\Shlink\Installer\Service\ShlinkAssetsHandlerInterface;
use Shlinkio\Shlink\Installer\Util\ConfigWriterInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Filesystem\Filesystem;
use function chdir;
use function getcwd;
class SetOptionCommandTest extends TestCase
{
private CommandTester $commandTester;
private MockObject & ConfigWriterInterface $configWriter;
private MockObject & ShlinkAssetsHandlerInterface $assetsHandler;
private MockObject & ConfigOptionsManagerInterface $optionsManager;
private MockObject & Filesystem $filesystem;
private string $initialCwd;
public function setUp(): void
{
$this->initialCwd = getcwd() ?: '';
chdir(__DIR__ . '/../../test-resources');
$this->configWriter = $this->createMock(ConfigWriterInterface::class);
$this->assetsHandler = $this->createMock(ShlinkAssetsHandlerInterface::class);
$this->optionsManager = $this->createMock(ConfigOptionsManagerInterface::class);
$this->filesystem = $this->createMock(Filesystem::class);
$app = new Application();
$command = new SetOptionCommand(
$this->configWriter,
$this->assetsHandler,
$this->optionsManager,
$this->filesystem,
['foo' => [
'Set option 1' => 'option_1',
'Set option 2' => 'option_2',
'Set option 3' => 'option_3',
]],
['option_1', 'option_3'],
);
$app->addCommand($command);
$this->commandTester = new CommandTester($command);
}
protected function tearDown(): void
{
chdir($this->initialCwd);
}
#[Test]
public function exceptionIsThrownWhenGeneratedConfigFileDoesNotExist(): void
{
$this->filesystem->expects($this->once())->method('exists')->with($this->isString())->willReturn(false);
$this->configWriter->expects($this->never())->method('toFile');
$this->assetsHandler->expects($this->never())->method('dropCachedConfigIfAny');
$this->optionsManager->expects($this->never())->method('get');
$this->expectException(InvalidShlinkPathException::class);
$this->commandTester->execute([]);
}
#[Test]
public function expectedOptionsAreOfferedBasedOnConfig(): void
{
$this->filesystem->expects($this->once())->method('exists')->with($this->isString())->willReturn(true);
$this->configWriter->expects($this->once())->method('toFile');
$this->assetsHandler->expects($this->once())->method('dropCachedConfigIfAny')->with(
$this->isInstanceOf(SymfonyStyle::class),
);
$plugin = $this->createMock(ConfigOptionInterface::class);
$plugin->expects($this->once())->method('ask')->willReturn('');
$plugin->expects($this->once())->method('getEnvVar')->willReturn('foo');
$this->optionsManager->expects($this->once())->method('get')->with($this->isString())->willReturn(
$plugin,
);
$this->commandTester->setInputs(['1']);
$this->commandTester->execute([]);
$output = $this->commandTester->getDisplay();
self::assertEquals(0, $this->commandTester->getStatusCode());
self::assertStringContainsString('Set option 1', $output);
self::assertStringContainsString('Set option 3', $output);
self::assertStringNotContainsString('Set option 2', $output);
}
}