Completed
Push — master ( c2123c...66812a )
by
unknown
12:37
created

OrderPlaceControllerTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 493
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 11
dl 0
loc 493
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 12 1
A postFixtureLoad() 0 9 1
A testCartAction() 0 20 1
A testSyncAction() 0 57 1
A getModifiedCartData() 0 52 1
A testCustomerSyncAction() 0 49 1
A getModifiedCustomerOrder() 0 63 1
A testSyncGuestOrderAction() 0 60 1
A getModifiedGuestCartData() 0 51 1
A getModifiedGuestCustomerOrder() 0 62 1
1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\Tests\Functional\Controller;
4
5
use Oro\Bundle\IntegrationBundle\Entity\Channel;
6
use Oro\Bundle\TestFrameworkBundle\Test\WebTestCase;
7
8
use OroCRM\Bundle\MagentoBundle\Entity\Cart;
9
use OroCRM\Bundle\MagentoBundle\Entity\Order;
10
use OroCRM\Bundle\MagentoBundle\Entity\Customer;
11
12
use OroCRM\Bundle\MagentoBundle\Tests\Functional\Controller\Stub\StubIterator;
13
14
/**
15
 * @outputBuffering enabled
16
 * @dbIsolation
17
 */
18
class OrderPlaceControllerTest extends WebTestCase
19
{
20
    const TEST_NEW_EMAIL       = '[email protected]';
21
    const TEST_NEW_GUEST_EMAIL = '[email protected]';
22
    const TEST_NEW_ITEMS_QTY   = 444;
23
24
    /** @var Channel */
25
    protected $channel;
26
27
    /** @var Cart */
28
    protected $cart;
29
30
    /** @var Order */
31
    protected $order;
32
33
    /** @var Cart */
34
    protected $guestCart;
35
36
    /** @var Order */
37
    protected $guestOrder;
38
39
    /** @var Customer */
40
    protected $customer;
41
42
    /** @var  \PHPUnit_Framework_MockObject_MockObject */
43
    protected $soapTransport;
44
45
    protected function setUp()
46
    {
47
        $this->initClient(['debug' => false], $this->generateBasicAuthHeader(), true);
48
        $this->client->useHashNavigation(true);
49
        $this->loadFixtures(['OroCRM\Bundle\MagentoBundle\Tests\Functional\Fixture\LoadMagentoChannel'], true);
50
51
        $this->soapTransport = $this->getMockBuilder('OroCRM\Bundle\MagentoBundle\Provider\Transport\SoapTransport')
52
            ->setMethods(['init', 'call', 'getCarts', 'getCustomers', 'getOrders'])
53
            ->disableOriginalConstructor()->getMock();
54
55
        $this->getContainer()->set('orocrm_magento.transport.soap_transport', $this->soapTransport);
56
    }
57
58
    protected function postFixtureLoad()
59
    {
60
        $this->channel  = $this->getReference('integration');
61
        $this->cart     = $this->getReference('cart');
62
        $this->order    = $this->getReference('order');
63
        $this->customer = $this->getReference('customer');
64
        $this->guestCart = $this->getReference('guestCart');
65
        $this->guestOrder = $this->getReference('guestOrder');
66
    }
67
68
    public function testCartAction()
69
    {
70
        $widgetId = '2w45254tst4562';
71
        $this->client->request(
72
            'GET',
73
            $this->getUrl(
74
                'orocrm_magento_orderplace_cart',
75
                [
76
                    'id'               => $this->cart->getId(),
77
                    '_widgetContainer' => 'block',
78
                    '_wid'             => $widgetId
79
                ]
80
            )
81
        );
82
        $result = $this->client->getResponse();
83
        $this->assertHtmlResponseStatusCodeEquals($result, 200);
84
        $this->assertContains('iframe', $result->getContent());
85
        $this->assertContains('orderPlaceFrame', $result->getContent());
86
        $this->assertContains($widgetId, $result->getContent());
87
    }
88
89
    public function testSyncAction()
90
    {
91
        $jobManager = $this->getContainer()->get('akeneo_batch.job_repository')->getJobManager();
92
        $jobManager->beginTransaction();
93
        $newCart = $this->getModifiedCartData($this->cart, $this->customer);
94
95
        $cartIterator = new StubIterator([$newCart]);
96
        $orderIterator = new StubIterator(
97
            [
98
                [
99
                    'increment_id' => $this->order->getIncrementId(),
100
                    'quote_id' => $this->cart->getOriginId(),
101
                    'customer_id' => $this->customer->getOriginId(),
102
                ],
103
            ]
104
        );
105
        $customerIterator = new StubIterator([]);
106
107
        $this->soapTransport->expects($this->any())->method('call');
108
        $this->soapTransport->expects($this->once())->method('getCarts')->will($this->returnValue($cartIterator));
109
        $this->soapTransport->expects($this->once())->method('getOrders')->will($this->returnValue($orderIterator));
110
        $this->soapTransport->expects($this->any())->method('getCustomers')
111
            ->will($this->returnValue($customerIterator));
112
113
        $this->client->request(
114
            'GET',
115
            $this->getUrl('orocrm_magento_orderplace_new_cart_order_sync', ['id' => $this->cart->getId()]),
116
            [],
117
            [],
118
            ['HTTP_X-Requested-With' => 'XMLHttpRequest']
119
        );
120
121
        $result = $this->client->getResponse();
122
        $this->assertJsonResponseStatusCodeEquals($result, 200);
123
        $arrayJson = json_decode($result->getContent(), 1);
124
        $this->assertEquals('success', $arrayJson['statusType']);
125
        $this->assertEquals('Data successfully synchronized.', $arrayJson['message']);
126
        $this->assertEquals(
127
            $arrayJson['url'],
128
            $this->getUrl('orocrm_magento_order_view', ['id' => $this->order->getId()])
129
        );
130
        $jobManager->rollback();
131
        $jobManager->getConnection()->close();
132
133
        $this->client->request('GET', $this->getUrl('orocrm_magento_cart_view', ['id' => $this->cart->getId()]));
134
        $result = $this->client->getResponse();
135
        $this->assertHtmlResponseStatusCodeEquals($result, 200);
136
137
        $resultContent = $result->getContent();
138
        $this->assertContains('Cart Information', $resultContent);
139
        $this->assertContains(self::TEST_NEW_EMAIL, $resultContent);
140
        $this->assertContains((string)self::TEST_NEW_ITEMS_QTY, $resultContent);
141
        $this->assertContains('Expired', $resultContent);
142
143
        $this->assertContains('Customer Information', $resultContent);
144
        $this->assertContains('[email protected]', $resultContent);
145
    }
146
147
    /**
148
     * @param Cart     $cart
149
     * @param Customer $customer
150
     *
151
     * @return array
152
     */
153
    protected function getModifiedCartData(Cart $cart, Customer $customer)
154
    {
155
        return [
156
            'entity_id'                   => $cart->getOriginId(),
157
            'store_id'                    => $cart->getStore()->getOriginId(),
158
            'created_at'                  => '2014-04-22 10:41:43',
159
            'updated_at'                  => '2014-05-29 08:52:33',
160
            'is_active'                   => false,
161
            'is_virtual'                  => false,
162
            'is_multi_shipping'           => false,
163
            'items_count'                 => '2',
164
            'items_qty'                   => self::TEST_NEW_ITEMS_QTY,
165
            'orig_order_id'               => '0',
166
            'store_to_base_rate'          => '1.0000',
167
            'store_to_quote_rate'         => '1.0000',
168
            'base_currency_code'          => 'USD',
169
            'store_currency_code'         => 'USD',
170
            'quote_currency_code'         => 'USD',
171
            'grand_total'                 => '855.0000',
172
            'base_grand_total'            => '855.0000',
173
            'customer_id'                 => $customer->getOriginId(),
174
            'customer_tax_class_id'       => '3',
175
            'customer_group_id'           => '1',
176
            'customer_email'              => self::TEST_NEW_EMAIL,
177
            'customer_firstname'          => 'firstname',
178
            'customer_lastname'           => 'lastname',
179
            'customer_note_notify'        => '1',
180
            'customer_is_guest'           => '0',
181
            'remote_ip'                   => '82.117.235.210',
182
            'global_currency_code'        => 'USD',
183
            'base_to_global_rate'         => '1.0000',
184
            'base_to_quote_rate'          => '1.0000',
185
            'subtotal'                    => '855.0000',
186
            'base_subtotal'               => '855.0000',
187
            'subtotal_with_discount'      => '855.0000',
188
            'base_subtotal_with_discount' => '855.0000',
189
            'is_changed'                  => '1',
190
            'trigger_recollect'           => '0',
191
            'is_persistent'               => '0',
192
            'shipping_address'            => [],
193
            'billing_address'             => [],
194
            'items'                       => [],
195
            'payment'                     => '',
196
            'store_code'                  => $cart->getStore()->getCode(),
197
            'store_storename'             => $cart->getStore()->getName(),
198
            'store_website_id'            => $cart->getStore()->getWebsite()->getOriginId(),
199
            'store_website_code'          => $cart->getStore()->getWebsite()->getCode(),
200
            'store_website_name'          => $cart->getStore()->getWebsite()->getName(),
201
            'customer_group_code'         => 'General',
202
            'customer_group_name'         => 'General',
203
        ];
204
    }
205
206
    public function testCustomerSyncAction()
207
    {
208
        $jobManager = $this->getContainer()->get('akeneo_batch.job_repository')->getJobManager();
209
        $jobManager->beginTransaction();
210
        $newCustomerOrder = $this->getModifiedCustomerOrder($this->customer);
211
212
        $orderIterator = new StubIterator([$newCustomerOrder]);
213
        $customerIterator = new StubIterator([]);
214
215
        $this->soapTransport->expects($this->any())->method('call');
216
        $this->soapTransport->expects($this->once())->method('getOrders')->will($this->returnValue($orderIterator));
217
        $this->soapTransport->expects($this->any())->method('getCustomers')
218
            ->will($this->returnValue($customerIterator));
219
220
        $this->client->request(
221
            'GET',
222
            $this->getUrl('orocrm_magento_orderplace_new_customer_order_sync', ['id' => $this->customer->getId()]),
223
            [],
224
            [],
225
            ['HTTP_X-Requested-With' => 'XMLHttpRequest']
226
        );
227
        $jobManager->rollback();
228
        $jobManager->getConnection()->close();
229
230
        $result = $this->client->getResponse();
231
        $this->assertJsonResponseStatusCodeEquals($result, 200);
232
        $arrayJson = json_decode($result->getContent(), 1);
233
        $this->assertEquals($arrayJson['statusType'], 'success');
234
        $this->assertEquals($arrayJson['message'], 'Data successfully synchronized.');
235
236
        $this->assertEquals(
237
            $arrayJson['url'],
238
            $this->getUrl('orocrm_magento_order_view', ['id' => $this->order->getId()])
239
        );
240
241
        $this->client->request(
242
            'GET',
243
            $this->getUrl('orocrm_magento_customer_view', ['id' => $this->customer->getId()])
244
        );
245
        $result = $this->client->getResponse();
246
        $this->assertHtmlResponseStatusCodeEquals($result, 200);
247
248
        $this->assertContains('General Information', $result->getContent());
249
        $this->assertContains('100000307', $result->getContent());
250
        $this->assertContains('$750', $result->getContent());
251
        $this->assertContains('pending', $result->getContent());
252
        $this->assertContains('$755', $result->getContent());
253
        $this->assertContains('$755', $result->getContent());
254
    }
255
256
    /**
257
     * @param Customer $customer
258
     *
259
     * @return array
260
     */
261
    protected function getModifiedCustomerOrder(Customer $customer)
262
    {
263
        return [
264
            'increment_id'         => '100000307',
265
            'store_id'             => $customer->getStore()->getOriginId(),
266
            'created_at'           => '2014-05-29 16:41:43',
267
            'updated_at'           => '2014-05-29 16:41:43',
268
            'customer_id'          => $customer->getOriginId(),
269
            'tax_amount'           => '0.0000',
270
            'shipping_amount'      => '5.0000',
271
            'discount_amount'      => '0.0000',
272
            'subtotal'             => '750.0000',
273
            'grand_total'          => '755.0000',
274
            'total_qty_ordered'    => '1.0000',
275
            'base_tax_amount'      => '0.0000',
276
            'base_shipping_amount' => '5.0000',
277
            'base_discount_amount' => '0.0000',
278
            'base_subtotal'        => '750.0000',
279
            'base_grand_total'     => '755.0000',
280
            'billing_address_id'   => '603',
281
            'billing_firstname'    => 'asdf',
282
            'billing_lastname'     => 'asdf',
283
            'shipping_address_id'  => '604',
284
            'shipping_firstname'   => 'asdf',
285
            'shipping_lastname'    => 'asdf',
286
            'billing_name'         => 'asdf asdf',
287
            'shipping_name'        => 'asdf asdf',
288
            'store_to_base_rate'   => '1.0000',
289
            'store_to_order_rate'  => '1.0000',
290
            'base_to_global_rate'  => '1.0000',
291
            'base_to_order_rate'   => '1.0000',
292
            'weight'               => '0.3000',
293
            'store_name'           => $customer->getStore()->getName(),
294
            'status'               => 'pending',
295
            'state'                => 'new',
296
            'global_currency_code' => 'USD',
297
            'base_currency_code'   => 'USD',
298
            'store_currency_code'  => 'USD',
299
            'order_currency_code'  => 'USD',
300
            'shipping_method'      => 'flatrate_flatrate',
301
            'shipping_description' => 'Flat Rate - Fixed',
302
            'customer_email'       => '[email protected]',
303
            'customer_firstname'   => 'asdf',
304
            'customer_lastname'    => 'asdf',
305
            'quote_id'             => '100',
306
            'is_virtual'           => '0',
307
            'customer_group_id'    => '1',
308
            'customer_note_notify' => '0',
309
            'customer_is_guest'    => '0',
310
            'email_sent'           => '1',
311
            'order_id'             => '302',
312
            'shipping_address'     => [],
313
            'billing_address'      => [],
314
            'items'                => [],
315
            'payment'              => '',
316
            'status_history'       => [],
317
            'store_code'           => $customer->getStore()->getCode(),
318
            'store_storename'      => $customer->getStore()->getName(),
319
            'store_website_id'     => $customer->getStore()->getWebsite()->getOriginId(),
320
            'store_website_code'   => $customer->getStore()->getWebsite()->getCode(),
321
            'store_website_name'   => $customer->getStore()->getWebsite()->getName(),
322
        ];
323
    }
324
325
    public function testSyncGuestOrderAction()
326
    {
327
        $jobManager = $this->getContainer()->get('akeneo_batch.job_repository')->getJobManager();
328
        $jobManager->beginTransaction();
329
330
        $newCart = $this->getModifiedGuestCartData($this->guestCart);
331
        $cartIterator = new StubIterator([$newCart]);
332
333
        $newCustomerOrder = $this->getModifiedGuestCustomerOrder($this->guestCart);
334
        $orderIterator = new StubIterator([$newCustomerOrder]);
335
        $customerIterator = new StubIterator([]);
336
337
        $this->soapTransport->expects($this->any())->method('call');
338
        $this->soapTransport->expects($this->once())->method('getCarts')->will($this->returnValue($cartIterator));
339
        $this->soapTransport->expects($this->once())->method('getOrders')->will($this->returnValue($orderIterator));
340
        $this->soapTransport->expects($this->any())->method('getCustomers')
341
            ->will($this->returnValue($customerIterator));
342
343
        $this->client->request(
344
            'GET',
345
            $this->getUrl('orocrm_magento_orderplace_new_cart_order_sync', ['id' => $this->guestCart->getId()]),
346
            [],
347
            [],
348
            ['HTTP_X-Requested-With' => 'XMLHttpRequest']
349
        );
350
351
        $result = $this->client->getResponse();
352
        $this->assertJsonResponseStatusCodeEquals($result, 200);
353
        $arrayJson = json_decode($result->getContent(), 1);
354
        $this->assertEquals('success', $arrayJson['statusType']);
355
        $this->assertEquals('Data successfully synchronized.', $arrayJson['message']);
356
        $this->assertEquals(
357
            $arrayJson['url'],
358
            $this->getUrl('orocrm_magento_order_view', ['id' => $this->guestOrder->getId()])
359
        );
360
        $jobManager->rollback();
361
        $jobManager->getConnection()->close();
362
363
        $this->client->request('GET', $this->getUrl('orocrm_magento_cart_view', ['id' => $this->guestCart->getId()]));
364
        $result = $this->client->getResponse();
365
        $this->assertHtmlResponseStatusCodeEquals($result, 200);
366
367
        $resultContent = $result->getContent();
368
        $this->assertContains('Cart Information', $resultContent);
369
        $this->assertContains(self::TEST_NEW_GUEST_EMAIL, $resultContent);
370
371
        $this->assertContains('Customer Information', $resultContent);
372
        $this->assertContains(self::TEST_NEW_GUEST_EMAIL, $resultContent);
373
374
        $this->assertEquals(
375
            $arrayJson['url'],
376
            $this->getUrl('orocrm_magento_order_view', ['id' => $this->guestOrder->getId()])
377
        );
378
        $result = $this->client->getResponse();
379
        $this->assertHtmlResponseStatusCodeEquals($result, 200);
380
381
        $resultContent = $result->getContent();
382
        $this->assertContains('Customer Information', $resultContent);
383
        $this->assertContains(self::TEST_NEW_GUEST_EMAIL, $resultContent);
384
    }
385
386
    /**
387
     * @param Cart $cart
388
     *
389
     * @return array
390
     */
391
    protected function getModifiedGuestCartData(Cart $cart)
392
    {
393
        return [
394
            'entity_id'                   => $cart->getId(),
395
            'store_id'                    => $cart->getStore()->getOriginId(),
396
            'created_at'                  => '2014-04-22 10:41:43',
397
            'updated_at'                  => '2014-05-29 08:52:33',
398
            'is_active'                   => false,
399
            'is_virtual'                  => false,
400
            'is_multi_shipping'           => false,
401
            'items_count'                 => '2',
402
            'items_qty'                   => self::TEST_NEW_ITEMS_QTY,
403
            'orig_order_id'               => '0',
404
            'store_to_base_rate'          => '1.0000',
405
            'store_to_quote_rate'         => '1.0000',
406
            'base_currency_code'          => 'USD',
407
            'store_currency_code'         => 'USD',
408
            'quote_currency_code'         => 'USD',
409
            'grand_total'                 => '855.0000',
410
            'base_grand_total'            => '855.0000',
411
            'customer_tax_class_id'       => '3',
412
            'customer_group_id'           => '0',
413
            'customer_email'              => self::TEST_NEW_GUEST_EMAIL,
414
            'customer_firstname'          => 'Guest firstname',
415
            'customer_lastname'           => 'Guest lastname',
416
            'customer_note_notify'        => '1',
417
            'customer_is_guest'           => '1',
418
            'remote_ip'                   => '82.117.235.210',
419
            'global_currency_code'        => 'USD',
420
            'base_to_global_rate'         => '1.0000',
421
            'base_to_quote_rate'          => '1.0000',
422
            'subtotal'                    => '855.0000',
423
            'base_subtotal'               => '855.0000',
424
            'subtotal_with_discount'      => '855.0000',
425
            'base_subtotal_with_discount' => '855.0000',
426
            'is_changed'                  => '1',
427
            'trigger_recollect'           => '0',
428
            'is_persistent'               => '0',
429
            'shipping_address'            => [],
430
            'billing_address'             => [],
431
            'items'                       => [],
432
            'payment'                     => '',
433
            'store_code'                  => $cart->getStore()->getCode(),
434
            'store_storename'             => $cart->getStore()->getName(),
435
            'store_website_id'            => $cart->getStore()->getWebsite()->getOriginId(),
436
            'store_website_code'          => $cart->getStore()->getWebsite()->getCode(),
437
            'store_website_name'          => $cart->getStore()->getWebsite()->getName(),
438
            'customer_group_code'         => 'NOT LOGGED IN',
439
            'customer_group_name'         => 'NOT LOGGED IN',
440
        ];
441
    }
442
443
    /**
444
     * @param Cart $cart
445
     *
446
     * @return array
447
     */
448
    protected function getModifiedGuestCustomerOrder(Cart $cart)
449
    {
450
        return [
451
            'increment_id'         => '100000308',
452
            'store_id'             => $cart->getStore()->getOriginId(),
453
            'created_at'           => '2014-05-29 16:41:43',
454
            'updated_at'           => '2014-05-29 16:41:43',
455
            'tax_amount'           => '0.0000',
456
            'shipping_amount'      => '5.0000',
457
            'discount_amount'      => '0.0000',
458
            'subtotal'             => '750.0000',
459
            'grand_total'          => '755.0000',
460
            'total_qty_ordered'    => '1.0000',
461
            'base_tax_amount'      => '0.0000',
462
            'base_shipping_amount' => '5.0000',
463
            'base_discount_amount' => '0.0000',
464
            'base_subtotal'        => '750.0000',
465
            'base_grand_total'     => '755.0000',
466
            'billing_address_id'   => '603',
467
            'billing_firstname'    => 'Guest asdf',
468
            'billing_lastname'     => 'Guest asdf',
469
            'shipping_address_id'  => '604',
470
            'shipping_firstname'   => 'Guest asdf',
471
            'shipping_lastname'    => 'Guest asdf',
472
            'billing_name'         => 'Guest asdf asdf',
473
            'shipping_name'        => 'Guest asdf asdf',
474
            'store_to_base_rate'   => '1.0000',
475
            'store_to_order_rate'  => '1.0000',
476
            'base_to_global_rate'  => '1.0000',
477
            'base_to_order_rate'   => '1.0000',
478
            'weight'               => '0.3000',
479
            'store_name'           => $cart->getStore()->getName(),
480
            'status'               => 'pending',
481
            'state'                => 'new',
482
            'global_currency_code' => 'USD',
483
            'base_currency_code'   => 'USD',
484
            'store_currency_code'  => 'USD',
485
            'order_currency_code'  => 'USD',
486
            'shipping_method'      => 'flatrate_flatrate',
487
            'shipping_description' => 'Flat Rate - Fixed',
488
            'customer_email'       => self::TEST_NEW_GUEST_EMAIL,
489
            'customer_firstname'   => 'Guest asdf',
490
            'customer_lastname'    => 'Guest asdf',
491
            'quote_id'             => $cart->getOriginId(),
492
            'is_virtual'           => '0',
493
            'customer_group_id'    => '0',
494
            'customer_note_notify' => '0',
495
            'customer_is_guest'    => '1',
496
            'email_sent'           => '1',
497
            'order_id'             => '303',
498
            'shipping_address'     => [],
499
            'billing_address'      => [],
500
            'items'                => [],
501
            'payment'              => '',
502
            'status_history'       => [],
503
            'store_code'           => $cart->getStore()->getCode(),
504
            'store_storename'      => $cart->getStore()->getName(),
505
            'store_website_id'     => $cart->getStore()->getWebsite()->getOriginId(),
506
            'store_website_code'   => $cart->getStore()->getWebsite()->getCode(),
507
            'store_website_name'   => $cart->getStore()->getWebsite()->getName(),
508
        ];
509
    }
510
}
511