Completed
Push — master ( d5dc6c...9a2ca3 )
by Alejandro
11s
created

generatedConfigIsProperlyPersisted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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