CustomerPluginTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Newsletter\Test\Unit\Model\Plugin;
  7. use Magento\Customer\Api\Data\CustomerInterface;
  8. use Magento\Customer\Model\ResourceModel\CustomerRepository;
  9. use Magento\Customer\Api\Data\CustomerExtensionInterface;
  10. use Magento\Framework\Api\ExtensionAttributesFactory;
  11. use Magento\Newsletter\Model\ResourceModel\Subscriber;
  12. class CustomerPluginTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Newsletter\Model\Plugin\CustomerPlugin
  16. */
  17. private $plugin;
  18. /**
  19. * @var \Magento\Newsletter\Model\SubscriberFactory|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $subscriberFactory;
  22. /**
  23. * @var \Magento\Newsletter\Model\Subscriber|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $subscriber;
  26. /**
  27. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  28. */
  29. private $objectManager;
  30. /**
  31. * @var ExtensionAttributesFactory|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $extensionFactoryMock;
  34. /**
  35. * @var CustomerExtensionInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $customerExtensionMock;
  38. /**
  39. * @var Subscriber|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $subscriberResourceMock;
  42. /**
  43. * @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $customerMock;
  46. protected function setUp()
  47. {
  48. $this->subscriberFactory = $this->getMockBuilder(\Magento\Newsletter\Model\SubscriberFactory::class)
  49. ->disableOriginalConstructor()
  50. ->setMethods(['create'])
  51. ->getMock();
  52. $this->subscriber = $this->getMockBuilder(\Magento\Newsletter\Model\Subscriber::class)
  53. ->setMethods(
  54. [
  55. 'loadByEmail',
  56. 'getId',
  57. 'delete',
  58. 'updateSubscription',
  59. 'subscribeCustomerById',
  60. 'unsubscribeCustomerById',
  61. 'isSubscribed',
  62. ]
  63. )->disableOriginalConstructor()
  64. ->getMock();
  65. $this->extensionFactoryMock = $this->getMockBuilder(ExtensionAttributesFactory::class)
  66. ->disableOriginalConstructor()
  67. ->setMethods(['create'])
  68. ->getMock();
  69. $this->customerExtensionMock = $this->getMockBuilder(CustomerExtensionInterface::class)
  70. ->setMethods(['getIsSubscribed', 'setIsSubscribed'])
  71. ->disableOriginalConstructor()
  72. ->getMockForAbstractClass();
  73. $this->subscriberResourceMock = $this->getMockBuilder(Subscriber::class)
  74. ->disableOriginalConstructor()
  75. ->getMock();
  76. $this->customerMock = $this->getMockBuilder(CustomerInterface::class)
  77. ->setMethods(['getExtensionAttributes'])
  78. ->disableOriginalConstructor()
  79. ->getMockForAbstractClass();
  80. $this->subscriberFactory->expects($this->any())->method('create')->willReturn($this->subscriber);
  81. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  82. $this->plugin = $this->objectManager->getObject(
  83. \Magento\Newsletter\Model\Plugin\CustomerPlugin::class,
  84. [
  85. 'subscriberFactory' => $this->subscriberFactory,
  86. 'extensionFactory' => $this->extensionFactoryMock,
  87. 'subscriberResource' => $this->subscriberResourceMock,
  88. ]
  89. );
  90. }
  91. /**
  92. * @param bool $subscriptionOriginalValue
  93. * @param bool $subscriptionNewValue
  94. * @dataProvider afterSaveDataProvider
  95. * @return void
  96. */
  97. public function testAfterSave($subscriptionOriginalValue, $subscriptionNewValue)
  98. {
  99. $customerId = 1;
  100. /** @var CustomerInterface | \PHPUnit_Framework_MockObject_MockObject $result */
  101. $result = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  102. /** @var CustomerRepository | \PHPUnit_Framework_MockObject_MockObject $subject */
  103. $subject = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
  104. /** @var CustomerExtensionInterface|\PHPUnit_Framework_MockObject_MockObject $resultExtensionAttributes */
  105. $resultExtensionAttributes = $this->getMockBuilder(CustomerExtensionInterface::class)
  106. ->setMethods(['getIsSubscribed', 'setIsSubscribed'])
  107. ->getMockForAbstractClass();
  108. $result->expects($this->atLeastOnce())->method('getId')->willReturn($customerId);
  109. $result->expects($this->any())->method('getExtensionAttributes')->willReturn(null);
  110. $this->extensionFactoryMock->expects($this->any())
  111. ->method('create')
  112. ->willReturn($resultExtensionAttributes);
  113. $result->expects($this->once())
  114. ->method('setExtensionAttributes')
  115. ->with($resultExtensionAttributes)
  116. ->willReturnSelf();
  117. $this->customerMock->expects($this->once())
  118. ->method('getExtensionAttributes')
  119. ->willReturn($this->customerExtensionMock);
  120. $resultExtensionAttributes->expects($this->any())
  121. ->method('getIsSubscribed')
  122. ->willReturn($subscriptionOriginalValue);
  123. $this->customerExtensionMock->expects($this->any())
  124. ->method('getIsSubscribed')
  125. ->willReturn($subscriptionNewValue);
  126. if ($subscriptionOriginalValue !== $subscriptionNewValue) {
  127. if ($subscriptionNewValue) {
  128. $this->subscriber->expects($this->once())->method('subscribeCustomerById')->with($customerId);
  129. } else {
  130. $this->subscriber->expects($this->once())->method('unsubscribeCustomerById')->with($customerId);
  131. }
  132. $this->subscriber->expects($this->once())->method('isSubscribed')->willReturn($subscriptionNewValue);
  133. $resultExtensionAttributes->expects($this->once())->method('setIsSubscribed')->with($subscriptionNewValue);
  134. }
  135. $this->assertEquals($result, $this->plugin->afterSave($subject, $result, $this->customerMock));
  136. }
  137. /**
  138. * @return array
  139. */
  140. public function afterSaveDataProvider()
  141. {
  142. return [
  143. [true, true],
  144. [false, false],
  145. [true, false],
  146. [false, true],
  147. ];
  148. }
  149. public function testAfterDelete()
  150. {
  151. $subject = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
  152. $customer = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  153. $customer->expects($this->once())->method('getEmail')->willReturn('test@test.com');
  154. $this->subscriber->expects($this->once())->method('loadByEmail')->with('test@test.com')->willReturnSelf();
  155. $this->subscriber->expects($this->once())->method('getId')->willReturn(1);
  156. $this->subscriber->expects($this->once())->method('delete')->willReturnSelf();
  157. $this->assertEquals(true, $this->plugin->afterDelete($subject, true, $customer));
  158. }
  159. public function testAroundDeleteById()
  160. {
  161. $customerId = 1;
  162. $deleteCustomerById = function () {
  163. return true;
  164. };
  165. $subject = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
  166. $customer = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  167. $subject->expects($this->once())->method('getById')->willReturn($customer);
  168. $customer->expects($this->once())->method('getEmail')->willReturn('test@test.com');
  169. $this->subscriber->expects($this->once())->method('loadByEmail')->with('test@test.com')->willReturnSelf();
  170. $this->subscriber->expects($this->once())->method('getId')->willReturn(1);
  171. $this->subscriber->expects($this->once())->method('delete')->willReturnSelf();
  172. $this->assertEquals(true, $this->plugin->aroundDeleteById($subject, $deleteCustomerById, $customerId));
  173. }
  174. /**
  175. * @param int|null $subscriberStatusKey
  176. * @param int|null $subscriberStatusValue
  177. * @param bool $isSubscribed
  178. * @dataProvider afterGetByIdDataProvider
  179. * @return void
  180. */
  181. public function testAfterGetByIdCreatesExtensionAttributesIfItIsNotSet(
  182. $subscriberStatusKey,
  183. $subscriberStatusValue,
  184. $isSubscribed
  185. ) {
  186. $subject = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
  187. $subscriber = [$subscriberStatusKey => $subscriberStatusValue];
  188. $this->extensionFactoryMock->expects($this->any())
  189. ->method('create')
  190. ->willReturn($this->customerExtensionMock);
  191. $this->customerMock->expects($this->once())
  192. ->method('setExtensionAttributes')
  193. ->with($this->customerExtensionMock)
  194. ->willReturnSelf();
  195. $this->customerMock->expects($this->any())
  196. ->method('getId')
  197. ->willReturn(1);
  198. $this->subscriberResourceMock->expects($this->once())
  199. ->method('loadByCustomerData')
  200. ->with($this->customerMock)
  201. ->willReturn($subscriber);
  202. $this->customerExtensionMock->expects($this->once())->method('setIsSubscribed')->with($isSubscribed);
  203. $this->assertEquals(
  204. $this->customerMock,
  205. $this->plugin->afterGetById($subject, $this->customerMock)
  206. );
  207. }
  208. public function testAfterGetByIdSetsIsSubscribedFlagIfItIsNotSet()
  209. {
  210. $subject = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
  211. $subscriber = ['subscriber_id' => 1, 'subscriber_status' => 1];
  212. $this->customerMock->expects($this->any())
  213. ->method('getExtensionAttributes')
  214. ->willReturn($this->customerExtensionMock);
  215. $this->customerExtensionMock->expects($this->any())
  216. ->method('getIsSubscribed')
  217. ->willReturn(null);
  218. $this->subscriberResourceMock->expects($this->once())
  219. ->method('loadByCustomerData')
  220. ->with($this->customerMock)
  221. ->willReturn($subscriber);
  222. $this->customerExtensionMock->expects($this->once())
  223. ->method('setIsSubscribed')
  224. ->willReturnSelf();
  225. $this->assertEquals(
  226. $this->customerMock,
  227. $this->plugin->afterGetById($subject, $this->customerMock)
  228. );
  229. }
  230. /**
  231. * @return array
  232. */
  233. public function afterGetByIdDataProvider()
  234. {
  235. return [
  236. ['subscriber_status', 1, true],
  237. ['subscriber_status', 2, false],
  238. ['subscriber_status', 3, false],
  239. ['subscriber_status', 4, false],
  240. [null, null, false],
  241. ];
  242. }
  243. }