Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TestConfig.php « Mock « Framework « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 06e836c8640cb280984430498cf8121119b0b588 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Tests\Framework\Mock;

use Piwik\Application\Kernel\GlobalSettingsProvider;
use Piwik\Config;
use Piwik\Tests\Framework\TestingEnvironmentVariables;

class TestConfig extends Config
{
    private $allowSave = false;
    private $doSetTestEnvironment = false;

    public function __construct(GlobalSettingsProvider $provider, TestingEnvironmentVariables $testingEnvironment, $allowSave = false, $doSetTestEnvironment = true)
    {
        parent::__construct($provider);

        $this->allowSave = $allowSave;
        $this->doSetTestEnvironment = $doSetTestEnvironment;

        $this->reload();

        $this->setFromTestEnvironment($testingEnvironment);
    }

    public function reload($pathLocal = null, $pathGlobal = null, $pathCommon = null)
    {
        parent::reload($pathLocal, $pathGlobal, $pathCommon);

        $this->setTestEnvironment();
    }

    public function forceSave()
    {
        if ($this->allowSave) {
            parent::forceSave();
        }
    }

    public function setTestEnvironment()
    {
        if (!$this->allowSave) {
            $this->doNotWriteConfigInTests = true;
        }

        $chain = $this->settings->getIniFileChain();

        $databaseTestsSettings = $chain->get('database_tests'); // has to be __get otherwise when called from TestConfig, PHP will issue a NOTICE
        if (!empty($databaseTestsSettings)) {
            $chain->set('database', $databaseTestsSettings);
        }

        // Ensure local mods do not affect tests
        if (empty($pathGlobal)) {
            $general = $chain->getFrom($this->getLocalPath(), 'General');
            $instanceId = isset($general['instance_id']) ? $general['instance_id'] : null;

            $chain->set('Debug', $chain->getFrom($this->getGlobalPath(), 'Debug'));
            $chain->set('mail', $chain->getFrom($this->getGlobalPath(), 'mail'));

            $globalGeneral = $chain->getFrom($this->getGlobalPath(), 'General');
            if ($instanceId) {
                $globalGeneral['instance_id'] = $instanceId;
            }
            $chain->set('General', $globalGeneral);

            $chain->set('Segments', $chain->getFrom($this->getGlobalPath(), 'Segments'));
            $chain->set('Tracker', $chain->getFrom($this->getGlobalPath(), 'Tracker'));
            $chain->set('Deletelogs', $chain->getFrom($this->getGlobalPath(), 'Deletelogs'));
            $chain->set('Deletereports', $chain->getFrom($this->getGlobalPath(), 'Deletereports'));
            $chain->set('Development', $chain->getFrom($this->getGlobalPath(), 'Development'));
        }

        // for unit tests, we set that no plugin is installed. This will force
        // the test initialization to create the plugins tables, execute ALTER queries, etc.
        $chain->set('PluginsInstalled', array('PluginsInstalled' => array()));
    }

    private function setFromTestEnvironment(\Piwik\Tests\Framework\TestingEnvironmentVariables $testingEnvironment)
    {
        $chain = $this->settings->getIniFileChain();

        $general =& $chain->get('General');
        $log =& $chain->get('log');
        $database =& $chain->get('database');

        if ($testingEnvironment->configFileLocal) {
            $general['session_save_handler'] = 'dbtable';
        }

        $log['log_writers'] = array('file');

        // TODO: replace this and below w/ configOverride use
        if ($testingEnvironment->tablesPrefix) {
            $database['tables_prefix'] = $testingEnvironment->tablesPrefix;
        }

        if ($testingEnvironment->dbName) {
            $database['dbname'] = $testingEnvironment->dbName;
        }

        if ($testingEnvironment->configOverride) {
            $cache =& $chain->getAll();
            $cache = $this->arrayMergeRecursiveDistinct($cache, $testingEnvironment->configOverride);
        }
    }

    private function arrayMergeRecursiveDistinct(array $array1, array $array2)
    {
        $result = $array1;

        foreach ($array2 as $key => $value) {
            if (is_array($value)) {
                $result[$key] = isset($result[$key]) && is_array($result[$key])
                    ? $this->arrayMergeRecursiveDistinct($result[$key], $value)
                    : $value;
            } else {
                $result[$key] = $value;
            }
        }

        return $result;
    }
}