Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerPluginTest.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Customer\Api\Data\CustomerExtensionInterface;
13 
14 class CustomerPluginTest extends \PHPUnit\Framework\TestCase
15 {
19  private $plugin;
20 
24  private $subscriberFactory;
25 
29  private $subscriber;
30 
34  private $objectManager;
35 
39  private $extensionFactoryMock;
40 
44  private $customerExtensionMock;
45 
49  private $subscriberResourceMock;
50 
54  private $customerMock;
55 
56  protected function setUp()
57  {
58  $this->subscriberFactory = $this->getMockBuilder(\Magento\Newsletter\Model\SubscriberFactory::class)
59  ->disableOriginalConstructor()
60  ->setMethods(['create'])
61  ->getMock();
62  $this->subscriber = $this->getMockBuilder(\Magento\Newsletter\Model\Subscriber::class)
63  ->setMethods(
64  [
65  'loadByEmail',
66  'getId',
67  'delete',
68  'updateSubscription',
69  'subscribeCustomerById',
70  'unsubscribeCustomerById',
71  'isSubscribed',
72  ]
73  )->disableOriginalConstructor()
74  ->getMock();
75  $this->extensionFactoryMock = $this->getMockBuilder(ExtensionAttributesFactory::class)
76  ->disableOriginalConstructor()
77  ->setMethods(['create'])
78  ->getMock();
79  $this->customerExtensionMock = $this->getMockBuilder(CustomerExtensionInterface::class)
80  ->setMethods(['getIsSubscribed', 'setIsSubscribed'])
81  ->disableOriginalConstructor()
82  ->getMockForAbstractClass();
83  $this->subscriberResourceMock = $this->getMockBuilder(Subscriber::class)
84  ->disableOriginalConstructor()
85  ->getMock();
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);
92 
93  $this->plugin = $this->objectManager->getObject(
94  \Magento\Newsletter\Model\Plugin\CustomerPlugin::class,
95  [
96  'subscriberFactory' => $this->subscriberFactory,
97  'extensionFactory' => $this->extensionFactoryMock,
98  'subscriberResource' => $this->subscriberResourceMock,
99  ]
100  );
101  }
102 
109  public function testAfterSave($subscriptionOriginalValue, $subscriptionNewValue)
110  {
111  $customerId = 1;
113  $result = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
115  $subject = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
116 
118  $resultExtensionAttributes = $this->getMockBuilder(CustomerExtensionInterface::class)
119  ->setMethods(['getIsSubscribed', 'setIsSubscribed'])
120  ->getMockForAbstractClass();
121  $result->expects($this->atLeastOnce())->method('getId')->willReturn($customerId);
122  $result->expects($this->any())->method('getExtensionAttributes')->willReturn(null);
123  $this->extensionFactoryMock->expects($this->any())
124  ->method('create')
125  ->willReturn($resultExtensionAttributes);
126  $result->expects($this->once())
127  ->method('setExtensionAttributes')
128  ->with($resultExtensionAttributes)
129  ->willReturnSelf();
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);
139 
140  if ($subscriptionOriginalValue !== $subscriptionNewValue) {
141  if ($subscriptionNewValue) {
142  $this->subscriber->expects($this->once())->method('subscribeCustomerById')->with($customerId);
143  } else {
144  $this->subscriber->expects($this->once())->method('unsubscribeCustomerById')->with($customerId);
145  }
146  $this->subscriber->expects($this->once())->method('isSubscribed')->willReturn($subscriptionNewValue);
147  $resultExtensionAttributes->expects($this->once())->method('setIsSubscribed')->with($subscriptionNewValue);
148  }
149 
150  $this->assertEquals($result, $this->plugin->afterSave($subject, $result, $this->customerMock));
151  }
152 
156  public function afterSaveDataProvider()
157  {
158  return [
159  [true, true],
160  [false, false],
161  [true, false],
162  [false, true],
163  ];
164  }
165 
166  public function testAfterDelete()
167  {
168  $subject = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
169  $customer = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
170  $customer->expects($this->once())->method('getEmail')->willReturn('[email protected]');
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();
174 
175  $this->assertEquals(true, $this->plugin->afterDelete($subject, true, $customer));
176  }
177 
178  public function testAroundDeleteById()
179  {
180  $customerId = 1;
181  $deleteCustomerById = function () {
182  return true;
183  };
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);
187  $customer->expects($this->once())->method('getEmail')->willReturn('[email protected]');
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();
191 
192  $this->assertEquals(true, $this->plugin->aroundDeleteById($subject, $deleteCustomerById, $customerId));
193  }
194 
203  $subscriberStatusKey,
204  $subscriberStatusValue,
205  $isSubscribed
206  ) {
207  $subject = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
208  $subscriber = [$subscriberStatusKey => $subscriberStatusValue];
209 
210  $this->extensionFactoryMock->expects($this->any())
211  ->method('create')
212  ->willReturn($this->customerExtensionMock);
213  $this->customerMock->expects($this->once())
214  ->method('setExtensionAttributes')
215  ->with($this->customerExtensionMock)
216  ->willReturnSelf();
217  $this->customerMock->expects($this->any())
218  ->method('getId')
219  ->willReturn(1);
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);
225 
226  $this->assertEquals(
227  $this->customerMock,
228  $this->plugin->afterGetById($subject, $this->customerMock)
229  );
230  }
231 
233  {
234  $subject = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
235  $subscriber = ['subscriber_id' => 1, 'subscriber_status' => 1];
236 
237  $this->customerMock->expects($this->any())
238  ->method('getExtensionAttributes')
239  ->willReturn($this->customerExtensionMock);
240  $this->customerExtensionMock->expects($this->any())
241  ->method('getIsSubscribed')
242  ->willReturn(null);
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')
249  ->willReturnSelf();
250 
251  $this->assertEquals(
252  $this->customerMock,
253  $this->plugin->afterGetById($subject, $this->customerMock)
254  );
255  }
256 
260  public function afterGetByIdDataProvider()
261  {
262  return [
263  ['subscriber_status', 1, true],
264  ['subscriber_status', 2, false],
265  ['subscriber_status', 3, false],
266  ['subscriber_status', 4, false],
267  [null, null, false],
268  ];
269  }
270 }
$customer
Definition: customers.php:11
testAfterGetByIdCreatesExtensionAttributesIfItIsNotSet( $subscriberStatusKey, $subscriberStatusValue, $isSubscribed)