1919use Magento \Framework \Event \Observer ;
2020use Magento \Customer \Model \Customer ;
2121use Magento \Customer \Model \ResourceModel \Customer \Collection as CustomerCollection ;
22+ use Magento \Quote \Model \QuoteRepository ;
23+ use Magento \Sales \Api \OrderAddressRepositoryInterface ;
2224use PHPUnit \Framework \MockObject \MockObject ;
2325
2426/**
@@ -32,6 +34,13 @@ class CustomersObserverTest extends AbstractTest
3234 */
3335 public function stopOnFailedValidationSuccessfully ()
3436 {
37+ $ searchCriteria = $ this ->createMock (\Magento \Framework \Api \SearchCriteriaInterface::class);
38+ $ searchCriteriaInterfaceFactory = $ this ->getSearchCriteriaInterfaceFactory ($ searchCriteria );
39+
40+ $ orderAddressRepository = $ this ->getOrderAddressRepository ();
41+
42+ $ quoteRepository = $ this ->getQuoteRepository ();
43+
3544 $ customerRepository = $ this ->getCustomerRepository ();
3645 $ customerRepository ->expects ($ this ->never ())
3746 ->method ('getById ' );
@@ -51,7 +60,13 @@ public function stopOnFailedValidationSuccessfully()
5160 ->with ('configuration ' )
5261 ->willReturn ($ configuration );
5362
54- $ observer = new CustomersObserver ($ customerCollectionFactory , $ customerRepository );
63+ $ observer = new CustomersObserver (
64+ $ customerCollectionFactory ,
65+ $ customerRepository ,
66+ $ quoteRepository ,
67+ $ orderAddressRepository ,
68+ $ searchCriteriaInterfaceFactory
69+ );
5570
5671 $ observer ->execute ($ eventObserver );
5772 }
@@ -61,6 +76,95 @@ public function stopOnFailedValidationSuccessfully()
6176 */
6277 public function runSuccessfully ()
6378 {
79+ $ searchCriteria = $ this ->createMock (\Magento \Framework \Api \SearchCriteriaInterface::class);
80+ $ searchCriteriaInterfaceFactory = $ this ->getSearchCriteriaInterfaceFactory ($ searchCriteria );
81+
82+ $ quoteAddress = $ this ->createMock (\Magento \Quote \Api \Data \AddressInterface::class);
83+ $ quoteAddress ->expects ($ this ->once ())
84+ ->method ('setFirstname ' )
85+ ->with ('dummy-text ' );
86+ $ quoteAddress ->expects ($ this ->once ())
87+ ->method ('setMiddlename ' )
88+ ->with ('dummy-text ' );
89+ $ quoteAddress ->expects ($ this ->once ())
90+ ->method ('setLastname ' )
91+ ->with ('dummy-text ' );
92+ $ quoteAddress ->expects ($ this ->once ())
93+ ->method ('setCompany ' )
94+ ->with ('dummy-text ' );
95+ $ quoteAddress ->expects ($ this ->once ())
96+ ->method ('setEmail ' )
97+ ->with ('dummy-email ' );
98+ $ quoteAddress ->expects ($ this ->once ())
99+ ->method ('setCity ' )
100+ ->with ('dummy-text ' );
101+ $ quoteAddress ->expects ($ this ->once ())
102+ ->method ('setPostcode ' )
103+ ->with ('dummy-text ' );
104+ $ quoteAddress ->expects ($ this ->once ())
105+ ->method ('setStreet ' )
106+ ->with ('dummy-text ' );
107+ $ quoteAddress ->expects ($ this ->once ())
108+ ->method ('setVatId ' )
109+ ->with ('dummy-text ' );
110+ $ quote = $ this ->createMock (\Magento \Quote \Api \Data \CartInterface::class);
111+ $ quote ->expects ($ this ->once ())
112+ ->method ('getBillingAddress ' )
113+ ->willReturn ($ quoteAddress );
114+ $ searchData = $ this ->createMock (\Magento \Quote \Api \Data \CartSearchResultsInterface::class);
115+ $ searchData ->expects ($ this ->once ())
116+ ->method ('getItems ' )
117+ ->willReturn ([$ quote ]);
118+ $ quoteRepository = $ this ->getQuoteRepository ();
119+ $ quoteRepository ->expects ($ this ->once ())
120+ ->method ('getList ' )
121+ ->with ($ searchCriteria )
122+ ->willReturn ($ searchData );
123+ $ quoteRepository ->expects ($ this ->once ())
124+ ->method ('save ' )
125+ ->with ($ quote );
126+
127+ $ orderAddress = $ this ->createMock (\Magento \Sales \Api \Data \OrderAddressInterface::class);
128+ $ orderAddress ->expects ($ this ->once ())
129+ ->method ('setFirstname ' )
130+ ->with ('dummy-text ' );
131+ $ orderAddress ->expects ($ this ->once ())
132+ ->method ('setMiddlename ' )
133+ ->with ('dummy-text ' );
134+ $ orderAddress ->expects ($ this ->once ())
135+ ->method ('setLastname ' )
136+ ->with ('dummy-text ' );
137+ $ orderAddress ->expects ($ this ->once ())
138+ ->method ('setCompany ' )
139+ ->with ('dummy-text ' );
140+ $ orderAddress ->expects ($ this ->once ())
141+ ->method ('setEmail ' )
142+ ->with ('dummy-email ' );
143+ $ orderAddress ->expects ($ this ->once ())
144+ ->method ('setCity ' )
145+ ->with ('dummy-text ' );
146+ $ orderAddress ->expects ($ this ->once ())
147+ ->method ('setPostcode ' )
148+ ->with ('dummy-text ' );
149+ $ orderAddress ->expects ($ this ->once ())
150+ ->method ('setStreet ' )
151+ ->with ('dummy-text ' );
152+ $ orderAddress ->expects ($ this ->once ())
153+ ->method ('setVatId ' )
154+ ->with ('dummy-text ' );
155+ $ searchData = $ this ->createMock (\Magento \Sales \Api \Data \OrderAddressSearchResultInterface::class);
156+ $ searchData ->expects ($ this ->once ())
157+ ->method ('getItems ' )
158+ ->willReturn ([$ orderAddress ]);
159+ $ orderAddressRepository = $ this ->getOrderAddressRepository ();
160+ $ orderAddressRepository ->expects ($ this ->once ())
161+ ->method ('getList ' )
162+ ->with ($ searchCriteria )
163+ ->willReturn ($ searchData );
164+ $ orderAddressRepository ->expects ($ this ->once ())
165+ ->method ('save ' )
166+ ->with ($ orderAddress );
167+
64168 $ address = $ this ->createMock (AddressInterface::class);
65169 $ address ->expects ($ this ->once ())
66170 ->method ('setStreet ' )
@@ -126,7 +230,13 @@ public function runSuccessfully()
126230 ->willReturn ($ configuration );
127231
128232
129- $ observer = new CustomersObserver ($ customerCollectionFactory , $ customerRepository );
233+ $ observer = new CustomersObserver (
234+ $ customerCollectionFactory ,
235+ $ customerRepository ,
236+ $ quoteRepository ,
237+ $ orderAddressRepository ,
238+ $ searchCriteriaInterfaceFactory
239+ );
130240
131241 $ observer ->execute ($ eventObserver );
132242 }
@@ -155,12 +265,41 @@ private function getCustomerRepository()
155265 return $ this ->createMock (CustomerRepositoryInterface::class);
156266 }
157267
268+ /**
269+ * @return MockObject|QuoteRepository
270+ */
271+ private function getQuoteRepository ()
272+ {
273+ return $ this ->getMockBuilder (QuoteRepository::class)
274+ ->disableOriginalConstructor ()
275+ ->getMock ();
276+ }
277+
278+ /**
279+ * @return MockObject|OrderAddressRepositoryInterface
280+ */
281+ private function getOrderAddressRepository ()
282+ {
283+ return $ this ->getMockBuilder (OrderAddressRepositoryInterface::class)
284+ ->disableOriginalConstructor ()
285+ ->getMock ();
286+ }
287+
158288 /**
159289 * @param MockObject $instance
160290 * @return MockObject|\Magento\Customer\Model\ResourceModel\Customer\CollectionFactory
161291 */
162292 private function getCustomerCollectionFactory (MockObject $ instance = null )
163293 {
164- return $ this ->getFactoryAsMock ('\Magento\Customer\Model\ResourceModel\Customer\Collection ' , $ instance );
294+ return $ this ->getFactoryAsMock (\Magento \Customer \Model \ResourceModel \Customer \Collection::class, $ instance );
295+ }
296+
297+ /**
298+ * @param MockObject $instance
299+ * @return MockObject|\Magento\Framework\Api\SearchCriteriaInterfaceFactory
300+ */
301+ private function getSearchCriteriaInterfaceFactory (MockObject $ instance = null )
302+ {
303+ return $ this ->getFactoryAsMock (\Magento \Framework \Api \SearchCriteriaInterface::class, $ instance );
165304 }
166305}
0 commit comments