Passed
Push — master ( 283335...4a2fc2 )
by Fran
18:22 queued 08:26
created

ConfigTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 74
dl 0
loc 151
rs 10
c 0
b 0
f 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testMultipleModuleConfig() 0 30 1
A getInstance() 0 8 1
A testConfigFileFunctions() 0 20 1
A testCleaningConfigFiles() 0 8 3
A simulateRequiredConfig() 0 9 2
A restoreConfig() 0 5 1
A getBasicConfigUse() 0 36 3
1
<?php
2
3
namespace PSFS\tests\base;
4
5
use PHPUnit\Framework\TestCase;
6
use PSFS\base\Cache;
7
use PSFS\base\config\Config;
8
use PSFS\base\exception\GeneratorException;
9
use PSFS\base\types\helpers\FileHelper;
10
use PSFS\base\types\helpers\GeneratorHelper;
11
12
/**
13
 * Class DispatcherTest
14
 * @package PSFS\tests\base
15
 */
16
class ConfigTest extends TestCase
17
{
18
19
    const CONFIG_BACKUP_PATH = CONFIG_DIR . DIRECTORY_SEPARATOR . 'config.json.bak';
20
21
    /**
22
     * Creates an instance of Config
23
     * @return Config
24
     * @throws GeneratorException
25
     */
26
    private function getInstance()
27
    {
28
        $config = Config::getInstance();
29
30
        $this->assertNotNull($config, 'Instance not created');
31
        $this->assertInstanceOf(Config::class, $config, 'Instance different than expected');
32
        Cache::getInstance()->storeData(self::CONFIG_BACKUP_PATH, $config->dumpConfig(), Cache::JSON, true);
33
        return $config;
34
    }
35
36
    private function restoreConfig()
37
    {
38
        $config = Cache::getInstance()->getDataFromFile(self::CONFIG_BACKUP_PATH, Cache::JSON, true);
39
        Config::save($config, []);
40
        FileHelper::deleteDir(self::CONFIG_BACKUP_PATH);
41
    }
42
43
    private function simulateRequiredConfig()
44
    {
45
        $config = Config::getInstance();
46
        $data = [];
47
        foreach (Config::$required as $key) {
48
            $data[$key] = uniqid('test', true);
49
        }
50
        Config::save($data, []);
51
        $config->loadConfigData();
52
    }
53
54
    /**
55
     * Test that checks basic functionality
56
     * @return array
57
     * @throws GeneratorException
58
     */
59
    public function getBasicConfigUse()
60
    {
61
        $config = $this->getInstance();
62
        $previousConfigData = $config->dumpConfig();
63
        $config->clearConfig();
64
65
        // Check if config can create the config dir
66
        $dirtmp = uniqid('test', true);
67
        GeneratorHelper::createDir(CONFIG_DIR . DIRECTORY_SEPARATOR . $dirtmp);
68
        $this->assertFileExists(CONFIG_DIR . DIRECTORY_SEPARATOR . $dirtmp, 'Can\'t create test dir');
69
        @rmdir(CONFIG_DIR . DIRECTORY_SEPARATOR . $dirtmp);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for rmdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

69
        /** @scrutinizer ignore-unhandled */ @rmdir(CONFIG_DIR . DIRECTORY_SEPARATOR . $dirtmp);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
70
71
        // Check if platform is configured
72
        $this->assertTrue(is_bool($config->getDebugMode()));
73
74
        // Check path getters
75
        $this->assertFileExists(GeneratorHelper::getTemplatePath());
76
77
        Config::save([], [
78
            'label' => ['test'],
79
            'value' => [true]
80
        ]);
81
82
        $configData = $config->dumpConfig();
83
        $this->assertNotEmpty($configData, 'Empty configuration');
84
        $this->assertTrue(is_array($configData), 'Configuration is not an array');
85
86
        $configured = $config->isConfigured();
87
        $this->assertTrue(is_bool($configured) && false === $configured);
88
        $this->assertTrue(is_bool($config->checkTryToSaveConfig()));
89
90
        $this->simulateRequiredConfig();
91
        $configured = $config->isConfigured();
92
        $this->assertTrue(is_bool($configured) && true === $configured);
93
94
        return $previousConfigData;
95
    }
96
97
    /**
98
     * @return void
99
     * @throws GeneratorException
100
     */
101
    public function testConfigFileFunctions()
102
    {
103
        $config = $this->getInstance();
104
105
        // Original config data
106
        $original_data = $this->getBasicConfigUse();
107
108
        Config::save($original_data, []);
109
110
        $this->assertEquals($original_data, $config->dumpConfig(), 'Missmatch configurations');
111
112
        Config::save($original_data, [
113
            'label' => [uniqid('t', true)],
114
            'value' => [microtime(true)],
115
        ]);
116
117
        $this->assertNotEquals($original_data, $config->dumpConfig(), 'The same configuration file');
118
119
        Config::save($original_data, []);
120
        $this->restoreConfig();
121
    }
122
123
    /**
124
     * @return void
125
     * @throws GeneratorException
126
     */
127
    public function testMultipleModuleConfig()
128
    {
129
        Config::dropInstance();
130
        $config = $this->getInstance();
131
132
        // Original config data
133
        $original_data = $config->dumpConfig();
134
        $test_data = microtime(true);
135
        Config::save($original_data, [
136
            'label' => ['test'],
137
            'value' => [$test_data],
138
        ]);
139
140
        $this->assertEquals(Config::getParam('test'), $test_data, 'The value is not the same');
141
        $this->assertEquals(Config::getParam('test' . uniqid('t', true), $test_data), $test_data, 'The value is not the same with default value');
142
        $this->assertEquals(Config::getParam('test', null, 'test'), $test_data, 'The value is not the same without module value');
143
144
        $test_data2 = microtime(true);
145
        $original_data = $config->dumpConfig();
146
        Config::save($original_data, [
147
            'label' => ['test.test'],
148
            'value' => [$test_data2],
149
        ]);
150
        $this->assertEquals(Config::getParam('test'), $test_data, 'The value is not the same');
151
        $this->assertEquals(Config::getParam('test' . uniqid('t', true), $test_data), $test_data, 'The value is not the same with default value');
152
        $this->assertEquals(Config::getParam('test', null, 'test'), $test_data2, 'The value is not the same with module value');
153
        $this->assertEquals(Config::getParam('test', null, 'testa'), $test_data, 'The value is not the same with module value and default value');
154
        $this->assertEquals(Config::getParam('test', $test_data2, 'testa'), $test_data, 'The value is not the same with module value and default value');
155
156
        $this->restoreConfig();
157
    }
158
159
    public function testCleaningConfigFiles()
160
    {
161
        foreach (Config::$cleanable_config_files as $cleanable_config_file) {
162
            $this->assertFileExists(CONFIG_DIR . DIRECTORY_SEPARATOR . $cleanable_config_file);
163
        }
164
        Config::clearConfigFiles();
165
        foreach (Config::$cleanable_config_files as $cleanable_config_file) {
166
            $this->assertFileDoesNotExist(CONFIG_DIR . DIRECTORY_SEPARATOR . $cleanable_config_file);
167
        }
168
    }
169
}
170