Completed
Pull Request — master (#168)
by
unknown
02:57
created

SettingsManagerControllerTest::testCopyAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 11
nc 2
nop 4
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\SettingsBundle\Tests\Functional\Controller;
13
14
use ONGR\ElasticsearchBundle\Test\AbstractElasticsearchTestCase;
15
use ONGR\SettingsBundle\Settings\General\Provider\ManagerAwareSettingProvider;
16
use ONGR\SettingsBundle\Settings\General\SettingsContainer;
17
use ONGR\SettingsBundle\Document\Profile;
18
use ONGR\SettingsBundle\Tests\Fixtures\Security\LoginTestHelper;
19
use Symfony\Bundle\FrameworkBundle\Client;
20
21
/**
22
 * Tests for SettingsManagerController.
23
 */
24
class SettingsManagerControllerTest extends AbstractElasticsearchTestCase
25
{
26
    /**
27
     * @var Client.
28
     */
29
    private $client;
30
31
    /**
32
     * @var LoginTestHelper.
33
     */
34
    private $loginHelper;
0 ignored issues
show
Unused Code introduced by
The property $loginHelper is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
35
36
    /**
37
     * @var Container
38
     */
39
    private $container;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
40
41
    public function getDataArray()
42
    {
43
        return ['default' => []];
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function setUp()
50
    {
51
        parent::setUp();
52
53
        $this->client = static::createClient();
54
55
        $this->container = $this->client->getContainer();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->client->getContainer() can also be of type object<Symfony\Component...ion\ContainerInterface>. However, the property $container is declared as type object<ONGR\SettingsBund...l\Controller\Container>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
56
    }
57
58
    /**
59
     * Data provider for testCopyAction.
60
     *
61
     * @return array
62
     */
63
    public function copyActionData()
64
    {
65
        // Case #0 non existing profile, existing item passed.
66
        $out[] = [302, '/settings/setting/test_setting/copy/test', 'non_existant_profile', false];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$out was never initialized. Although not strictly required by PHP, it is generally a good practice to add $out = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
67
68
        // Case #1 existing profile set, existing item passed.
69
        $out[] = [302, '/settings/setting/test_setting/copy/test', 'new_profile', true];
70
71
        // Case #2 non-existent item passed.
72
        $out[] = [500, '/settings/setting/non_existant_setting/copy/test', 'new_profile', false];
73
74
        // Case #3 existent item, non-existing old profile passed.
75
        $out[] = [500, '/settings/setting/test_setting/copy/non_existant_profile', 'new_profile', false];
76
77
        return $out;
78
    }
79
80
    /**
81
     * Creates a profile
82
     * @param string $profile
83
     */
84
    private function createProfile($profile)
85
    {
86
        $requestParameters = [
87
            'profileName' => $profile,
88
            'profileDescription' => 'test profile'
89
        ];
90
        $this->client->request('POST', '/settings/create/profile', $requestParameters);
91
    }
92
93
    /**
94
     * Create setting.
95
     * @param string $name
96
     * @param string $profile
97
     */
98
    private function createSetting($name, $profile)
99
    {
100
        $requestParameters = [
101
            'settingName' => $name,
102
            'settingProfiles' => [$profile],
103
            'settingDescription' => 'description0',
104
            'settingType' => 'string',
105
            'setting-default' => 'test value'
106
        ];
107
        $this->client->request('POST', '/settings/setting/set/', $requestParameters);
108
    }
109
110
    /**
111
     * Data provider for testCreateAction().
112
     *
113
     * @return array
114
     */
115
    public function createActionData()
116
    {
117
        // case #0 create setting with valid request content
118
        $out[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$out was never initialized. Although not strictly required by PHP, it is generally a good practice to add $out = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
119
            [
120
                'settingName' => 'data',
121
                'settingProfiles' => ['test'],
122
                'settingDescription' => 'description0',
123
                'settingType' => 'string',
124
                'setting-default' => 'test value'
125
            ],
126
            302,
127
            true
128
        ];
129
130
        // case #1 create setting with blank request parameters
131
        $out[] = [[], 302, false];
132
133
        // case #3 create setting with no profile
134
        $out[] = [
135
            [
136
                'settingName' => 'data',
137
                'settingDescription' => 'description0',
138
                'settingType' => 'string',
139
                'setting-default' => 'test value'
140
            ],
141
            302,
142
            false
143
        ];
144
145
        // case #3 create array setting
146
        $out[] = [
147
            [
148
                'settingName' => 'data',
149
                'settingProfiles' => ['test'],
150
                'settingDescription' => 'description0',
151
                'settingType' => 'array',
152
                'setting-array_0' => 1,
153
                'setting-array_1' => 2,
154
            ],
155
            302,
156
            true
157
        ];
158
159
        // case #4 create bool setting
160
        $out[] = [
161
            [
162
                'settingName' => 'data',
163
                'settingProfiles' => ['test'],
164
                'settingDescription' => 'description0',
165
                'settingType' => 'bool',
166
                'setting-boolean' => 'true'
167
            ],
168
            302,
169
            true
170
        ];
171
172
        // case #5 create setting with no value
173
        $out[] = [
174
            [
175
                'settingName' => 'data',
176
                'settingProfiles' => ['test'],
177
                'settingDescription' => 'description0',
178
                'settingType' => 'string',
179
            ],
180
            302,
181
            false
182
        ];
183
184
        return $out;
185
    }
186
187
    /**
188
     * Tests profile creation
189
     */
190
    public function testCreateProfile()
191
    {
192
        $requestParameters = [
193
            'profileName' => 'test',
194
            'profileDescription' => 'test'
195
        ];
196
        $this->client->request('POST', '/settings/create/profile', $requestParameters);
197
        $this->assertEquals(302, $this->client->getResponse()->getStatusCode());
198
    }
199
200
    /**
201
     * Test for createAction().
202
     *
203
     * @param array   $requestParameters
204
     * @param int     $statusCode
205
     * @param bool    $created
206
     *
207
     * @dataProvider createActionData()
208
     */
209
    public function testCreateAction($requestParameters, $statusCode, $created)
210
    {
211
        $manager = $this->getManager();
212
        $profile = new Profile();
213
        $profile->setName('test');
214
        $manager->persist($profile);
215
        $manager->commit();
216
217
        $this->client->request('POST', '/settings/setting/set/', $requestParameters);
218
        $this->assertEquals($statusCode, $this->client->getResponse()->getStatusCode());
219
        $settings = $manager->find('ONGRSettingsBundle:Setting', 'test_data');
220
        if ($created) {
221
            $this->assertNotNull($settings);
222
        } else {
223
            $this->assertNull($settings);
224
        }
225
    }
226
227
    /**
228
     * Test for copyAction.
229
     *
230
     * @param int    $status
231
     * @param string $url
232
     * @param string $new_profile
233
     * @param bool   $created
234
     *
235
     * @dataProvider copyActionData()
236
     */
237
    public function testCopyAction($status, $url, $new_profile, $created)
238
    {
239
        $this->createProfile('test');
240
        $this->createProfile('new_profile');
241
        $this->createSetting('test_setting', 'test');
242
        $this->client->request('POST', $url, ['settingProfiles' => [$new_profile]]);
243
        $this->assertEquals($status, $this->client->getResponse()->getStatusCode());
244
245
        $settings = $this->getManager()->find('ONGRSettingsBundle:Setting', $new_profile.'_test_setting');
246
        if ($created) {
247
            $this->assertNotNull($settings);
248
        } else {
249
            $this->assertNull($settings);
250
        }
251
    }
252
253
    /**
254
     * Test for editAction().
255
     */
256 View Code Duplication
    public function testEditAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
257
    {
258
        $this->createProfile('test');
259
        $this->createSetting('test_setting', 'test');
260
        $this->client->request('GET', '/settings/setting/test_setting/edit/test');
261
        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
262
    }
263
264
    /**
265
     * Data provider for testRemoveAction().
266
     *
267
     * @return array
268
     */
269
    public function removeActionData()
270
    {
271
        // Case #0 remove existing setting with domain set.
272
        $out[] = ['/settings/setting/test_setting/remove/test', 302];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$out was never initialized. Although not strictly required by PHP, it is generally a good practice to add $out = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
273
274
        // Case #1 remove non-existing setting.
275
        $out[] = ['/settings/setting/non-existent/remove', 500];
276
277
        return $out;
278
    }
279
280
    /**
281
     * Test for removeAction().
282
     *
283
     * @param string $url
284
     * @param string $expectedStatusCode
285
     *
286
     * @dataProvider removeActionData()
287
     */
288 View Code Duplication
    public function testRemoveAction($url, $expectedStatusCode)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
289
    {
290
        $this->createProfile('test');
291
        $this->createSetting('test_setting', 'test');
292
        $this->client->request('DELETE', $url);
293
        $this->assertEquals($expectedStatusCode, $this->client->getResponse()->getStatusCode());
294
    }
295
296
    /**
297
     * Test EditAction and ensure cached value is cleared.
298
     */
299
    public function testCacheClearAfterModify()
300
    {
301
        $client = $this->client;
302
        $this->createProfile('domain_foo');
303
        // Create setting.
304
        $requestContent = [
305
            'settingName' => 'setting_foo',
306
            'settingType' => 'string',
307
            'settingProfiles' => ['domain_foo'],
308
            'setting-default' => 'foo'
309
        ];
310
        $client->request('POST', '/settings/setting/update/', $requestContent);
311
312
        // Assert value.
313
        $this->enableDomain($client);
314
        $this->assertSettingValue($client, 'foo');
315
316
        // Modify.
317
        $requestContent['setting-default'] = 'bar';
318
        $client->request('POST', '/settings/setting/update/', $requestContent);
319
320
        // Assert modified value.
321
        $this->enableDomain($client);
322
        $this->assertSettingValue($client, 'bar');
323
    }
324
325
    /**
326
     * Assert value has been set.
327
     *
328
     * @param Client $client
329
     * @param string $expectedValue
330
     */
331
    protected function assertSettingValue(Client $client, $expectedValue)
332
    {
333
        $settingsContainer = $client->getContainer()->get('ongr_settings.settings_container');
334
        $value = $settingsContainer->get('setting_foo');
335
        $this->assertSame($expectedValue, $value);
336
    }
337
338
    /**
339
     * Add domain_foo so that the setting can be read.
340
     *
341
     * @param Client $client
342
     */
343
    protected function enableDomain(Client $client)
344
    {
345
        $container = $client->getContainer();
346
        /** @var SettingsContainer $settingsContainer */
347
        $settingsContainer = $container->get('ongr_settings.settings_container');
348
        $settingsContainer->setProfiles(['domain_foo']);
349
350
        /** @var ManagerAwareSettingProvider $provider */
351
        $provider = $container->get('ongr_settings.dummy_profile_provider');
352
        $settingsContainer->addProvider($provider);
353
    }
354
}
355