Completed
Push — master ( 3b7290...73d268 )
by
unknown
11:56
created

ChannelControllerTest::testGrid()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 24
Code Lines 13

Duplication

Lines 8
Ratio 33.33 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 24
rs 8.5125
cc 5
eloc 13
nc 6
nop 1
1
<?php
2
3
namespace OroCRM\Bundle\ChannelBundle\Tests\Functional\Controller;
4
5
use Symfony\Component\Form\Form;
6
7
use Oro\Bundle\TestFrameworkBundle\Test\WebTestCase;
8
use Oro\Bundle\OrganizationBundle\Entity\Organization;
9
use Oro\Bundle\OrganizationBundle\Migrations\Data\ORM\LoadOrganizationAndBusinessUnitData;
10
11
/**
12
 * @outputBuffering enabled
13
 * @dbIsolation
14
 */
15
class ChannelControllerTest extends WebTestCase
16
{
17
    const CHANNEL_NAME = 'some name';
18
    const GRID_NAME    = 'orocrm-channels-grid';
19
20
    public function setUp()
21
    {
22
        $this->initClient(
23
            ['debug' => false],
24
            array_merge($this->generateBasicAuthHeader(), ['HTTP_X-CSRF-Header' => 1])
25
        );
26
        $this->client->useHashNavigation(true);
0 ignored issues
show
Bug introduced by
The method useHashNavigation() does not seem to exist on object<Oro\Bundle\TestFr...workBundle\Test\Client>.

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...
27
    }
28
29
    public function testCreateChannel()
30
    {
31
        $crawler      = $this->client->request('GET', $this->getUrl('orocrm_channel_create'));
32
        $name         = 'Simple channel';
33
        $form         = $crawler->selectButton('Save and Close')->form();
34
        $channelType  = 'custom';
35
36
        $form['orocrm_channel_form[name]']        = $name;
37
        $form['orocrm_channel_form[channelType]'] = $channelType;
38
        $form['orocrm_channel_form[entities]']    = json_encode(
39
            ['OroCRM\Bundle\ChannelBundle\Entity\CustomerIdentity']
40
        );
41
42
        $this->client->followRedirects(true);
43
        $crawler = $this->client->submit($form);
0 ignored issues
show
Documentation introduced by
$form is of type array<string,string,{"or...m[entities]":"string"}>, but the function expects a object<Symfony\Component\DomCrawler\Form>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
        $result  = $this->client->getResponse();
45
46
        $this->assertHtmlResponseStatusCodeEquals($result, 200);
47
        $this->assertContains('Channel saved', $crawler->html());
48
49
        return compact('name', 'channelType');
50
    }
51
52
    /**
53
     * @depends testCreateChannel
54
     */
55
    public function testView($data)
56
    {
57
        $response = $this->client->requestGrid(
58
            self::GRID_NAME,
59
            [
60
                self::GRID_NAME . '[_filter][name][value]' => $data['name']
61
            ]
62
        );
63
64
        $gridResult = $this->getJsonResponseContent($response, 200);
65
        $gridResult = reset($gridResult['data']);
66
        $id         = $gridResult['id'];
67
68
        $crawler = $this->client->request(
69
            'GET',
70
            $this->getUrl('orocrm_channel_view', ['id' => $id])
71
        );
72
73
        $result = $this->client->getResponse();
74
75
        $this->assertHtmlResponseStatusCodeEquals($result, 200);
76
        $this->assertContains('Channels', $crawler->html());
77
        $this->assertContains($data['name'], $crawler->html());
78
        $this->assertContains($data['channelType'], $crawler->html());
79
    }
80
81
    /**
82
     * @param array $data
83
     *
84
     * @depends testCreateChannel
85
     *
86
     * @return array
87
     */
88
    public function testUpdateChannel($data)
89
    {
90
        $response = $this->client->requestGrid(
91
            self::GRID_NAME,
92
            [
93
                self::GRID_NAME . '[_filter][name][value]' => $data['name']
94
            ]
95
        );
96
97
        $result  = $this->getJsonResponseContent($response, 200);
98
        $result  = reset($result['data']);
99
        $channel = $result;
100
101
        $crawler = $this->client->request(
102
            'GET',
103
            $this->getUrl('orocrm_channel_update', ['id' => $result['id']])
104
        );
105
        /** @var Form $form */
106
        $form                              = $crawler->selectButton('Save and Close')->form();
107
        $name                              = 'name' . $this->generateRandomString();
108
        $form['orocrm_channel_form[name]'] = $name;
109
110
        $this->client->followRedirects(true);
111
        $crawler = $this->client->submit($form);
0 ignored issues
show
Documentation introduced by
$form is of type object<Symfony\Component\Form\Form>, but the function expects a object<Symfony\Component\DomCrawler\Form>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
112
113
        $result = $this->client->getResponse();
114
        $this->assertHtmlResponseStatusCodeEquals($result, 200, 'text/html; charset=UTF-8');
115
        $this->assertContains('Channel saved', $crawler->html());
116
117
        $channel['name'] = $name;
118
119
        return $channel;
120
    }
121
122
    /**
123
     * @depends testUpdateChannel
124
     *
125
     * @param $channel
126
     */
127
    public function testChangeStatusChannel($channel)
128
    {
129
        $crawler = $this->client->request(
130
            'GET',
131
            $this->getUrl('orocrm_channel_change_status', ['id' => $channel['id']])
132
        );
133
134
        $this->client->getResponse();
0 ignored issues
show
Unused Code introduced by
The call to the method Oro\Bundle\TestFramework...t\Client::getResponse() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
135
        $this->assertContains('Channel deactivated', $crawler->html());
136
137
        return $channel;
138
    }
139
140
    /**
141
     * @depends testChangeStatusChannel
142
     *
143
     * @param $channel
144
     */
145
    public function testDeleteChannel($channel)
146
    {
147
        $this->client->request(
148
            'DELETE',
149
            $this->getUrl('orocrm_api_delete_channel', ['id' => $channel['id']])
150
        );
151
152
        $response = $this->client->getResponse();
153
        $this->assertResponseStatusCodeEquals($response, 204);
154
155
        $response = $this->client->requestGrid(
156
            self::GRID_NAME,
157
            [
158
                self::GRID_NAME . '[_filter][name][value]' => $channel['name']
159
            ]
160
        );
161
162
        $result = $this->getJsonResponseContent($response, 200);
163
164
        $this->assertEmpty($result['data']);
165
        $this->assertEmpty($result['options']['totalRecords']);
166
    }
167
168
    /**
169
     * @dataProvider gridProvider
170
     *
171
     * @param array $filters
172
     */
173
    public function testGrid($filters)
174
    {
175
        $this->loadFixtures(['OroCRM\Bundle\ChannelBundle\Tests\Functional\Fixture\LoadChannel']);
176
177
        if (isset($filters['gridParameters']['id'])) {
178
            $gridId = $filters['gridParameters']['gridName'] . '[' . $filters['gridParameters']['id'] . ']';
179
180
            $filters['gridParameters'][$gridId] = $this->getReference('default_channel')->getId();
181
        }
182
183
        $response = $this->client->requestGrid($filters['gridParameters'], $filters['gridFilters']);
184
        $result   = $this->getJsonResponseContent($response, 200);
185
186 View Code Duplication
        foreach ($result['data'] as $row) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
187
            if ((isset($filters['gridParameters']['id']))) {
188
                foreach ($filters['assert'] as $fieldName => $value) {
189
                    $this->assertEquals($value, $row[$fieldName]);
190
                }
191
                break;
192
            }
193
        }
194
195
        $this->assertCount((int) $filters['expectedResultCount'], $result['data']);
196
    }
197
198
    public function gridProvider()
199
    {
200
        return [
201
            'Channel grid'                => [
202
                [
203
                    'gridParameters'      => [
204
                        'gridName' => self::GRID_NAME
205
                    ],
206
                    'gridFilters'         => [],
207
                    'assert'              => [
208
                        'name' => self::CHANNEL_NAME,
209
                    ],
210
                    'expectedResultCount' => 1
211
                ],
212
            ],
213
            'Channel grid without result' => [
214
                [
215
                    'gridParameters'      => [
216
                        'gridName' => self::GRID_NAME
217
                    ],
218
                    'gridFilters'         => [
219
                        self::GRID_NAME . '[_filter][name][value]' => 'Not found',
220
                    ],
221
                    'assert'              => [
222
                        'name' => self::CHANNEL_NAME,
223
                    ],
224
                    'expectedResultCount' => 0
225
                ],
226
            ],
227
        ];
228
    }
229
}
230