11
11
use Magento \Checkout \Api \Data \ShippingInformationInterface ;
12
12
use Magento \Checkout \Api \ShippingInformationManagementInterface ;
13
13
use Magento \Checkout \Model \GuestShippingInformationManagement ;
14
- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
14
+ use Magento \Customer \Model \Address ;
15
+ use Magento \Customer \Model \AddressFactory ;
16
+ use Magento \Framework \Exception \InputException ;
17
+ use Magento \Framework \Validator \Factory as ValidatorFactory ;
18
+ use Magento \Framework \Validator \ValidatorInterface ;
19
+ use Magento \Quote \Api \Data \AddressInterface ;
15
20
use Magento \Quote \Model \QuoteIdMask ;
16
21
use Magento \Quote \Model \QuoteIdMaskFactory ;
17
22
use PHPUnit \Framework \MockObject \MockObject ;
18
23
use PHPUnit \Framework \TestCase ;
19
24
25
+ /**
26
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27
+ */
20
28
class GuestShippingInformationManagementTest extends TestCase
21
29
{
22
30
/**
23
- * @var MockObject
31
+ * @var ShippingInformationManagementInterface| MockObject
24
32
*/
25
33
protected $ shippingInformationManagementMock ;
26
34
27
35
/**
28
- * @var MockObject
36
+ * @var QuoteIdMaskFactory| MockObject
29
37
*/
30
38
protected $ quoteIdMaskFactoryMock ;
31
39
40
+ /**
41
+ * @var ValidatorFactory|MockObject
42
+ */
43
+ protected $ validatorFactoryMock ;
44
+
45
+ /**
46
+ * @var AddressFactory|MockObject
47
+ */
48
+ protected $ addressFactoryMock ;
49
+
32
50
/**
33
51
* @var GuestShippingInformationManagement
34
52
*/
35
53
protected $ model ;
36
54
37
55
protected function setUp (): void
38
56
{
39
- $ objectManager = new ObjectManager ($ this );
40
57
$ this ->quoteIdMaskFactoryMock = $ this ->createPartialMock (
41
58
QuoteIdMaskFactory::class,
42
59
['create ' ]
43
60
);
44
61
$ this ->shippingInformationManagementMock = $ this ->createMock (
45
62
ShippingInformationManagementInterface::class
46
63
);
47
-
48
- $ this ->model = $ objectManager -> getObject (
49
- GuestShippingInformationManagement::class,
50
- [
51
- ' quoteIdMaskFactory ' => $ this ->quoteIdMaskFactoryMock ,
52
- ' shippingInformationManagement ' => $ this ->shippingInformationManagementMock
53
- ]
64
+ $ this -> validatorFactoryMock = $ this -> createMock (ValidatorFactory::class);
65
+ $ this ->addressFactoryMock = $ this -> createMock (AddressFactory::class);
66
+ $ this -> model = new GuestShippingInformationManagement (
67
+ $ this -> quoteIdMaskFactoryMock ,
68
+ $ this ->shippingInformationManagementMock ,
69
+ $ this ->validatorFactoryMock ,
70
+ $ this -> addressFactoryMock
54
71
);
55
72
}
56
73
@@ -59,17 +76,34 @@ public function testSaveAddressInformation()
59
76
$ cartId = 'masked_id ' ;
60
77
$ quoteId = '100 ' ;
61
78
$ addressInformationMock = $ this ->getMockForAbstractClass (ShippingInformationInterface::class);
62
-
79
+ $ shippingAddressMock = $ this ->getMockForAbstractClass (AddressInterface::class);
80
+ $ addressInformationMock ->expects ($ this ->once ())
81
+ ->method ('getShippingAddress ' )
82
+ ->willReturn ($ shippingAddressMock );
83
+ $ shippingAddressMock ->expects ($ this ->once ())
84
+ ->method ('getExtensionAttributes ' )
85
+ ->willReturn (null );
86
+ $ customerAddressMock = $ this ->createMock (Address::class);
87
+ $ this ->addressFactoryMock ->expects ($ this ->once ())
88
+ ->method ('create ' )
89
+ ->willReturn ($ customerAddressMock );
90
+ $ validatorMock = $ this ->createMock (ValidatorInterface::class);
91
+ $ this ->validatorFactoryMock ->expects ($ this ->once ())
92
+ ->method ('createValidator ' )
93
+ ->with ('customer_address ' , 'save ' )
94
+ ->willReturn ($ validatorMock );
95
+ $ validatorMock ->expects ($ this ->once ())->method ('isValid ' )->willReturn (true );
63
96
$ quoteIdMaskMock = $ this ->getMockBuilder (QuoteIdMask::class)
64
97
->addMethods (['getQuoteId ' ])
65
98
->onlyMethods (['load ' ])
66
99
->disableOriginalConstructor ()
67
100
->getMock ();
68
101
$ this ->quoteIdMaskFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ quoteIdMaskMock );
69
-
70
- $ quoteIdMaskMock ->expects ($ this ->once ())->method ('load ' )->with ($ cartId , 'masked_id ' )->willReturnSelf ();
102
+ $ quoteIdMaskMock ->expects ($ this ->once ())
103
+ ->method ('load ' )
104
+ ->with ($ cartId , 'masked_id ' )
105
+ ->willReturnSelf ();
71
106
$ quoteIdMaskMock ->expects ($ this ->once ())->method ('getQuoteId ' )->willReturn ($ quoteId );
72
-
73
107
$ paymentInformationMock = $ this ->getMockForAbstractClass (PaymentDetailsInterface::class);
74
108
$ this ->shippingInformationManagementMock ->expects ($ this ->once ())
75
109
->method ('saveAddressInformation ' )
@@ -78,7 +112,39 @@ public function testSaveAddressInformation()
78
112
$ addressInformationMock
79
113
)
80
114
->willReturn ($ paymentInformationMock );
115
+ $ this ->model ->saveAddressInformation ($ cartId , $ addressInformationMock );
116
+ }
81
117
118
+ /**
119
+ * Validate save address information when it is invalid
120
+ *
121
+ * @return void
122
+ * @throws \PHPUnit\Framework\MockObject\Exception
123
+ */
124
+ public function testSaveAddressInformationWithInvalidAddress ()
125
+ {
126
+ $ cartId = 'masked_id ' ;
127
+ $ addressInformationMock = $ this ->getMockForAbstractClass (ShippingInformationInterface::class);
128
+ $ shippingAddressMock = $ this ->getMockForAbstractClass (AddressInterface::class);
129
+ $ addressInformationMock ->expects ($ this ->once ())
130
+ ->method ('getShippingAddress ' )
131
+ ->willReturn ($ shippingAddressMock );
132
+ $ shippingAddressMock ->method ('getExtensionAttributes ' )->willReturn (null );
133
+ $ customerAddressMock = $ this ->createMock (Address::class);
134
+ $ this ->addressFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ customerAddressMock );
135
+ $ validatorMock = $ this ->createMock (ValidatorInterface::class);
136
+ $ this ->validatorFactoryMock ->expects ($ this ->once ())
137
+ ->method ('createValidator ' )
138
+ ->with ('customer_address ' , 'save ' )
139
+ ->willReturn ($ validatorMock );
140
+ $ validatorMock ->expects ($ this ->once ())->method ('isValid ' )->willReturn (false );
141
+ $ validatorMock ->expects ($ this ->once ())
142
+ ->method ('getMessages ' )
143
+ ->willReturn (['First Name is not valid! ' , 'Last Name is not valid! ' ]);
144
+ $ this ->expectException (InputException::class);
145
+ $ this ->expectExceptionMessage (
146
+ 'The shipping address contains invalid data: First Name is not valid!, Last Name is not valid! '
147
+ );
82
148
$ this ->model ->saveAddressInformation ($ cartId , $ addressInformationMock );
83
149
}
84
150
}
0 commit comments