Completed
Push — master ( b53e51...1009f9 )
by Alejandro
04:13 queued 38s
created

InstallCommandTest::createInputStream()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
namespace ShlinkioTest\Shlink\CLI\Command\Install;
3
4
use PHPUnit\Framework\TestCase;
5
use Prophecy\Argument;
6
use Prophecy\Prophecy\MethodProphecy;
7
use Prophecy\Prophecy\ObjectProphecy;
8
use Shlinkio\Shlink\CLI\Command\Install\InstallCommand;
9
use Shlinkio\Shlink\CLI\Install\ConfigCustomizerPluginManagerInterface;
10
use Shlinkio\Shlink\CLI\Install\Plugin\ConfigCustomizerPluginInterface;
11
use Symfony\Component\Console\Application;
12
use Symfony\Component\Console\Helper\ProcessHelper;
13
use Symfony\Component\Console\Tester\CommandTester;
14
use Symfony\Component\Filesystem\Exception\IOException;
15
use Symfony\Component\Filesystem\Filesystem;
16
use Symfony\Component\Process\Process;
17
use Zend\Config\Writer\WriterInterface;
18
19
class InstallCommandTest extends TestCase
20
{
21
    /**
22
     * @var InstallCommand
23
     */
24
    protected $command;
25
    /**
26
     * @var CommandTester
27
     */
28
    protected $commandTester;
29
    /**
30
     * @var ObjectProphecy
31
     */
32
    protected $configWriter;
33
    /**
34
     * @var ObjectProphecy
35
     */
36
    protected $filesystem;
37
38
    public function setUp()
39
    {
40
        $processMock = $this->prophesize(Process::class);
41
        $processMock->isSuccessful()->willReturn(true);
42
        $processHelper = $this->prophesize(ProcessHelper::class);
43
        $processHelper->getName()->willReturn('process');
44
        $processHelper->setHelperSet(Argument::any())->willReturn(null);
45
        $processHelper->run(Argument::cetera())->willReturn($processMock->reveal());
46
47
        $this->filesystem = $this->prophesize(Filesystem::class);
48
        $this->filesystem->exists(Argument::cetera())->willReturn(false);
49
50
        $this->configWriter = $this->prophesize(WriterInterface::class);
51
52
        $configCustomizer = $this->prophesize(ConfigCustomizerPluginInterface::class);
53
        $configCustomizers = $this->prophesize(ConfigCustomizerPluginManagerInterface::class);
54
        $configCustomizers->get(Argument::cetera())->willReturn($configCustomizer->reveal());
55
56
        $app = new Application();
57
        $helperSet = $app->getHelperSet();
58
        $helperSet->set($processHelper->reveal());
59
        $app->setHelperSet($helperSet);
60
        $this->command = new InstallCommand(
61
            $this->configWriter->reveal(),
62
            $this->filesystem->reveal(),
63
            $configCustomizers->reveal()
64
        );
65
        $app->add($this->command);
66
67
        $this->commandTester = new CommandTester($this->command);
68
    }
69
70
    /**
71
     * @test
72
     */
73
    public function generatedConfigIsProperlyPersisted()
74
    {
75
        $this->configWriter->toFile(Argument::any(), Argument::type('array'), false)->shouldBeCalledTimes(1);
76
        $this->commandTester->execute([]);
77
    }
78
79
    /**
80
     * @test
81
     */
82
    public function cachedConfigIsDeletedIfExists()
83
    {
84
        /** @var MethodProphecy $appConfigExists */
85
        $appConfigExists = $this->filesystem->exists('data/cache/app_config.php')->willReturn(true);
86
        /** @var MethodProphecy $appConfigRemove */
87
        $appConfigRemove = $this->filesystem->remove('data/cache/app_config.php')->willReturn(null);
88
89
        $this->commandTester->execute([]);
90
91
        $appConfigExists->shouldHaveBeenCalledTimes(1);
92
        $appConfigRemove->shouldHaveBeenCalledTimes(1);
93
    }
94
95
    /**
96
     * @test
97
     */
98
    public function exceptionWhileDeletingCachedConfigCancelsProcess()
99
    {
100
        /** @var MethodProphecy $appConfigExists */
101
        $appConfigExists = $this->filesystem->exists('data/cache/app_config.php')->willReturn(true);
102
        /** @var MethodProphecy $appConfigRemove */
103
        $appConfigRemove = $this->filesystem->remove('data/cache/app_config.php')->willThrow(IOException::class);
104
        /** @var MethodProphecy $configToFile */
105
        $configToFile = $this->configWriter->toFile(Argument::cetera())->willReturn(true);
106
107
        $this->commandTester->execute([]);
108
109
        $appConfigExists->shouldHaveBeenCalledTimes(1);
110
        $appConfigRemove->shouldHaveBeenCalledTimes(1);
111
        $configToFile->shouldNotHaveBeenCalled();
112
    }
113
114
    /**
115
     * @test
116
     */
117
    public function whenCommandIsUpdatePreviousConfigCanBeImported()
118
    {
119
        $ref = new \ReflectionObject($this->command);
120
        $prop = $ref->getProperty('isUpdate');
121
        $prop->setAccessible(true);
122
        $prop->setValue($this->command, true);
123
124
        /** @var MethodProphecy $importedConfigExists */
125
        $importedConfigExists = $this->filesystem->exists(
126
            __DIR__ . '/../../../test-resources/' . InstallCommand::GENERATED_CONFIG_PATH
127
        )->willReturn(true);
128
129
        $this->commandTester->setInputs([
130
            '',
131
            '/foo/bar/wrong_previous_shlink',
132
            '',
133
            __DIR__ . '/../../../test-resources',
134
        ]);
135
        $this->commandTester->execute([]);
136
137
        $importedConfigExists->shouldHaveBeenCalled();
138
    }
139
}
140