Completed
Pull Request — master (#224)
by Alejandro
03:14
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 onlyMissingOptionsAreAsked()
66
    {
67
        $choice = $this->io->choice(Argument::cetera())->willReturn('MySQL');
68
        $ask = $this->io->ask(Argument::cetera())->willReturn('asked');
69
70
        $config = new CustomizableAppConfig();
71
        $config->setDatabase([
72
            'DRIVER' => 'pdo_pgsql',
73
            'NAME' => 'foo',
74
            'PASSWORD' => 'foo',
75
        ]);
76
77
        $this->plugin->process($this->io->reveal(), $config);
78
79
        $this->assertEquals([
80
            'DRIVER' => 'pdo_pgsql',
81
            'NAME' => 'foo',
82
            'USER' => 'asked',
83
            'PASSWORD' => 'foo',
84
            'HOST' => 'asked',
85
            'PORT' => 'asked',
86
        ], $config->getDatabase());
87
        $choice->shouldNotHaveBeenCalled();
88
        $ask->shouldHaveBeenCalledTimes(3);
89
    }
90
91
    /**
92
     * @test
93
     */
94
    public function noQuestionsAskedIfImportedConfigContainsEverything()
95
    {
96
        $choice = $this->io->choice(Argument::cetera())->willReturn('MySQL');
97
        $ask = $this->io->ask(Argument::cetera())->willReturn('asked');
98
99
        $config = new CustomizableAppConfig();
100
        $config->setDatabase([
101
            'DRIVER' => 'pdo_pgsql',
102
            'NAME' => 'foo',
103
            'USER' => 'foo',
104
            'PASSWORD' => 'foo',
105
            'HOST' => 'foo',
106
            'PORT' => 'foo',
107
        ]);
108
109
        $this->plugin->process($this->io->reveal(), $config);
110
111
        $this->assertEquals([
112
            'DRIVER' => 'pdo_pgsql',
113
            'NAME' => 'foo',
114
            'USER' => 'foo',
115
            'PASSWORD' => 'foo',
116
            'HOST' => 'foo',
117
            'PORT' => 'foo',
118
        ], $config->getDatabase());
119
        $choice->shouldNotHaveBeenCalled();
120
        $ask->shouldNotHaveBeenCalled();
121
    }
122
123
    /**
124
     * @test
125
     */
126
    public function sqliteDatabaseIsImportedWhenRequested()
127
    {
128
        $copy = $this->filesystem->copy(Argument::cetera())->willReturn(null);
129
130
        $config = new CustomizableAppConfig();
131
        $config->setDatabase([
132
            'DRIVER' => 'pdo_sqlite',
133
        ]);
134
135
        $this->plugin->process($this->io->reveal(), $config);
136
137
        $this->assertEquals([
138
            'DRIVER' => 'pdo_sqlite',
139
        ], $config->getDatabase());
140
        $copy->shouldHaveBeenCalledTimes(1);
141
    }
142
}
143