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

configIsRequestedToTheUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Installer\Config\Plugin;
5
6
use PHPUnit\Framework\TestCase;
7
use Prophecy\Argument;
8
use Prophecy\Prophecy\ObjectProphecy;
9
use Shlinkio\Shlink\Installer\Config\Plugin\DatabaseConfigCustomizer;
10
use Shlinkio\Shlink\Installer\Model\CustomizableAppConfig;
11
use Symfony\Component\Console\Style\SymfonyStyle;
12
use Symfony\Component\Filesystem\Filesystem;
13
14
class DatabaseConfigCustomizerTest extends TestCase
15
{
16
    /**
17
     * @var DatabaseConfigCustomizer
18
     */
19
    private $plugin;
20
    /**
21
     * @var ObjectProphecy
22
     */
23
    private $io;
24
    /**
25
     * @var ObjectProphecy
26
     */
27
    private $filesystem;
28
29
    public function setUp()
30
    {
31
        $this->io = $this->prophesize(SymfonyStyle::class);
32
        $this->io->title(Argument::any())->willReturn(null);
33
        $this->filesystem = $this->prophesize(Filesystem::class);
34
35
        $this->plugin = new DatabaseConfigCustomizer($this->filesystem->reveal());
36
    }
37
38
    /**
39
     * @test
40
     */
41
    public function configIsRequestedToTheUser()
42
    {
43
        $choice = $this->io->choice(Argument::cetera())->willReturn('MySQL');
44
        $ask = $this->io->ask(Argument::cetera())->willReturn('param');
45
        $config = new CustomizableAppConfig();
46
47
        $this->plugin->process($this->io->reveal(), $config);
48
49
        $this->assertTrue($config->hasDatabase());
50
        $this->assertEquals([
51
            'DRIVER' => 'pdo_mysql',
52
            'NAME' => 'param',
53
            'USER' => 'param',
54
            'PASSWORD' => 'param',
55
            'HOST' => 'param',
56
            'PORT' => 'param',
57
        ], $config->getDatabase());
58
        $choice->shouldHaveBeenCalledTimes(1);
59
        $ask->shouldHaveBeenCalledTimes(5);
60
    }
61
62
    /**
63
     * @test
64
     */
65
    public function overwriteIsRequestedIfValueIsAlreadySet()
66
    {
67
        $choice = $this->io->choice(Argument::cetera())->willReturn('MySQL');
68
        $confirm = $this->io->confirm(Argument::cetera())->willReturn(false);
69
        $ask = $this->io->ask(Argument::cetera())->willReturn('MySQL');
70
        $config = new CustomizableAppConfig();
71
        $config->setDatabase([
72
            'DRIVER' => 'pdo_pgsql',
73
            'NAME' => 'MySQL',
74
            'USER' => 'MySQL',
75
            'PASSWORD' => 'MySQL',
76
            'HOST' => 'MySQL',
77
            'PORT' => 'MySQL',
78
        ]);
79
80
        $this->plugin->process($this->io->reveal(), $config);
81
82
        $this->assertEquals([
83
            'DRIVER' => 'pdo_mysql',
84
            'NAME' => 'MySQL',
85
            'USER' => 'MySQL',
86
            'PASSWORD' => 'MySQL',
87
            'HOST' => 'MySQL',
88
            'PORT' => 'MySQL',
89
        ], $config->getDatabase());
90
        $confirm->shouldHaveBeenCalledTimes(1);
91
        $choice->shouldHaveBeenCalledTimes(1);
92
        $ask->shouldHaveBeenCalledTimes(5);
93
    }
94
95
    /**
96
     * @test
97
     */
98
    public function existingValueIsKeptIfRequested()
99
    {
100
        $confirm = $this->io->confirm(Argument::cetera())->willReturn(true);
101
102
        $config = new CustomizableAppConfig();
103
        $config->setDatabase([
104
            'DRIVER' => 'pdo_pgsql',
105
            'NAME' => 'MySQL',
106
            'USER' => 'MySQL',
107
            'PASSWORD' => 'MySQL',
108
            'HOST' => 'MySQL',
109
            'PORT' => 'MySQL',
110
        ]);
111
112
        $this->plugin->process($this->io->reveal(), $config);
113
114
        $this->assertEquals([
115
            'DRIVER' => 'pdo_pgsql',
116
            'NAME' => 'MySQL',
117
            'USER' => 'MySQL',
118
            'PASSWORD' => 'MySQL',
119
            'HOST' => 'MySQL',
120
            'PORT' => 'MySQL',
121
        ], $config->getDatabase());
122
        $confirm->shouldHaveBeenCalledTimes(1);
123
    }
124
125
    /**
126
     * @test
127
     */
128
    public function sqliteDatabaseIsImportedWhenRequested()
129
    {
130
        $confirm = $this->io->confirm(Argument::cetera())->willReturn(true);
131
        $copy = $this->filesystem->copy(Argument::cetera())->willReturn(null);
132
133
        $config = new CustomizableAppConfig();
134
        $config->setDatabase([
135
            'DRIVER' => 'pdo_sqlite',
136
        ]);
137
138
        $this->plugin->process($this->io->reveal(), $config);
139
140
        $this->assertEquals([
141
            'DRIVER' => 'pdo_sqlite',
142
        ], $config->getDatabase());
143
        $confirm->shouldHaveBeenCalledTimes(1);
144
        $copy->shouldHaveBeenCalledTimes(1);
145
    }
146
}
147