10 use Magento\Customer\Api\Data\CustomerExtensionInterface;
24 private $subscriberFactory;
34 private $objectManager;
39 private $extensionFactoryMock;
44 private $customerExtensionMock;
49 private $subscriberResourceMock;
54 private $customerMock;
58 $this->subscriberFactory = $this->getMockBuilder(\
Magento\Newsletter\Model\SubscriberFactory::class)
59 ->disableOriginalConstructor()
60 ->setMethods([
'create'])
62 $this->subscriber = $this->getMockBuilder(\
Magento\Newsletter\Model\Subscriber::class)
69 'subscribeCustomerById',
70 'unsubscribeCustomerById',
73 )->disableOriginalConstructor()
75 $this->extensionFactoryMock = $this->getMockBuilder(ExtensionAttributesFactory::class)
76 ->disableOriginalConstructor()
77 ->setMethods([
'create'])
79 $this->customerExtensionMock = $this->getMockBuilder(CustomerExtensionInterface::class)
80 ->setMethods([
'getIsSubscribed',
'setIsSubscribed'])
81 ->disableOriginalConstructor()
82 ->getMockForAbstractClass();
83 $this->subscriberResourceMock = $this->getMockBuilder(Subscriber::class)
84 ->disableOriginalConstructor()
86 $this->customerMock = $this->getMockBuilder(CustomerInterface::class)
87 ->setMethods([
'getExtensionAttributes'])
88 ->disableOriginalConstructor()
89 ->getMockForAbstractClass();
90 $this->subscriberFactory->expects($this->any())->method(
'create')->willReturn($this->subscriber);
91 $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
93 $this->plugin = $this->objectManager->getObject(
94 \
Magento\Newsletter\Model\Plugin\CustomerPlugin::class,
96 'subscriberFactory' => $this->subscriberFactory,
97 'extensionFactory' => $this->extensionFactoryMock,
98 'subscriberResource' => $this->subscriberResourceMock,
109 public function testAfterSave($subscriptionOriginalValue, $subscriptionNewValue)
113 $result = $this->createMock(\
Magento\Customer\Api\Data\CustomerInterface::class);
115 $subject = $this->createMock(\
Magento\Customer\Api\CustomerRepositoryInterface::class);
118 $resultExtensionAttributes = $this->getMockBuilder(CustomerExtensionInterface::class)
119 ->setMethods([
'getIsSubscribed',
'setIsSubscribed'])
120 ->getMockForAbstractClass();
122 $result->expects($this->any())->method(
'getExtensionAttributes')->willReturn(
null);
123 $this->extensionFactoryMock->expects($this->any())
125 ->willReturn($resultExtensionAttributes);
126 $result->expects($this->once())
127 ->method(
'setExtensionAttributes')
128 ->with($resultExtensionAttributes)
130 $this->customerMock->expects($this->once())
131 ->method(
'getExtensionAttributes')
132 ->willReturn($this->customerExtensionMock);
133 $resultExtensionAttributes->expects($this->any())
134 ->method(
'getIsSubscribed')
135 ->willReturn($subscriptionOriginalValue);
136 $this->customerExtensionMock->expects($this->any())
137 ->method(
'getIsSubscribed')
138 ->willReturn($subscriptionNewValue);
140 if ($subscriptionOriginalValue !== $subscriptionNewValue) {
141 if ($subscriptionNewValue) {
142 $this->subscriber->expects($this->once())->method(
'subscribeCustomerById')->with(
$customerId);
144 $this->subscriber->expects($this->once())->method(
'unsubscribeCustomerById')->with(
$customerId);
146 $this->subscriber->expects($this->once())->method(
'isSubscribed')->willReturn($subscriptionNewValue);
147 $resultExtensionAttributes->expects($this->once())->method(
'setIsSubscribed')->with($subscriptionNewValue);
150 $this->assertEquals(
$result, $this->plugin->afterSave($subject,
$result, $this->customerMock));
168 $subject = $this->createMock(\
Magento\Customer\Api\CustomerRepositoryInterface::class);
169 $customer = $this->createMock(\
Magento\Customer\Api\Data\CustomerInterface::class);
171 $this->subscriber->expects($this->once())->method(
'loadByEmail')->with(
'[email protected]')->willReturnSelf();
172 $this->subscriber->expects($this->once())->method(
'getId')->willReturn(1);
173 $this->subscriber->expects($this->once())->method(
'delete')->willReturnSelf();
175 $this->assertEquals(
true, $this->plugin->afterDelete($subject,
true,
$customer));
181 $deleteCustomerById =
function () {
184 $subject = $this->createMock(\
Magento\Customer\Api\CustomerRepositoryInterface::class);
185 $customer = $this->createMock(\
Magento\Customer\Api\Data\CustomerInterface::class);
186 $subject->expects($this->once())->method(
'getById')->willReturn(
$customer);
188 $this->subscriber->expects($this->once())->method(
'loadByEmail')->with(
'[email protected]')->willReturnSelf();
189 $this->subscriber->expects($this->once())->method(
'getId')->willReturn(1);
190 $this->subscriber->expects($this->once())->method(
'delete')->willReturnSelf();
192 $this->assertEquals(
true, $this->plugin->aroundDeleteById($subject, $deleteCustomerById,
$customerId));
203 $subscriberStatusKey,
204 $subscriberStatusValue,
207 $subject = $this->createMock(\
Magento\Customer\Api\CustomerRepositoryInterface::class);
208 $subscriber = [$subscriberStatusKey => $subscriberStatusValue];
210 $this->extensionFactoryMock->expects($this->any())
212 ->willReturn($this->customerExtensionMock);
213 $this->customerMock->expects($this->once())
214 ->method(
'setExtensionAttributes')
215 ->with($this->customerExtensionMock)
217 $this->customerMock->expects($this->any())
220 $this->subscriberResourceMock->expects($this->once())
221 ->method(
'loadByCustomerData')
222 ->with($this->customerMock)
223 ->willReturn($subscriber);
224 $this->customerExtensionMock->expects($this->once())->method(
'setIsSubscribed')->with($isSubscribed);
228 $this->plugin->afterGetById($subject, $this->customerMock)
234 $subject = $this->createMock(\
Magento\Customer\Api\CustomerRepositoryInterface::class);
235 $subscriber = [
'subscriber_id' => 1,
'subscriber_status' => 1];
237 $this->customerMock->expects($this->any())
238 ->method(
'getExtensionAttributes')
239 ->willReturn($this->customerExtensionMock);
240 $this->customerExtensionMock->expects($this->any())
241 ->method(
'getIsSubscribed')
243 $this->subscriberResourceMock->expects($this->once())
244 ->method(
'loadByCustomerData')
245 ->with($this->customerMock)
246 ->willReturn($subscriber);
247 $this->customerExtensionMock->expects($this->once())
248 ->method(
'setIsSubscribed')
253 $this->plugin->afterGetById($subject, $this->customerMock)
263 [
'subscriber_status', 1,
true],
264 [
'subscriber_status', 2,
false],
265 [
'subscriber_status', 3,
false],
266 [
'subscriber_status', 4,
false],
testAfterGetByIdSetsIsSubscribedFlagIfItIsNotSet()
testAfterGetByIdCreatesExtensionAttributesIfItIsNotSet( $subscriberStatusKey, $subscriberStatusValue, $isSubscribed)
afterGetByIdDataProvider()