1
1
<?php
2
2
/**
3
- *
4
3
* Copyright © 2015 Magento. All rights reserved.
5
4
* See COPYING.txt for license details.
6
5
*/
7
6
8
- // @codingStandardsIgnoreFile
9
-
10
7
namespace Magento \Quote \Test \Unit \Model ;
11
8
12
- use \Magento \Quote \Model \QuoteAddressValidator ;
13
-
14
9
class QuoteAddressValidatorTest extends \PHPUnit_Framework_TestCase
15
10
{
16
11
/**
17
- * @var QuoteAddressValidator
12
+ * @var \Magento\Quote\Model\ QuoteAddressValidator
18
13
*/
19
14
protected $ model ;
20
15
@@ -31,7 +26,7 @@ class QuoteAddressValidatorTest extends \PHPUnit_Framework_TestCase
31
26
/**
32
27
* @var \PHPUnit_Framework_MockObject_MockObject
33
28
*/
34
- protected $ customerFactoryMock ;
29
+ protected $ customerRepositoryMock ;
35
30
36
31
/**
37
32
* @var \PHPUnit_Framework_MockObject_MockObject
@@ -41,7 +36,7 @@ class QuoteAddressValidatorTest extends \PHPUnit_Framework_TestCase
41
36
/**
42
37
* @var \PHPUnit_Framework_MockObject_MockObject
43
38
*/
44
- protected $ customerMock ;
39
+ protected $ customerSessionMock ;
45
40
46
41
public function setUp ()
47
42
{
@@ -61,15 +56,20 @@ public function setUp()
61
56
'' ,
62
57
false
63
58
);
64
- $ this ->customerFactoryMock = $ this ->getMock (
65
- '\Magento\Customer\Model\CustomerFactory ' , ['create ' , '__wakeup ' ], [], '' , false );
66
- $ this ->customerMock = $ this ->getMock ('\Magento\Customer\Model\Customer ' , [], [], '' , false );
67
-
59
+ $ this ->customerRepositoryMock = $ this ->getMock (
60
+ '\Magento\Customer\Api\CustomerRepositoryInterface ' ,
61
+ [],
62
+ [],
63
+ '' ,
64
+ false
65
+ );
66
+ $ this ->customerSessionMock = $ this ->getMock ('\Magento\Customer\Model\Session ' , [], [], '' , false );
68
67
$ this ->model = $ this ->objectManager ->getObject (
69
68
'\Magento\Quote\Model\QuoteAddressValidator ' ,
70
69
[
71
70
'addressRepository ' => $ this ->addressRepositoryMock ,
72
- 'customerFactory ' => $ this ->customerFactoryMock
71
+ 'customerRepository ' => $ this ->customerRepositoryMock ,
72
+ 'customerSession ' => $ this ->customerSessionMock
73
73
]
74
74
);
75
75
}
@@ -81,16 +81,12 @@ public function setUp()
81
81
public function testValidateInvalidCustomer ()
82
82
{
83
83
$ customerId = 100 ;
84
-
85
- $ this ->customerFactoryMock ->expects ($ this ->once ())
86
- ->method ('create ' )
87
- ->will ($ this ->returnValue ($ this ->customerMock ));
88
-
89
- $ this ->customerMock ->expects ($ this ->once ())->method ('load ' )->with ($ customerId );
90
- $ this ->customerMock ->expects ($ this ->once ())->method ('getId ' )->will ($ this ->returnValue (null ));
91
-
92
84
$ address = $ this ->getMock ('\Magento\Quote\Api\Data\AddressInterface ' );
85
+ $ customerMock = $ this ->getMock ('\Magento\Customer\Api\Data\CustomerInterface ' );
86
+
93
87
$ address ->expects ($ this ->atLeastOnce ())->method ('getCustomerId ' )->willReturn ($ customerId );
88
+ $ this ->customerRepositoryMock ->expects ($ this ->once ())->method ('getById ' )->with ($ customerId )
89
+ ->willReturn ($ customerMock );
94
90
$ this ->model ->validate ($ address );
95
91
}
96
92
@@ -100,14 +96,13 @@ public function testValidateInvalidCustomer()
100
96
*/
101
97
public function testValidateInvalidAddress ()
102
98
{
103
- $ this ->customerFactoryMock ->expects ($ this ->never ())->method ('create ' );
104
- $ this ->customerMock ->expects ($ this ->never ())->method ('load ' );
99
+ $ address = $ this ->getMock ('\Magento\Quote\Api\Data\AddressInterface ' );
100
+ $ this ->customerRepositoryMock ->expects ($ this ->never ())->method ('getById ' );
101
+ $ address ->expects ($ this ->atLeastOnce ())->method ('getId ' )->willReturn (101 );
105
102
106
103
$ this ->addressRepositoryMock ->expects ($ this ->once ())->method ('getById ' )
107
104
->willThrowException (new \Magento \Framework \Exception \NoSuchEntityException ());
108
105
109
- $ address = $ this ->getMock ('\Magento\Quote\Api\Data\AddressInterface ' );
110
- $ address ->expects ($ this ->atLeastOnce ())->method ('getId ' )->willReturn (101 );
111
106
$ this ->model ->validate ($ address );
112
107
}
113
108
@@ -116,70 +111,76 @@ public function testValidateInvalidAddress()
116
111
*/
117
112
public function testValidateNewAddress ()
118
113
{
119
- $ this ->customerFactoryMock ->expects ($ this ->never ())->method ('create ' );
114
+ $ this ->customerRepositoryMock ->expects ($ this ->never ())->method ('getById ' );
120
115
$ this ->addressRepositoryMock ->expects ($ this ->never ())->method ('getById ' );
121
-
122
116
$ address = $ this ->getMock ('\Magento\Quote\Api\Data\AddressInterface ' );
123
117
$ this ->assertTrue ($ this ->model ->validate ($ address ));
124
118
}
125
119
126
120
/**
127
- * @expectedException \Magento\Framework\Exception\InputException
128
- * @expectedExceptionMessage Address with id 100 belongs to another customer
121
+ * @expectedException \Magento\Framework\Exception\NoSuchEntityException
122
+ * @expectedExceptionMessage Invalid address id 100
129
123
*/
130
124
public function testValidateWithAddressOfOtherCustomer ()
131
125
{
132
126
$ addressCustomer = 100 ;
133
127
$ addressId = 100 ;
134
-
135
128
$ address = $ this ->getMock ('\Magento\Quote\Api\Data\AddressInterface ' );
129
+ $ customerMock = $ this ->getMock ('\Magento\Customer\Api\Data\CustomerInterface ' );
130
+
131
+ $ this ->customerRepositoryMock ->expects ($ this ->once ())->method ('getById ' )->with ($ addressCustomer )
132
+ ->willReturn ($ customerMock );
133
+ $ this ->addressRepositoryMock ->expects ($ this ->once ())->method ('getById ' )->willReturn ($ this ->quoteAddressMock );
134
+ $ customerMock ->expects ($ this ->once ())->method ('getId ' )->willReturn (42 );
136
135
$ address ->expects ($ this ->atLeastOnce ())->method ('getId ' )->willReturn ($ addressId );
137
136
$ address ->expects ($ this ->atLeastOnce ())->method ('getCustomerId ' )->willReturn ($ addressCustomer );
138
137
139
- /** Customer mock */
140
- $ this ->customerFactoryMock ->expects ($ this ->once ())
141
- ->method ('create ' )
142
- ->will ($ this ->returnValue ($ this ->customerMock ));
143
-
144
- $ this ->customerMock ->expects ($ this ->once ())->method ('load ' )->with ($ addressCustomer );
145
- $ this ->customerMock ->expects ($ this ->once ())->method ('getId ' )->will ($ this ->returnValue ($ addressCustomer ));
138
+ $ this ->quoteAddressMock ->expects ($ this ->once ())->method ('getCustomerId ' )->willReturn (42 );
139
+ $ this ->model ->validate ($ address );
140
+ }
146
141
147
- /** Quote address mock */
148
- $ this ->addressRepositoryMock ->expects ($ this ->once ())->method ('getById ' )
149
- ->will ($ this ->returnValue ($ this ->quoteAddressMock ));
142
+ /**
143
+ * @expectedException \Magento\Framework\Exception\NoSuchEntityException
144
+ * @expectedExceptionMessage Invalid address id 42
145
+ */
146
+ public function testValidateWithInvalidCustomerAddressId ()
147
+ {
148
+ $ customerAddressId = 42 ;
149
+ $ address = $ this ->getMock ('\Magento\Quote\Api\Data\AddressInterface ' );
150
+ $ customerAddress = $ this ->getMock ('\Magento\Quote\Api\Data\AddressInterface ' );
151
+ $ customerMock = $ this ->getMock ('\Magento\Customer\Api\Data\CustomerInterface ' );
150
152
151
- $ this ->quoteAddressMock ->expects ($ this ->atLeastOnce ())->method ('getCustomerId ' )
152
- ->will ($ this ->returnValue (10 ));
153
+ $ address ->expects ($ this ->atLeastOnce ())->method ('getCustomerAddressId ' )->willReturn ($ customerAddressId );
154
+ $ this ->customerSessionMock ->expects ($ this ->once ())->method ('getCustomerDataObject ' )->willReturn ($ customerMock );
155
+ $ customerMock ->expects ($ this ->once ())->method ('getAddresses ' )->willReturn ([$ customerAddress ]);
156
+ $ customerAddress ->expects ($ this ->once ())->method ('getId ' )->willReturn (43 );
153
157
154
- /** Validate */
155
158
$ this ->model ->validate ($ address );
156
159
}
157
160
158
161
public function testValidateWithValidAddress ()
159
162
{
160
163
$ addressCustomer = 100 ;
161
164
$ addressId = 100 ;
165
+ $ customerAddressId = 42 ;
162
166
163
167
$ address = $ this ->getMock ('\Magento\Quote\Api\Data\AddressInterface ' );
164
168
$ address ->expects ($ this ->atLeastOnce ())->method ('getId ' )->willReturn ($ addressId );
165
169
$ address ->expects ($ this ->atLeastOnce ())->method ('getCustomerId ' )->willReturn ($ addressCustomer );
170
+ $ address ->expects ($ this ->atLeastOnce ())->method ('getCustomerAddressId ' )->willReturn ($ customerAddressId );
171
+ $ customerMock = $ this ->getMock ('\Magento\Customer\Api\Data\CustomerInterface ' );
172
+ $ customerAddress = $ this ->getMock ('\Magento\Quote\Api\Data\AddressInterface ' );
166
173
167
- /** Customer mock */
168
- $ this ->customerFactoryMock ->expects ($ this ->once ())
169
- ->method ('create ' )
170
- ->will ($ this ->returnValue ($ this ->customerMock ));
174
+ $ this ->customerRepositoryMock ->expects ($ this ->once ())->method ('getById ' )->willReturn ($ customerMock );
175
+ $ customerMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ addressCustomer );
171
176
172
- $ this ->customerMock ->expects ($ this ->once ())->method ('load ' )->with ( $ addressCustomer );
173
- $ this ->customerMock ->expects ($ this ->once ())->method ('getId ' )->will ( $ this -> returnValue ( $ addressCustomer) );
177
+ $ this ->addressRepositoryMock ->expects ($ this ->once ())->method ('getById ' )->willReturn ( $ this -> quoteAddressMock );
178
+ $ this ->quoteAddressMock ->expects ($ this ->any ())->method ('getCustomerId ' )->willReturn ( $ addressCustomer );
174
179
175
- /** Quote address mock */
176
- $ this ->addressRepositoryMock ->expects ($ this ->once ())->method ('getById ' )
177
- ->will ($ this ->returnValue ($ this ->quoteAddressMock ));
178
-
179
- $ this ->quoteAddressMock ->expects ($ this ->any ())->method ('getCustomerId ' )
180
- ->will ($ this ->returnValue ($ addressCustomer ));
180
+ $ this ->customerSessionMock ->expects ($ this ->once ())->method ('getCustomerDataObject ' )->willReturn ($ customerMock );
181
+ $ customerMock ->expects ($ this ->once ())->method ('getAddresses ' )->willReturn ([$ customerAddress ]);
182
+ $ customerAddress ->expects ($ this ->once ())->method ('getId ' )->willReturn (42 );
181
183
182
- /** Validate */
183
- $ this ->model ->validate ($ address );
184
+ $ this ->assertTrue ($ this ->model ->validate ($ address ));
184
185
}
185
186
}
0 commit comments