Skip to content
This repository was archived by the owner on Nov 6, 2021. It is now read-only.

Commit e1c6a4b

Browse files
author
Florian Horn
committed
Extended customer fuzzyfier to fuzzyfy quote and order addresses
1 parent d199fb3 commit e1c6a4b

File tree

2 files changed

+235
-7
lines changed

2 files changed

+235
-7
lines changed

Observer/CustomersObserver.php

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
use Magento\Customer\Model\ResourceModel\Customer\Collection as CustomerCollection;
1616
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
1717
use Magento\Customer\Api\CustomerRepositoryInterface;
18+
use Magento\Sales\Api\OrderAddressRepositoryInterface;
19+
use Magento\Sales\Model\ResourceModel\Order\Address\Collection as OrderAddressCollection;
20+
use Magento\Quote\Api\QuoteAddressRepositoryInterface;
21+
use Magento\Quote\Model\QuoteRepository;
22+
use Magento\Framework\Api\SearchCriteriaInterfaceFactory;
1823

1924
class CustomersObserver extends FuzzyfyrObserver
2025
{
@@ -26,18 +31,39 @@ class CustomersObserver extends FuzzyfyrObserver
2631
* @var CustomerRepositoryInterface
2732
*/
2833
protected $customerRepository;
34+
/**
35+
* @var QuoteRepository
36+
*/
37+
protected $quoteRepository;
38+
/**
39+
* @var OrderAddressRepositoryInterface
40+
*/
41+
protected $orderAddressRepository;
42+
/**
43+
* @var SearchCriteriaInterfaceFactory
44+
*/
45+
protected $searchCriteriaFactory;
2946

3047
/**
3148
* CustomersObserver constructor.
3249
* @param CustomerCollectionFactory $customerCollectionFactory
3350
* @param CustomerRepositoryInterface $customerRepository
51+
* @param QuoteRepository $quoteRepository
52+
* @param OrderAddressRepositoryInterface $orderAddressRepository
53+
* @param SearchCriteriaInterfaceFactory $searchCriteriaFactory
3454
*/
3555
public function __construct(
3656
CustomerCollectionFactory $customerCollectionFactory,
37-
CustomerRepositoryInterface $customerRepository
57+
CustomerRepositoryInterface $customerRepository,
58+
QuoteRepository $quoteRepository,
59+
OrderAddressRepositoryInterface $orderAddressRepository,
60+
SearchCriteriaInterfaceFactory $searchCriteriaFactory
3861
) {
3962
$this->customerCollectionFactory = $customerCollectionFactory;
4063
$this->customerRepository = $customerRepository;
64+
$this->quoteRepository = $quoteRepository;
65+
$this->orderAddressRepository = $orderAddressRepository;
66+
$this->searchCriteriaFactory = $searchCriteriaFactory;
4167
}
4268

4369
/**
@@ -54,23 +80,48 @@ public function isValid(Configuration $configuration)
5480
*/
5581
protected function run(Configuration $configuration)
5682
{
83+
/*
84+
* Customer
85+
*/
5786
/** @var CustomerCollection $customerCollection */
5887
$customerCollection = $this->customerCollectionFactory->create();
5988
$customerCollection->load();
6089
foreach ($customerCollection->getItems() as $customer) {
6190
/** @var \Magento\Customer\Model\Customer $customer */
6291
$customerData = $this->customerRepository->getById($customer->getId());
63-
$this->doUpdate($configuration, $customerData);
92+
$this->doUpdateCustomer($configuration, $customerData);
6493
$this->customerRepository->save($customerData);
6594
}
95+
96+
$searchCriteria = $this->searchCriteriaFactory->create();
97+
/*
98+
* Quotes
99+
*/
100+
$quoteCollection = $this->quoteRepository->getList($searchCriteria);
101+
foreach ($quoteCollection->getItems() as $quote) {
102+
$this->doUpdateQuoteAddress($configuration, $quote->getBillingAddress());
103+
$this->quoteRepository->save($quote);
104+
}
105+
106+
/*
107+
* Orders
108+
*/
109+
/** @var OrderAddressCollection $orderAddressCollection */
110+
$orderAddressCollection = $this->orderAddressRepository->getList($searchCriteria);
111+
foreach ($orderAddressCollection->getItems() as $orderAddress) {
112+
$this->doUpdateOrderAddress($configuration, $orderAddress);
113+
$this->orderAddressRepository->save($orderAddress);
114+
}
66115
}
67116

68117
/**
69118
* @param Configuration $configuration
70119
* @param \Magento\Customer\Model\Customer $customer
71120
*/
72-
protected function doUpdate(Configuration $configuration, \Magento\Customer\Api\Data\CustomerInterface $customer)
73-
{
121+
protected function doUpdateCustomer(
122+
Configuration $configuration,
123+
\Magento\Customer\Api\Data\CustomerInterface $customer
124+
) {
74125
$customer->setEmail(
75126
sprintf(
76127
$configuration->getDummyContentEmail(),
@@ -88,4 +139,42 @@ protected function doUpdate(Configuration $configuration, \Magento\Customer\Api\
88139
}
89140
$customer->setAddresses($addresses);
90141
}
142+
143+
/**
144+
* @param Configuration $configuration
145+
* @param \Magento\Quote\Api\Data\AddressInterface $quoteAddress
146+
*/
147+
protected function doUpdateQuoteAddress(
148+
Configuration $configuration,
149+
\Magento\Quote\Api\Data\AddressInterface $quoteAddress
150+
) {
151+
$quoteAddress->setFirstname($configuration->getDummyContentText());
152+
$quoteAddress->setMiddlename($configuration->getDummyContentText());
153+
$quoteAddress->setLastname($configuration->getDummyContentText());
154+
$quoteAddress->setCompany($configuration->getDummyContentText());
155+
$quoteAddress->setEmail($configuration->getDummyContentEmail());
156+
$quoteAddress->setCity($configuration->getDummyContentText());
157+
$quoteAddress->setPostcode($configuration->getDummyContentText());
158+
$quoteAddress->setStreet($configuration->getDummyContentText());
159+
$quoteAddress->setVatId($configuration->getDummyContentText());
160+
}
161+
162+
/**
163+
* @param Configuration $configuration
164+
* @param \Magento\Sales\Api\Data\OrderAddressInterface $orderAddress
165+
*/
166+
protected function doUpdateOrderAddress(
167+
Configuration $configuration,
168+
\Magento\Sales\Api\Data\OrderAddressInterface $orderAddress
169+
) {
170+
$orderAddress->setFirstname($configuration->getDummyContentText());
171+
$orderAddress->setMiddlename($configuration->getDummyContentText());
172+
$orderAddress->setLastname($configuration->getDummyContentText());
173+
$orderAddress->setCompany($configuration->getDummyContentText());
174+
$orderAddress->setEmail($configuration->getDummyContentEmail());
175+
$orderAddress->setCity($configuration->getDummyContentText());
176+
$orderAddress->setPostcode($configuration->getDummyContentText());
177+
$orderAddress->setStreet($configuration->getDummyContentText());
178+
$orderAddress->setVatId($configuration->getDummyContentText());
179+
}
91180
}

Test/Unit/Observer/CustomersObserverTest.php

Lines changed: 142 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Magento\Framework\Event\Observer;
2020
use Magento\Customer\Model\Customer;
2121
use Magento\Customer\Model\ResourceModel\Customer\Collection as CustomerCollection;
22+
use Magento\Quote\Model\QuoteRepository;
23+
use Magento\Sales\Api\OrderAddressRepositoryInterface;
2224
use 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

Comments
 (0)