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

LeadControllersTest::testInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 15
Ratio 100 %
Metric Value
dl 15
loc 15
rs 9.4285
cc 1
eloc 10
nc 1
nop 1
1
<?php
2
3
namespace OroCRM\Bundle\SalesBundle\Tests\Functional\Controller;
4
5
use Symfony\Component\DomCrawler\Form;
6
use Symfony\Component\DomCrawler\Field\ChoiceFormField;
7
8
use Oro\Bundle\DataGridBundle\Tests\Functional\AbstractDatagridTestCase;
9
10
use OroCRM\Bundle\SalesBundle\Tests\Functional\Fixture\LoadSalesBundleFixtures;
11
12
/**
13
 * @outputBuffering enabled
14
 * @dbIsolation
15
 */
16
class LeadControllersTest extends AbstractDatagridTestCase
17
{
18
    /** @var bool */
19
    protected $isRealGridRequest = false;
20
21 View Code Duplication
    protected 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...
22
    {
23
        $this->initClient(
24
            [],
25
            array_merge($this->generateBasicAuthHeader(), ['HTTP_X-CSRF-Header' => 1])
26
        );
27
        $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...
28
        $this->loadFixtures(['OroCRM\Bundle\SalesBundle\Tests\Functional\Fixture\LoadSalesBundleFixtures']);
29
    }
30
31
    public function testIndex()
32
    {
33
        $this->client->request('GET', $this->getUrl('orocrm_sales_lead_index'));
34
        $result = $this->client->getResponse();
35
        $this->assertHtmlResponseStatusCodeEquals($result, 200);
36
    }
37
38
    public function testCreate()
39
    {
40
        $crawler = $this->client->request('GET', $this->getUrl('orocrm_sales_lead_create'));
41
        /** @var Form $form */
42
        $form = $crawler->selectButton('Save and Close')->form();
43
        $name = 'name' . $this->generateRandomString();
44
        $form['orocrm_sales_lead_form[name]']                = $name;
45
        $form['orocrm_sales_lead_form[firstName]']           = 'firstName';
46
        $form['orocrm_sales_lead_form[lastName]']            = 'lastName';
47
        $form['orocrm_sales_lead_form[address][city]']       = 'City Name';
48
        $form['orocrm_sales_lead_form[address][label]']      = 'Main Address';
49
        $form['orocrm_sales_lead_form[address][postalCode]'] = '10000';
50
        $form['orocrm_sales_lead_form[address][street2]']    = 'Second Street';
51
        $form['orocrm_sales_lead_form[address][street]']     = 'Main Street';
52
        $form['orocrm_sales_lead_form[companyName]']         = 'Company';
53
        $form['orocrm_sales_lead_form[email]']               = '[email protected]';
54
        $form['orocrm_sales_lead_form[owner]']               = 1;
55
        $form['orocrm_sales_lead_form[dataChannel]']         = $this->getReference('default_channel')->getId();
56
57
        $doc = new \DOMDocument("1.0");
58
        $doc->loadHTML(
59
            '<select name="orocrm_sales_lead_form[address][country]" id="orocrm_sales_lead_form_address_country" ' .
60
            'tabindex="-1" class="select2-offscreen"> ' .
61
            '<option value="" selected="selected"></option> ' .
62
            '<option value="US">United States</option> </select>'
63
        );
64
        $field = new ChoiceFormField($doc->getElementsByTagName('select')->item(0));
0 ignored issues
show
Compatibility introduced by
$doc->getElementsByTagName('select')->item(0) of type object<DOMNode> is not a sub-type of object<DOMElement>. It seems like you assume a child class of the class DOMNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
65
        $form->set($field);
66
        $doc->loadHTML(
67
            '<select name="orocrm_sales_lead_form[address][region]" id="orocrm_sales_lead_form_address_region" ' .
68
            'tabindex="-1" class="select2-offscreen"> ' .
69
            '<option value="" selected="selected"></option> ' .
70
            '<option value="US-CA">California</option> </select>'
71
        );
72
        $field = new ChoiceFormField($doc->getElementsByTagName('select')->item(0));
0 ignored issues
show
Compatibility introduced by
$doc->getElementsByTagName('select')->item(0) of type object<DOMNode> is not a sub-type of object<DOMElement>. It seems like you assume a child class of the class DOMNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
73
        $form->set($field);
74
75
        $form['orocrm_sales_lead_form[address][country]'] = 'US';
76
        $form['orocrm_sales_lead_form[address][region]'] = 'US-CA';
77
78
        $this->client->followRedirects(true);
79
        $crawler = $this->client->submit($form);
80
81
        $result = $this->client->getResponse();
82
        $this->assertHtmlResponseStatusCodeEquals($result, 200);
83
        $this->assertContains("Lead saved", $crawler->html());
84
85
        return $name;
86
    }
87
88
    /**
89
     * @param string $name
90
     * @depends testCreate
91
     *
92
     * @return string
93
     */
94 View Code Duplication
    public function testUpdate($name)
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...
95
    {
96
        $response = $this->client->requestGrid(
97
            'sales-lead-grid',
98
            ['sales-lead-grid[_filter][name][value]' => $name]
99
        );
100
101
        $result = $this->getJsonResponseContent($response, 200);
102
        $result = reset($result['data']);
103
        $returnValue = $result;
104
        $crawler = $this->client->request(
105
            'GET',
106
            $this->getUrl('orocrm_sales_lead_update', ['id' => $result['id']])
107
        );
108
109
        /** @var Form $form */
110
        $form = $crawler->selectButton('Save and Close')->form();
111
        $name = 'name' . $this->generateRandomString();
112
        $form['orocrm_sales_lead_form[name]'] = $name;
113
114
        $this->client->followRedirects(true);
115
        $crawler = $this->client->submit($form);
116
117
        $result = $this->client->getResponse();
118
        $this->assertHtmlResponseStatusCodeEquals($result, 200);
119
        $this->assertContains("Lead saved", $crawler->html());
120
121
        $returnValue['name'] = $name;
122
123
        return $returnValue;
124
    }
125
126
    /**
127
     * @param array $returnValue
128
     * @depends testUpdate
129
     *
130
     * @return string
131
     */
132 View Code Duplication
    public function testView($returnValue)
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...
133
    {
134
        $crawler = $this->client->request(
135
            'GET',
136
            $this->getUrl('orocrm_sales_lead_view', ['id' => $returnValue['id']])
137
        );
138
139
        $result = $this->client->getResponse();
140
        $this->assertHtmlResponseStatusCodeEquals($result, 200);
141
        $this->assertContains("{$returnValue['name']} - Leads - Sales", $crawler->html());
142
    }
143
144
    /**
145
     * @param array $returnValue
146
     * @depends testUpdate
147
     *
148
     * @return string
149
     */
150 View Code Duplication
    public function testInfo($returnValue)
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...
151
    {
152
        $crawler = $this->client->request(
153
            'GET',
154
            $this->getUrl(
155
                'orocrm_sales_lead_info',
156
                ['id' => $returnValue['id'], '_widgetContainer' => 'block']
157
            )
158
        );
159
160
        $result = $this->client->getResponse();
161
        $this->assertHtmlResponseStatusCodeEquals($result, 200);
162
        $this->assertContains($returnValue['firstName'], $crawler->html());
163
        $this->assertContains($returnValue['lastName'], $crawler->html());
164
    }
165
166
    /**
167
     * @param array $returnValue
168
     * @depends testUpdate
169
     */
170
    public function testDelete($returnValue)
171
    {
172
        $this->client->request(
173
            'DELETE',
174
            $this->getUrl('oro_api_delete_lead', ['id' => $returnValue['id']])
175
        );
176
177
        $result = $this->client->getResponse();
178
        $this->assertEmptyResponseStatusCodeEquals($result, 204);
179
180
        $this->client->request(
181
            'GET',
182
            $this->getUrl('orocrm_sales_lead_view', ['id' => $returnValue['id']])
183
        );
184
185
        $result = $this->client->getResponse();
186
        $this->assertHtmlResponseStatusCodeEquals($result, 404);
187
    }
188
189
    /**
190
     * @return array
191
     */
192
    public function gridProvider()
193
    {
194
        return [
195
            'Lead grid'                => [
196
                [
197
                    'gridParameters'      => [
198
                        'gridName' => 'sales-lead-grid'
199
                    ],
200
                    'gridFilters'         => [],
201
                    'assert'              => [
202
                        'name'        => 'Lead name',
203
                        'channelName' => LoadSalesBundleFixtures::CHANNEL_NAME,
204
                        'firstName'   => 'fname',
205
                        'lastName'    => 'lname',
206
                        'email'       => '[email protected]'
207
                    ],
208
                    'expectedResultCount' => 1
209
                ],
210
            ],
211
            'Lead grid with filters'   => [
212
                [
213
                    'gridParameters'      => [
214
                        'gridName' => 'sales-lead-grid'
215
                    ],
216
                    'gridFilters'         => [
217
                        'sales-lead-grid[_filter][name][value]' => 'Lead name',
218
                    ],
219
                    'assert'              => [
220
                        'name'        => 'Lead name',
221
                        'channelName' => LoadSalesBundleFixtures::CHANNEL_NAME,
222
                        'firstName'   => 'fname',
223
                        'lastName'    => 'lname',
224
                        'email'       => '[email protected]'
225
                    ],
226
                    'expectedResultCount' => 1
227
                ],
228
            ],
229
            'Lead grid without result' => [
230
                [
231
                    'gridParameters'      => [
232
                        'gridName' => 'sales-lead-grid'
233
                    ],
234
                    'gridFilters'         => [
235
                        'sales-lead-grid[_filter][name][value]' => 'some name',
236
                    ],
237
                    'assert'              => [],
238
                    'expectedResultCount' => 0
239
                ],
240
            ],
241
        ];
242
    }
243
}
244