Test Failed
Push — master ( 1b7368...050678 )
by Fran
25:16 queued 22:49
created

ConfigTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 69
dl 0
loc 139
rs 10
c 1
b 0
f 0
wmc 9
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\types\helpers\FileHelper;
9
use PSFS\base\types\helpers\GeneratorHelper;
10
11
require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'bootstrap.php';
12
13
/**
14
 * Class DispatcherTest
15
 * @package PSFS\tests\base
16
 */
17
class ConfigTest extends TestCase
18
{
19
20
    const CONFIG_BACKUP_PATH = CONFIG_DIR . DIRECTORY_SEPARATOR . 'config.json.bak';
21
22
    /**
23
     * Creates an instance of Config
24
     * @return Config
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
     */
58
    public function getBasicConfigUse()
59
    {
60
        $config = $this->getInstance();
61
        $previusConfigData = $config->dumpConfig();
62
        $config->clearConfig();
63
64
        // Check if config can create the config dir
65
        $dirtmp = uniqid('test', true);
66
        GeneratorHelper::createDir(CONFIG_DIR . DIRECTORY_SEPARATOR . $dirtmp);
67
        $this->assertFileExists(CONFIG_DIR . DIRECTORY_SEPARATOR . $dirtmp, 'Can\'t create test dir');
68
        @rmdir(CONFIG_DIR . DIRECTORY_SEPARATOR . $dirtmp);
69
70
        // Check if platform is configured
71
        $this->assertTrue(is_bool($config->getDebugMode()));
72
73
        // Check path getters
74
        $this->assertFileExists(GeneratorHelper::getTemplatePath());
75
76
        Config::save([], [
77
            'label' => ['test'],
78
            'value' => [true]
79
        ]);
80
81
        $configData = $config->dumpConfig();
82
        $this->assertNotEmpty($configData, 'Empty configuration');
83
        $this->assertTrue(is_array($configData), 'Configuration is not an array');
84
85
        $configured = $config->isConfigured();
86
        $this->assertTrue(is_bool($configured) && false === $configured);
87
        $this->assertTrue(is_bool($config->checkTryToSaveConfig()));
88
89
        $this->simulateRequiredConfig();
90
        $configured = $config->isConfigured();
91
        $this->assertTrue(is_bool($configured) && true === $configured);
92
93
        return $previusConfigData;
94
    }
95
96
    /**
97
     * @covers
98
     * @return void
99
     */
100
    public function testConfigFileFunctions()
101
    {
102
        $config = $this->getInstance();
103
104
        // Original config data
105
        $original_data = $this->getBasicConfigUse();
106
107
        Config::save($original_data, []);
108
109
        $this->assertEquals($original_data, $config->dumpConfig(), 'Missmatch configurations');
110
111
        Config::save($original_data, [
112
            'label' => [uniqid('t', true)],
113
            'value' => [microtime(true)],
114
        ]);
115
116
        $this->assertNotEquals($original_data, $config->dumpConfig(), 'The same configuration file');
117
118
        Config::save($original_data, []);
119
        $this->restoreConfig();
120
    }
121
122
    /**
123
     * @covers
124
     * @return void
125
     */
126
    public function testMultipleModuleConfig()
127
    {
128
        Config::dropInstance();
129
        $config = $this->getInstance();
130
131
        // Original config data
132
        $original_data = $config->dumpConfig();
133
        $test_data = microtime(true);
134
        Config::save($original_data, [
135
            'label' => ['test'],
136
            'value' => [$test_data],
137
        ]);
138
139
        $this->assertEquals(Config::getParam('test'), $test_data, 'The value is not the same');
140
        $this->assertEquals(Config::getParam('test' . uniqid('t', true), $test_data), $test_data, 'The value is not the same with default value');
141
        $this->assertEquals(Config::getParam('test', null, 'test'), $test_data, 'The value is not the same without module value');
142
143
        $test_data2 = microtime(true);
144
        $original_data = $config->dumpConfig();
145
        Config::save($original_data, [
146
            'label' => ['test.test'],
147
            'value' => [$test_data2],
148
        ]);
149
        $this->assertEquals(Config::getParam('test'), $test_data, 'The value is not the same');
150
        $this->assertEquals(Config::getParam('test' . uniqid('t', true), $test_data), $test_data, 'The value is not the same with default value');
151
        $this->assertEquals(Config::getParam('test', null, 'test'), $test_data2, 'The value is not the same with module value');
152
        $this->assertEquals(Config::getParam('test', null, 'testa'), $test_data, 'The value is not the same with module value and default value');
153
        $this->assertEquals(Config::getParam('test', $test_data2, 'testa'), $test_data, 'The value is not the same with module value and default value');
154
155
        $this->restoreConfig();
156
    }
157
}
158