SettingsManagerTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 188
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B getDataArray() 0 76 1
B testGetAllExperiments() 0 32 2
A testToggleExperiment() 0 11 1
A testGetCachedExperiment() 0 19 1
A testGetCachedExperimentException() 0 4 1
A getSettingsManager() 0 15 2
A tearDown() 0 7 1
1
<?php
2
3
namespace ONGR\SettingsBundle\Tests\Functional;
4
5
use ONGR\ElasticsearchBundle\Test\AbstractElasticsearchTestCase;
6
use ONGR\SettingsBundle\Service\SettingsManager;
7
8
class SettingsManagerTest extends AbstractElasticsearchTestCase
9
{
10
    /**
11
     * @var SettingsManager
12
     */
13
    private $settingsManager;
14
15
    protected function getDataArray()
16
    {
17
        return [
18
            'settings' => [
19
                'setting' => [
20
                    [
21
                        '_id' => '1',
22
                        'name' => 'foo',
23
                        'profile' => [
24
                            'profile1',
25
                        ],
26
                        'value' => '1',
27
                        'type' => 'bool',
28
                    ],
29
                    [
30
                        '_id' => '2',
31
                        'name' => 'bar',
32
                        'profile' => [
33
                            'profile1',
34
                            'profile2',
35
                        ],
36
                        'value' => 'test-string',
37
                        'type' => 'string',
38
                    ],
39
                    [
40
                        '_id' => '3',
41
                        'name' => 'acme',
42
                        'profile' => [
43
                            'profile2',
44
                        ],
45
                        'value' => '0',
46
                        'type' => 'bool',
47
                    ],
48
                    [
49
                        '_id' => '4',
50
                        'name' => 'active_profiles',
51
                        'value' => [
52
                            'profile2',
53
                        ],
54
                        'type' => 'hidden',
55
                    ],
56
                    [
57
                        '_id' => '5',
58
                        'name' => 'active_experiments',
59
                        'value' => [
60
                            'foo_experiment',
61
                        ],
62
                        'type' => 'hidden',
63
                    ],
64
                    [
65
                        '_id' => '6',
66
                        'name' => 'foo_experiment',
67
                        'profile' => [
68
                            'profile1',
69
                            'profile2',
70
                        ],
71
                        'value' => [
72
                            'test_experiment_value',
73
                        ],
74
                        'type' => 'experiment',
75
                    ],
76
                    [
77
                        '_id' => '7',
78
                        'name' => 'bar_experiment',
79
                        'profile' => [
80
                            'profile1',
81
                        ],
82
                        'value' => [
83
                            'test_experiment_value',
84
                        ],
85
                        'type' => 'experiment',
86
                    ],
87
                ]
88
            ]
89
        ];
90
    }
91
92
    public function testGetAllExperiments()
93
    {
94
        $expected = [
95
            [
96
                'id' => '6',
97
                'name' => 'foo_experiment',
98
                'value' => ['test_experiment_value'],
99
                'profile' => ['profile1', 'profile2'],
100
                'type' => 'experiment',
101
                'description' => null,
102
                'salt' => null,
103
            ],
104
            [
105
                'id' => '7',
106
                'name' => 'bar_experiment',
107
                'value' => ['test_experiment_value'],
108
                'profile' => ['profile1'],
109
                'type' => 'experiment',
110
                'description' => null,
111
                'salt' => null,
112
            ]
113
        ];
114
115
        $experiments = [];
116
        $results = $this->getContainer()->get('ongr_settings.settings_manager')->getAllExperiments();
117
118
        foreach ($results as $experiment) {
119
            $experiments[] = $experiment->getSerializableData();
120
        }
121
122
        $this->assertEquals($expected, $experiments);
123
    }
124
125
    public function testToggleExperiment()
126
    {
127
        $manager = $this->getSettingsManager();
128
        $this->assertEquals(['foo_experiment'], $manager->getActiveExperiments());
129
130
        $manager->toggleExperiment('foo_experiment');
131
        $this->assertEquals([], $manager->getActiveExperiments());
132
133
        $manager->toggleExperiment('bar_experiment');
134
        $this->assertEquals(['bar_experiment'], $manager->getActiveExperiments());
135
    }
136
137
    public function testGetCachedExperiment()
138
    {
139
        $expected = [
140
            'id' => '6',
141
            'name' => 'foo_experiment',
142
            'value' => ['test_experiment_value'],
143
            'profile' => ['profile1', 'profile2'],
144
            'type' => 'experiment',
145
            'description' => null,
146
            'salt' => null,
147
        ];
148
        $manager = $this->getSettingsManager();
149
        // Test it without cache:
150
        $this->assertEquals($expected, $manager->getCachedExperiment('foo_experiment'));
151
        // Test it with cache:
152
        $this->assertEquals($expected, $manager->getCachedExperiment('foo_experiment'));
153
        // test a non existing experiment:
154
        $this->assertNull($manager->getCachedExperiment('non-existing-experiment'));
155
    }
156
157
    /**
158
     * @expectedException \LogicException
159
     * @expectedExceptionMessage The setting `foo` was found but it is not an experiment
160
     */
161
    public function testGetCachedExperimentException()
162
    {
163
        $this->getSettingsManager()->getCachedExperiment('foo');
164
    }
165
166
    /**
167
     * @return SettingsManager
168
     */
169
    private function getSettingsManager()
170
    {
171
        if (!empty($this->settingsManager)) {
172
            $this->settingsManager->getCache()->deleteAll();
173
            return $this->settingsManager;
174
        }
175
176
        /** @var SettingsManager $manager */
177
        $manager = $this->getContainer()->get('ongr_settings.settings_manager');
178
        $manager->setActiveExperimentsSettingName('active_experiments');
179
180
        $this->settingsManager = $manager;
181
182
        return $this->settingsManager;
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188
    protected function tearDown()
189
    {
190
        parent::tearDown();
191
192
        // Deletes the settings manager cache
193
        $this->getSettingsManager();
194
    }
195
}
196