Completed
Push — master ( b8cd4e...ae6a8d )
by Simonas
8s
created

SettingsManagerControllerTest::testCreateAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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\Common\Provider\ManagerAwareSettingProvider;
16
use ONGR\SettingsBundle\Settings\Common\SettingsContainer;
17
use ONGR\SettingsBundle\Tests\Fixtures\Security\LoginTestHelper;
18
use Symfony\Bundle\FrameworkBundle\Client;
19
20
/**
21
 * Tests for SettingsManagerController.
22
 */
23
class SettingsManagerControllerTest extends AbstractElasticsearchTestCase
24
{
25
    /**
26
     * @var Client.
27
     */
28
    private $client;
29
30
    /**
31
     * @var LoginTestHelper.
32
     */
33
    private $loginHelper;
34
35
    /**
36
     * @var Container
37
     */
38
    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...
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 View Code Duplication
    public function setUp()
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...
44
    {
45
        parent::setUp();
46
47
        $this->loginHelper = new LoginTestHelper(static::createClient());
48
        $this->client = $this->loginHelper->loginAction();
49
50
        $this->container = $this->client->getContainer();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->client->getContainer() of type object<Symfony\Component...ion\ContainerInterface> is incompatible with the declared type object<ONGR\SettingsBund...l\Controller\Container> of property $container.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
51
    }
52
53
    /**
54
     * Data provider for testCopyAction.
55
     *
56
     * @return array
57
     */
58
    public function copyActionData()
59
    {
60
        // Case #0 non existing profile, existing item passed.
61
        $out[] = [500, '/settings/setting/name0/copy/foo/newProfile'];
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...
62
63
        // Case #1 existing profile set, existing item passed.
64
        $out[] = [200, '/settings/setting/name0/copy/default/newProfile'];
65
66
        // Case #2 non-existent profile and item passed.
67
        $out[] = [500, '/settings/setting/foo/copy/foo/newProfile'];
68
69
        // Case #3 existent profile, non-existing item passed.
70
        $out[] = [500, '/settings/setting/foo/copy/default/newProfile'];
71
72
        return $out;
73
    }
74
75
    /**
76
     * Create setting.
77
     */
78
    public function createSetting()
79
    {
80
        $requestContent = json_encode(['setting' => ['data' => ['value' => 'name0']]]);
81
        $this->client->request('POST', '/settings/setting/ng/name0/edit/default', [], [], [], $requestContent);
82
    }
83
84
    /**
85
     * Data provider for testCreateAction().
86
     *
87
     * @return array
88
     */
89
    public function createActionData()
90
    {
91
        // case #0 create setting with valid request content
92
        $out[] = [json_encode(['setting' => ['data' => ['value' => 'foo'], 'description' => 'description0']]), 200];
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...
93
94
        // case #1 create setting with blank request content
95
        $out[] = [json_encode([]), 400];
96
97
        // case #2 create setting with no request content
98
        $out[] = [null, 400];
99
100
        return $out;
101
    }
102
103
    /**
104
     * Test for createAction().
105
     *
106
     * @param string $requestContent
107
     * @param int    $statusCode
108
     *
109
     * @dataProvider createActionData()
110
     */
111
    public function testCreateAction($requestContent, $statusCode)
112
    {
113
        $this->client->request('POST', '/settings/setting/ng/setting_foo/edit/domain_foo', [], [], [], $requestContent);
114
        $this->assertEquals($statusCode, $this->client->getResponse()->getStatusCode());
115
    }
116
117
    /**
118
     * Test for copyAction.
119
     *
120
     * @param int    $status
121
     * @param string $url
122
     *
123
     * @dataProvider copyActionData()
124
     */
125
    public function testCopyActionLogedIn($status, $url)
126
    {
127
        $this->createSetting();
128
        $this->client->request('GET', $url);
129
        $this->assertEquals($status, $this->client->getResponse()->getStatusCode());
130
    }
131
132
    /**
133
     * Test for copyAction after logged Out.
134
     *
135
     * @param int    $status
136
     * @param string $url
137
     *
138
     * @dataProvider copyActionData()
139
     */
140
    public function testCopyActionLogedOut($status, $url)
0 ignored issues
show
Unused Code introduced by
The parameter $status is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
141
    {
142
        $this->createSetting();
143
        $this->client = $this->loginHelper->logoutAction($this->client);
144
        $this->client->request('GET', $url);
145
        $this->assertSame('/settings/login', $this->client->getRequest()->getRequestUri());
0 ignored issues
show
Bug introduced by
The method getRequestUri() does not seem to exist on object<Symfony\Component\BrowserKit\Request>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
146
    }
147
148
    /**
149
     * Test for editAction().
150
     */
151
    public function testEditAction()
152
    {
153
        $this->createSetting();
154
        $this->client->request('GET', '/settings/setting/name0/edit');
155
        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
156
    }
157
158
    /**
159
     * Data provider for testRemoveAction().
160
     *
161
     * @return array
162
     */
163
    public function removeActionData()
164
    {
165
        // Case #0 remove existing settings, check if default domain is set.
166
        $out[] = ['/settings/setting/name0/remove', 200];
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...
167
168
        // Case #1 remove existing setting with domain set.
169
        $out[] = ['/settings/setting/name0/remove/default', 200];
170
171
        // Case #2 remove non-existing setting.
172
        $out[] = ['/settings/setting/non-existent/remove', 500];
173
174
        return $out;
175
    }
176
177
    /**
178
     * Test for removeAction().
179
     *
180
     * @param string $url
181
     * @param string $expectedStatusCode
182
     *
183
     * @dataProvider removeActionData()
184
     */
185
    public function testRemoveAction($url, $expectedStatusCode)
186
    {
187
        $this->createSetting();
188
        $this->client->request('DELETE', $url);
189
        $this->assertEquals($expectedStatusCode, $this->client->getResponse()->getStatusCode());
190
    }
191
192
    /**
193
     * Test ngEditAction and ensure cached value is cleared.
194
     */
195
    public function testCacheClearAfterModify()
196
    {
197
        $client = $this->client;
198
199
        // Create setting.
200
        $requestContent = json_encode(['setting' => ['data' => ['value' => 'foo']]]);
201
        $client->request('POST', '/settings/setting/ng/setting_foo/edit/domain_foo', [], [], [], $requestContent);
202
        $response = $client->getResponse();
203
        $this->assertTrue($response->isOk());
204
205
        // Assert value.
206
        $this->enableDomain($client);
207
        $this->assertSettingValue($client, 'foo');
208
209
        // Modify.
210
        $requestContent = json_encode(['setting' => ['data' => ['value' => 'bar']]]);
211
        $client->request('POST', '/settings/setting/ng/setting_foo/edit/domain_foo', [], [], [], $requestContent);
212
        $response = $client->getResponse();
213
        $this->assertTrue($response->isOk());
214
215
        // Assert modified value.
216
        $this->enableDomain($client);
217
        $this->assertSettingValue($client, 'bar');
218
    }
219
220
    /**
221
     * Assert value has been set.
222
     *
223
     * @param Client $client
224
     * @param string $expectedValue
225
     */
226
    protected function assertSettingValue(Client $client, $expectedValue)
227
    {
228
        $settingsContainer = $client->getContainer()->get('ongr_settings.settings_container');
229
        $value = $settingsContainer->get('setting_foo');
230
        $this->assertSame($expectedValue, $value);
231
    }
232
233
    /**
234
     * Add domain_foo so that the setting can be read.
235
     *
236
     * @param Client $client
237
     */
238
    protected function enableDomain(Client $client)
239
    {
240
        $container = $client->getContainer();
241
        /** @var SettingsContainer $settingsContainer */
242
        $settingsContainer = $container->get('ongr_settings.settings_container');
243
        $settingsContainer->setProfiles(['domain_foo']);
244
245
        /** @var ManagerAwareSettingProvider $provider */
246
        $provider = $container->get('ongr_settings.dummy_profile_provider');
247
        $settingsContainer->addProvider($provider);
248
    }
249
}
250