Completed
Push — master ( 3c1969...5b5792 )
by Schlaefer
03:16 queued 10s
created

InstallerControllerTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
dl 0
loc 106
rs 10
c 0
b 0
f 0
wmc 9
lcom 2
cbo 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A tearDown() 0 7 1
A testIndex() 0 6 1
A testWrongInstallerState() 0 15 2
A testMigrateAndData() 0 29 1
A testConnectedDbExists() 0 13 1
A createInstallerToken() 0 4 1
A createSettings() 0 10 1
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Installer\Test\TestCase\Controller;
14
15
use Cake\Core\Configure;
16
use Cake\Datasource\ConnectionManager;
17
use Cake\Filesystem\File;
18
use Cake\ORM\TableRegistry;
19
use Installer\Lib\DbVersion;
20
use Installer\Lib\InstallerState;
21
use Installer\Lib\IntegrationTestCase;
22
23
class InstallerControllerTest extends IntegrationTestCase
24
{
25
    protected $isUpdated = false;
26
27
    public function setUp()
28
    {
29
        parent::setUp();
30
        $this->dropTables();
31
        $this->createInstallerToken();
32
        InstallerState::reset();
33
34
        Configure::write('Saito.installed', false);
35
        Configure::write('Saito.updated', false);
36
    }
37
38
    public function tearDown()
39
    {
40
        $this->createInstallerToken();
41
        $this->dropTables();
42
        InstallerState::reset();
43
        parent::tearDown();
44
    }
45
46
    public function testIndex()
47
    {
48
        $this->get('/');
49
50
        $this->assertRedirect('install/dbconnection');
51
    }
52
53
    public function testWrongInstallerState()
54
    {
55
        $actions = [
56
            'salt',
57
            'connected',
58
            'migrate',
59
            'data',
60
            'finished',
61
        ];
62
63
        foreach ($actions as $action) {
64
            $this->get('install/' . $action);
65
            $this->assertRedirect('/');
66
        }
67
    }
68
69
    public function testMigrateAndData()
70
    {
71
        InstallerState::set('migrate');
72
        $this->post('install/migrate');
73
74
        $email = '[email protected]';
75
        $this->post(
76
            'install/data',
77
            [
78
                'username' => 'admin',
79
                'password' => 'admin',
80
                'password_confirm' => 'admin',
81
                'user_email' => $email
82
            ]
83
        );
84
85
        $this->assertRedirect('install/finished');
86
87
        $Settings = TableRegistry::getTableLocator()->get('Settings');
88
        $this->assertEquals($email, $Settings->findByName('forum_email')->first()->get('value'));
89
90
        $dbVersion = (new DbVersion($Settings))->get();
0 ignored issues
show
Compatibility introduced by
$Settings of type object<Cake\ORM\Table> is not a sub-type of object<App\Model\Table\SettingsTable>. It seems like you assume a child class of the class Cake\ORM\Table to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
91
        $this->assertEquals(Configure::read('Saito.v'), $dbVersion);
92
93
        $Users = TableRegistry::getTableLocator()->get('Users');
94
        $admin = $Users->get(1);
95
        $this->assertTextContains('$2y$', $admin->get('password'));
96
        $this->assertEquals($email, $admin->get('user_email'));
97
    }
98
99
    public function testConnectedDbExists()
100
    {
101
        InstallerState::set('connected');
102
        $this->createSettings();
103
        (new DbVersion(TableRegistry::get('Settings')))->set('4.10.0');
0 ignored issues
show
Compatibility introduced by
\Cake\ORM\TableRegistry::get('Settings') of type object<Cake\ORM\Table> is not a sub-type of object<App\Model\Table\SettingsTable>. It seems like you assume a child class of the class Cake\ORM\Table to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Deprecated Code introduced by
The method Cake\ORM\TableRegistry::get() has been deprecated with message: 3.6.0 Use \Cake\ORM\Locator\TableLocator::get() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
104
105
        $token = new File(CONFIG . 'installer');
106
        $this->assertTrue($token->exists());
107
108
        $this->get('install/connected');
109
110
        $this->assertResponseCode(200);
111
    }
112
113
    private function createInstallerToken()
114
    {
115
        (new File(CONFIG . 'installer'))->create();
116
    }
117
118
    private function createSettings()
119
    {
120
        $connection = ConnectionManager::get('test');
121
        $connection->execute('DROP TABLE IF EXISTS `settings`;');
122
        $connection->execute('CREATE TABLE `settings` (id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT);');
123
        $connection->execute('ALTER TABLE settings ADD `name` VARCHAR(100)  NULL  DEFAULT NULL  AFTER `id`;');
124
        $connection->execute('ALTER TABLE settings ADD `value` VARCHAR(100)  NULL  DEFAULT NULL  AFTER `id`;');
125
        $connection->execute("INSERT INTO `settings` (`id`, `name`, `value`) VALUES ('1', 'db_version', NULL);");
126
        $connection->execute("INSERT INTO `settings` (`id`, `name`, `value`) VALUES ('2', 'forum_email', NULL);");
127
    }
128
}
129