5
5
*/
6
6
namespace Magento \Sales \Model \AdminOrder ;
7
7
8
+ use Magento \Sales \Api \OrderManagementInterface ;
8
9
use Magento \TestFramework \Helper \Bootstrap ;
9
10
use Magento \Sales \Model \Order ;
10
11
use Magento \Framework \Registry ;
12
+ use Magento \Framework \Message \ManagerInterface ;
13
+ use Magento \Backend \Model \Session \Quote ;
14
+ use Magento \Wishlist \Model \Wishlist ;
15
+ use Magento \Quote \Model \Quote \Address ;
16
+ use Magento \Customer \Model \Customer ;
17
+ use Magento \Catalog \Model \Product ;
18
+ use Magento \Quote \Model \Quote \Address \Rate ;
19
+ use Magento \Customer \Api \CustomerRepositoryInterface ;
20
+ use Magento \Customer \Api \AddressRepositoryInterface ;
11
21
12
22
/**
13
23
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -26,9 +36,9 @@ class CreateTest extends \PHPUnit_Framework_TestCase
26
36
protected function setUp ()
27
37
{
28
38
parent ::setUp ();
29
- $ this ->_messageManager = Bootstrap::getObjectManager ()->get (' Magento\Framework\Message\ ManagerInterface' );
39
+ $ this ->_messageManager = Bootstrap::getObjectManager ()->get (ManagerInterface::class );
30
40
$ this ->_model = Bootstrap::getObjectManager ()->create (
31
- ' Magento\Sales\Model\AdminOrder\ Create' ,
41
+ Create::class ,
32
42
['messageManager ' => $ this ->_messageManager ]
33
43
);
34
44
}
@@ -40,13 +50,13 @@ protected function setUp()
40
50
public function testInitFromOrderShippingAddressSameAsBillingWhenEmpty ()
41
51
{
42
52
/** @var $order \Magento\Sales\Model\Order */
43
- $ order = Bootstrap::getObjectManager ()->create (' Magento\Sales\Model\ Order' );
53
+ $ order = Bootstrap::getObjectManager ()->create (Order::class );
44
54
$ order ->loadByIncrementId ('100000001 ' );
45
55
$ this ->assertNull ($ order ->getShippingAddress ());
46
56
47
57
/** @var $objectManager \Magento\TestFramework\ObjectManager */
48
58
$ objectManager = Bootstrap::getObjectManager ();
49
- $ objectManager ->get (' Magento\Framework\ Registry' )->unregister ('rule_data ' );
59
+ $ objectManager ->get (Registry::class )->unregister ('rule_data ' );
50
60
$ this ->_model ->initFromOrder ($ order );
51
61
52
62
$ this ->assertNull ($ order ->getShippingAddress ());
@@ -60,14 +70,14 @@ public function testInitFromOrderShippingAddressSameAsBillingWhenEmpty()
60
70
public function testInitFromOrderShippingAddressSameAsBillingWhenSame ()
61
71
{
62
72
/** @var $order \Magento\Sales\Model\Order */
63
- $ order = Bootstrap::getObjectManager ()->create (' Magento\Sales\Model\ Order' );
73
+ $ order = Bootstrap::getObjectManager ()->create (Order::class );
64
74
$ order ->loadByIncrementId ('100000001 ' );
65
75
66
76
$ this ->assertNull ($ order ->getShippingAddress ()->getSameAsBilling ());
67
77
68
78
/** @var $objectManager \Magento\TestFramework\ObjectManager */
69
79
$ objectManager = Bootstrap::getObjectManager ();
70
- $ objectManager ->get (' Magento\Framework\ Registry' )->unregister ('rule_data ' );
80
+ $ objectManager ->get (Registry::class )->unregister ('rule_data ' );
71
81
$ this ->_model ->initFromOrder ($ order );
72
82
73
83
$ this ->assertTrue ($ order ->getShippingAddress ()->getSameAsBilling ());
@@ -84,12 +94,12 @@ public function testInitFromOrderShippingAddressSameAsBillingWhenDifferent()
84
94
$ objectManager = Bootstrap::getObjectManager ();
85
95
86
96
/** @var $order \Magento\Sales\Model\Order */
87
- $ order = $ objectManager ->create (' Magento\Sales\Model\ Order' );
97
+ $ order = $ objectManager ->create (Order::class );
88
98
$ order ->loadByIncrementId ('100000002 ' );
89
99
90
100
$ this ->assertNull ($ order ->getShippingAddress ()->getSameAsBilling ());
91
101
92
- $ objectManager ->get (' Magento\Framework\ Registry' )->unregister ('rule_data ' );
102
+ $ objectManager ->get (Registry::class )->unregister ('rule_data ' );
93
103
$ this ->_model ->initFromOrder ($ order );
94
104
95
105
$ this ->assertFalse ($ order ->getShippingAddress ()->getSameAsBilling ());
@@ -104,7 +114,7 @@ public function testInitFromOrderCcInformationDeleted()
104
114
$ objectManager = Bootstrap::getObjectManager ();
105
115
106
116
/** @var $order \Magento\Sales\Model\Order */
107
- $ order = $ objectManager ->create (' Magento\Sales\Model\ Order' );
117
+ $ order = $ objectManager ->create (Order::class );
108
118
$ order ->loadByIncrementId ('100000001 ' );
109
119
110
120
$ payment = $ order ->getPayment ();
@@ -113,7 +123,7 @@ public function testInitFromOrderCcInformationDeleted()
113
123
$ this ->assertEquals ('AE ' , $ payment ->getCcType ());
114
124
$ this ->assertEquals ('0005 ' , $ payment ->getCcLast4 ());
115
125
116
- $ objectManager ->get (' Magento\Framework\ Registry' )->unregister ('rule_data ' );
126
+ $ objectManager ->get (Registry::class )->unregister ('rule_data ' );
117
127
$ payment = $ this ->_model ->initFromOrder ($ order )->getQuote ()->getPayment ();
118
128
119
129
$ this ->assertNull ($ payment ->getCcExpMonth ());
@@ -153,8 +163,8 @@ public function testInitFromOrderWithEmptyPaymentDetails()
153
163
*/
154
164
public function testGetCustomerWishlistNoCustomerId ()
155
165
{
156
- /** @var \Magento\Backend\Model\Session\ Quote $session */
157
- $ session = Bootstrap::getObjectManager ()->create (' Magento\Backend\Model\Session\ Quote' );
166
+ /** @var Quote $session */
167
+ $ session = Bootstrap::getObjectManager ()->create (Quote::class );
158
168
$ session ->setCustomerId (null );
159
169
$ this ->assertFalse (
160
170
$ this ->_model ->getCustomerWishlist (true ),
@@ -172,15 +182,15 @@ public function testGetCustomerWishlist()
172
182
{
173
183
$ customerIdFromFixture = 1 ;
174
184
$ productIdFromFixture = 1 ;
175
- /** @var \Magento\Backend\Model\Session\ Quote $session */
176
- $ session = Bootstrap::getObjectManager ()->create (' Magento\Backend\Model\Session\ Quote' );
185
+ /** @var Quote $session */
186
+ $ session = Bootstrap::getObjectManager ()->create (Quote::class );
177
187
$ session ->setCustomerId ($ customerIdFromFixture );
178
188
179
189
/** Test new wishlist creation for the customer specified above */
180
- /** @var \Magento\Wishlist\Model\ Wishlist $wishlist */
190
+ /** @var Wishlist $wishlist */
181
191
$ wishlist = $ this ->_model ->getCustomerWishlist (true );
182
192
$ this ->assertInstanceOf (
183
- ' Magento\ Wishlist\Model\Wishlist ' ,
193
+ Wishlist::class ,
184
194
$ wishlist ,
185
195
'New Wish List is expected to be created if existing Customer does not have one yet. '
186
196
);
@@ -218,7 +228,7 @@ public function testSetBillingAddress()
218
228
/** Validate data before creating address object */
219
229
$ this ->_model ->setIsValidate (true )->setBillingAddress ($ addressData );
220
230
$ this ->assertInstanceOf (
221
- ' Magento\Quote\Model\Quote\ Address' ,
231
+ Address::class ,
222
232
$ this ->_model ->getBillingAddress (),
223
233
'Billing address object was not created. '
224
234
);
@@ -250,8 +260,8 @@ public function testSetBillingAddress()
250
260
public function testSetBillingAddressValidationErrors ()
251
261
{
252
262
$ customerIdFromFixture = 1 ;
253
- /** @var \Magento\Backend\Model\Session\ Quote $session */
254
- $ session = Bootstrap::getObjectManager ()->create (' Magento\Backend\Model\Session\ Quote' );
263
+ /** @var Quote $session */
264
+ $ session = Bootstrap::getObjectManager ()->create (Quote::class );
255
265
$ session ->setCustomerId ($ customerIdFromFixture );
256
266
$ invalidAddressData = array_merge ($ this ->_getValidAddressData (), ['firstname ' => '' , 'lastname ' => '' ]);
257
267
/**
@@ -319,8 +329,8 @@ public function testCreateOrderNewCustomerDifferentAddresses()
319
329
);
320
330
$ order = $ this ->_model ->createOrder ();
321
331
$ this ->_verifyCreatedOrder ($ order , $ shippingMethod );
322
- /** @var \Magento\Customer\Model\ Customer $customer */
323
- $ customer = Bootstrap::getObjectManager ()->create (' Magento\ Customer\Model\Customer ' );
332
+ /** @var Customer $customer */
333
+ $ customer = Bootstrap::getObjectManager ()->create (Customer::class );
324
334
$ customer ->load ($ order ->getCustomerId ());
325
335
$ this ->assertEquals (
326
336
$ firstNameForShippingAddress ,
@@ -450,6 +460,102 @@ public function testCreateOrderExistingCustomer()
450
460
$ this ->_verifyCreatedOrder ($ order , $ shippingMethod );
451
461
}
452
462
463
+ /**
464
+ * Tests order creation with new customer after failed first place order action.
465
+ *
466
+ * @magentoDataFixture Magento/Catalog/_files/product_simple.php
467
+ * @magentoDbIsolation enabled
468
+ * @magentoAppIsolation enabled
469
+ * @dataProvider createOrderNewCustomerWithFailedFirstPlaceOrderActionDataProvider
470
+ * @param string $customerEmailFirstAttempt
471
+ * @param string $customerEmailSecondAttempt
472
+ */
473
+ public function testCreateOrderNewCustomerWithFailedFirstPlaceOrderAction (
474
+ $ customerEmailFirstAttempt ,
475
+ $ customerEmailSecondAttempt
476
+ ) {
477
+ $ productIdFromFixture = 1 ;
478
+ $ shippingMethod = 'freeshipping_freeshipping ' ;
479
+ $ paymentMethod = 'checkmo ' ;
480
+ $ shippingAddressAsBilling = 1 ;
481
+ $ customerEmail = $ customerEmailFirstAttempt ;
482
+ $ orderData = [
483
+ 'currency ' => 'USD ' ,
484
+ 'account ' => [
485
+ 'group_id ' => '1 ' ,
486
+ 'email ' => $ customerEmail
487
+ ],
488
+ 'billing_address ' => array_merge (
489
+ $ this ->_getValidAddressData (),
490
+ ['save_in_address_book ' => '1 ' ]
491
+ ),
492
+ 'shipping_method ' => $ shippingMethod ,
493
+ 'comment ' => [
494
+ 'customer_note ' => ''
495
+ ],
496
+ 'send_confirmation ' => false ,
497
+ ];
498
+ $ paymentData = ['method ' => $ paymentMethod ];
499
+
500
+ $ this ->_preparePreconditionsForCreateOrder (
501
+ $ productIdFromFixture ,
502
+ $ customerEmail ,
503
+ $ shippingMethod ,
504
+ $ shippingAddressAsBilling ,
505
+ $ paymentData ,
506
+ $ orderData ,
507
+ $ paymentMethod
508
+ );
509
+
510
+ // Emulates failing place order action
511
+ $ orderManagement = $ this ->getMockForAbstractClass (OrderManagementInterface::class);
512
+ $ orderManagement ->method ('place ' )
513
+ ->willThrowException (new \Exception ('Can \'t place order ' ));
514
+ Bootstrap::getObjectManager ()->addSharedInstance ($ orderManagement , OrderManagementInterface::class);
515
+ try {
516
+ $ this ->_model ->createOrder ();
517
+ } catch (\Exception $ e ) {
518
+ Bootstrap::getObjectManager ()->removeSharedInstance (OrderManagementInterface::class);
519
+ }
520
+
521
+ $ customerEmail = $ customerEmailSecondAttempt ?: $ this ->_model ->getQuote ()->getCustomer ()->getEmail ();
522
+ $ orderData ['account ' ]['email ' ] = $ customerEmailSecondAttempt ;
523
+
524
+ $ this ->_preparePreconditionsForCreateOrder (
525
+ $ productIdFromFixture ,
526
+ $ customerEmail ,
527
+ $ shippingMethod ,
528
+ $ shippingAddressAsBilling ,
529
+ $ paymentData ,
530
+ $ orderData ,
531
+ $ paymentMethod
532
+ );
533
+
534
+ $ order = $ this ->_model ->createOrder ();
535
+ $ this ->_verifyCreatedOrder ($ order , $ shippingMethod );
536
+ }
537
+
538
+ /**
539
+ * Email before and after failed first place order action.
540
+ *
541
+ * @case #1 Is the same.
542
+ * @case #2 Is empty.
543
+ * @case #3 Filled after failed first place order action.
544
+ * @case #4 Empty after failed first place order action.
545
+ * @case #5 Changed after failed first place order action.
546
+ * @return array
547
+ */
548
+ public function createOrderNewCustomerWithFailedFirstPlaceOrderActionDataProvider ()
549
+ {
550
+ return [
551
+ 1 => ['customer@email.com ' , 'customer@email.com ' ],
552
+ 2 => ['' , '' ],
553
+ 3 => ['' , 'customer@email.com ' ],
554
+ 4 => ['customer@email.com ' , '' ],
555
+ 5 => ['customer@email.com ' , 'changed_customer@email.com ' ],
556
+ ];
557
+ }
558
+
453
559
/**
454
560
* @magentoAppIsolation enabled
455
561
* @magentoDataFixture Magento/Sales/_files/quote.php
@@ -460,11 +566,11 @@ public function testGetCustomerCartExistingCart()
460
566
$ fixtureCustomerId = 1 ;
461
567
462
568
/** Preconditions */
463
- /** @var \Magento\Backend\Model\Session\ Quote $session */
464
- $ session = Bootstrap::getObjectManager ()->create (' Magento\Backend\Model\Session\ Quote' );
569
+ /** @var Quote $session */
570
+ $ session = Bootstrap::getObjectManager ()->create (Quote::class );
465
571
$ session ->setCustomerId ($ fixtureCustomerId );
466
572
/** @var $quoteFixture \Magento\Quote\Model\Quote */
467
- $ quoteFixture = Bootstrap::getObjectManager ()->create (' Magento\Quote\Model\Quote ' );
573
+ $ quoteFixture = Bootstrap::getObjectManager ()->create (\ Magento \Quote \Model \Quote::class );
468
574
$ quoteFixture ->load ('test01 ' , 'reserved_order_id ' );
469
575
$ quoteFixture ->setCustomerIsGuest (false )->setCustomerId ($ fixtureCustomerId )->save ();
470
576
@@ -488,8 +594,8 @@ public function testGetCustomerCartNewCart()
488
594
$ customerEmailFromFixture = 'customer@example.com ' ;
489
595
490
596
/** Preconditions */
491
- /** @var \Magento\Backend\Model\Session\ Quote $session */
492
- $ session = Bootstrap::getObjectManager ()->create (' Magento\Backend\Model\Session\ Quote' );
597
+ /** @var Quote $session */
598
+ $ session = Bootstrap::getObjectManager ()->create (Quote::class );
493
599
$ session ->setCustomerId ($ customerIdFromFixture );
494
600
495
601
/** SUT execution */
@@ -525,19 +631,19 @@ protected function _preparePreconditionsForCreateOrder(
525
631
$ customerIdFromFixture = null
526
632
) {
527
633
/** Disable product options */
528
- /** @var \Magento\Catalog\Model\ Product $product */
529
- $ product = Bootstrap::getObjectManager ()->create (' Magento\Catalog\Model\ Product' );
634
+ /** @var Product $product */
635
+ $ product = Bootstrap::getObjectManager ()->create (Product::class );
530
636
$ product ->load ($ productIdFromFixture )->setHasOptions (false )->save ();
531
637
532
638
/** Set current customer */
533
- /** @var \Magento\Backend\Model\Session\ Quote $session */
534
- $ session = Bootstrap::getObjectManager ()->get (' Magento\Backend\Model\Session\ Quote' );
639
+ /** @var Quote $session */
640
+ $ session = Bootstrap::getObjectManager ()->get (Quote::class );
535
641
if ($ customerIdFromFixture !== null ) {
536
642
$ session ->setCustomerId ($ customerIdFromFixture );
537
643
538
644
/** Unset fake IDs for default billing and shipping customer addresses */
539
- /** @var \Magento\Customer\Model\ Customer $customer */
540
- $ customer = Bootstrap::getObjectManager ()->create (' Magento\ Customer\Model\Customer ' );
645
+ /** @var Customer $customer */
646
+ $ customer = Bootstrap::getObjectManager ()->create (Customer::class );
541
647
$ customer ->load ($ customerIdFromFixture )->setDefaultBilling (null )->setDefaultShipping (null )->save ();
542
648
} else {
543
649
/**
@@ -548,8 +654,8 @@ protected function _preparePreconditionsForCreateOrder(
548
654
}
549
655
550
656
/** Emulate availability of shipping method (all are disabled by default) */
551
- /** @var $rate \Magento\Quote\Model\Quote\Address\ Rate */
552
- $ rate = Bootstrap::getObjectManager ()->create (' Magento\Quote\Model\Quote\Address\ Rate' );
657
+ /** @var $rate Rate */
658
+ $ rate = Bootstrap::getObjectManager ()->create (Rate::class );
553
659
$ rate ->setCode ($ shippingMethod );
554
660
$ this ->_model ->getQuote ()->getShippingAddress ()->addShippingRate ($ rate );
555
661
@@ -657,11 +763,11 @@ private function getCustomerById($id)
657
763
}
658
764
659
765
/**
660
- * @return \Magento\Customer\Api\ CustomerRepositoryInterface
766
+ * @return CustomerRepositoryInterface
661
767
*/
662
768
private function getCustomerRepository ()
663
769
{
664
- return Bootstrap::getObjectManager ()->create (' Magento\Customer\Api\ CustomerRepositoryInterface' );
770
+ return Bootstrap::getObjectManager ()->create (CustomerRepositoryInterface::class );
665
771
}
666
772
667
773
/**
@@ -674,11 +780,11 @@ private function getAddressById($id)
674
780
}
675
781
676
782
/**
677
- * @return \Magento\Customer\Api\ AddressRepositoryInterface
783
+ * @return AddressRepositoryInterface
678
784
*/
679
785
private function getAddressRepository ()
680
786
{
681
- /** @var \Magento\Customer\Api\ AddressRepositoryInterface $addressRepository */
682
- return Bootstrap::getObjectManager ()->create (' Magento\Customer\Api\ AddressRepositoryInterface' );
787
+ /** @var AddressRepositoryInterface $addressRepository */
788
+ return Bootstrap::getObjectManager ()->create (AddressRepositoryInterface::class );
683
789
}
684
790
}
0 commit comments