InstallerControllerTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 72
dl 0
loc 141
rs 10
c 1
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A runTroughStep3() 0 16 1
A runTroughStep2() 0 20 1
A testNoStepActionAction() 0 10 1
A setUp() 0 5 1
A runTroughStep5() 0 20 1
A testInstallationProcess() 0 28 1
A runTroughStep4() 0 17 1
A onNotSuccessfulTest() 0 6 1
1
<?php
2
3
namespace ForkCMS\Bundle\InstallerBundle\Tests\Controller;
4
5
use Common\WebTestCase;
6
use Symfony\Bundle\FrameworkBundle\Client;
7
use Symfony\Component\Filesystem\Filesystem;
8
use Symfony\Component\HttpFoundation\Response;
9
use Throwable;
10
11
/**
12
 * @group installer
13
 */
14
class InstallerControllerTest extends WebTestCase
15
{
16
    /** @var string */
17
    private $kernelDir;
18
19
    protected function setUp(): void
20
    {
21
        parent::setUp();
22
23
        $this->kernelDir = $this->getProvidedData()[0]->getContainer()->getParameter('kernel.project_dir') . '/app';
24
    }
25
26
    protected function onNotSuccessfulTest(Throwable $t): void
27
    {
28
        // put back our parameters file
29
        $this->putParametersFileBack(new Filesystem(), $this->kernelDir);
30
31
        parent::onNotSuccessfulTest($t);
32
    }
33
34
    public function testNoStepActionAction(): void
35
    {
36
        $client = static::createClient(['environment' => 'test_install']);
37
38
        $client->request('GET', '/install');
39
        $client->followRedirect();
40
41
        // we should be redirected to the first step
42
        self::assertEquals(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
43
        self::assertCurrentUrlEndsWith($client, '/install/1');
44
    }
45
46
    public function testInstallationProcess(Client $client): void
47
    {
48
        $container = $client->getContainer();
49
        $filesystem = new Filesystem();
50
        $installDatabaseConfig = [
51
            'install_database[databaseHostname]' => $container->getParameter('database.host'),
52
            'install_database[databasePort]' => $container->getParameter('database.port'),
53
            'install_database[databaseName]' => $container->getParameter('database.name') . '_test',
54
            'install_database[databaseUsername]' => $container->getParameter('database.user'),
55
            'install_database[databasePassword]' => $container->getParameter('database.password'),
56
        ];
57
58
        // make sure we have a clean slate and our parameters file is backed up
59
        $this->emptyTestDatabase($container->get('database'));
0 ignored issues
show
Bug introduced by
It seems like $container->get('database') can also be of type null; however, parameter $database of Common\WebTestCase::emptyTestDatabase() does only seem to accept SpoonDatabase, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
        $this->emptyTestDatabase(/** @scrutinizer ignore-type */ $container->get('database'));
Loading history...
60
61
        // recreate the client with the empty database because we need this in our installer checks
62
63
        $this->backupParametersFile($filesystem, $this->kernelDir);
64
        $client = static::createClient(['environment' => 'test_install']);
65
66
        self::assertGetsRedirected($client, '/install', '/install/2');
67
        $this->runTroughStep2($client);
68
        $this->runTroughStep3($client);
69
        $this->runTroughStep4($client, $installDatabaseConfig);
70
        $this->runTroughStep5($client);
71
72
        // put back our parameters file
73
        $this->putParametersFileBack($filesystem, $this->kernelDir);
74
    }
75
76
    private function runTroughStep2(Client $client): void
77
    {
78
        self::assertCurrentUrlEndsWith($client, '/install/2');
79
80
        $form = $this->getFormForSubmitButton($client, 'Next');
81
        $form['install_languages[languages][0]']->tick();
0 ignored issues
show
Bug introduced by
The method tick() does not exist on Symfony\Component\DomCrawler\Field\FormField. It seems like you code against a sub-type of Symfony\Component\DomCrawler\Field\FormField such as Symfony\Component\DomCrawler\Field\ChoiceFormField. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
        $form['install_languages[languages][0]']->/** @scrutinizer ignore-call */ 
82
                                                  tick();
Loading history...
82
        $form['install_languages[languages][1]']->tick();
83
        $form['install_languages[languages][2]']->tick();
84
        $this->submitForm(
85
            $client,
86
            $form,
87
            [
88
                'install_languages[language_type]' => 'multiple',
89
                'install_languages[default_language]' => 'en',
90
            ]
91
        );
92
93
        // we should be redirected to step 3
94
        self::assertIs200($client);
95
        self::assertCurrentUrlEndsWith($client, '/install/3');
96
    }
97
98
    private function runTroughStep3(Client $client): void
99
    {
100
        $form = $this->getFormForSubmitButton($client, 'Next');
101
        $form['install_modules[modules][0]']->tick();
102
        $form['install_modules[modules][1]']->tick();
103
        $form['install_modules[modules][2]']->tick();
104
        $form['install_modules[modules][3]']->tick();
105
        $form['install_modules[modules][4]']->tick();
106
        $form['install_modules[modules][5]']->tick();
107
        $form['install_modules[modules][6]']->tick();
108
        $form['install_modules[modules][7]']->tick();
109
        $this->submitForm($client, $form);
110
111
        // we should be redirected to step 4
112
        self::assertIs200($client);
113
        self::assertCurrentUrlEndsWith($client, '/install/4');
114
    }
115
116
    private function runTroughStep4(Client $client, array $installDatabaseConfig): void
117
    {
118
        // first submit with incorrect data
119
        $form = $this->getFormForSubmitButton($client, 'Next');
120
        $this->submitForm($client, $form);
121
        self::assertGreaterThan(
122
            0,
123
            $client->getCrawler()->filter('div.errorMessage:contains("Problem with database credentials")')->count()
124
        );
125
126
        // submit with correct database credentials
127
        $form = $this->getFormForSubmitButton($client, 'Next');
128
        $this->submitForm($client, $form, $installDatabaseConfig, true);
129
130
        // we should be redirected to step 5
131
        self::assertIs200($client);
132
        self::assertCurrentUrlEndsWith($client, '/install/5');
133
    }
134
135
    private function runTroughStep5(Client $client): void
136
    {
137
        $form = $this->getFormForSubmitButton($client, 'Finish installation');
138
        $this->submitForm(
139
            $client,
140
            $form,
141
            [
142
                'install_login[email]' => '[email protected]',
143
                'install_login[password][first]' => 'password',
144
                'install_login[password][second]' => 'password',
145
            ],
146
            true
147
        );
148
149
        // we should be redirected to step 6
150
        self::assertIs200($client);
151
        self::assertCurrentUrlEndsWith($client, '/install/6');
152
        self::assertGreaterThan(
153
            0,
154
            $client->getCrawler()->filter('h2:contains("Installation complete")')->count()
155
        );
156
    }
157
}
158