123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Newsletter\Test\Unit\Model\Plugin;
- use Magento\Customer\Api\Data\CustomerInterface;
- use Magento\Customer\Model\ResourceModel\CustomerRepository;
- use Magento\Customer\Api\Data\CustomerExtensionInterface;
- use Magento\Framework\Api\ExtensionAttributesFactory;
- use Magento\Newsletter\Model\ResourceModel\Subscriber;
- class CustomerPluginTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Newsletter\Model\Plugin\CustomerPlugin
- */
- private $plugin;
- /**
- * @var \Magento\Newsletter\Model\SubscriberFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- private $subscriberFactory;
- /**
- * @var \Magento\Newsletter\Model\Subscriber|\PHPUnit_Framework_MockObject_MockObject
- */
- private $subscriber;
- /**
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
- */
- private $objectManager;
- /**
- * @var ExtensionAttributesFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- private $extensionFactoryMock;
- /**
- * @var CustomerExtensionInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $customerExtensionMock;
- /**
- * @var Subscriber|\PHPUnit_Framework_MockObject_MockObject
- */
- private $subscriberResourceMock;
- /**
- * @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $customerMock;
- protected function setUp()
- {
- $this->subscriberFactory = $this->getMockBuilder(\Magento\Newsletter\Model\SubscriberFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->subscriber = $this->getMockBuilder(\Magento\Newsletter\Model\Subscriber::class)
- ->setMethods(
- [
- 'loadByEmail',
- 'getId',
- 'delete',
- 'updateSubscription',
- 'subscribeCustomerById',
- 'unsubscribeCustomerById',
- 'isSubscribed',
- ]
- )->disableOriginalConstructor()
- ->getMock();
- $this->extensionFactoryMock = $this->getMockBuilder(ExtensionAttributesFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->customerExtensionMock = $this->getMockBuilder(CustomerExtensionInterface::class)
- ->setMethods(['getIsSubscribed', 'setIsSubscribed'])
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->subscriberResourceMock = $this->getMockBuilder(Subscriber::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->customerMock = $this->getMockBuilder(CustomerInterface::class)
- ->setMethods(['getExtensionAttributes'])
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $this->subscriberFactory->expects($this->any())->method('create')->willReturn($this->subscriber);
- $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->plugin = $this->objectManager->getObject(
- \Magento\Newsletter\Model\Plugin\CustomerPlugin::class,
- [
- 'subscriberFactory' => $this->subscriberFactory,
- 'extensionFactory' => $this->extensionFactoryMock,
- 'subscriberResource' => $this->subscriberResourceMock,
- ]
- );
- }
- /**
- * @param bool $subscriptionOriginalValue
- * @param bool $subscriptionNewValue
- * @dataProvider afterSaveDataProvider
- * @return void
- */
- public function testAfterSave($subscriptionOriginalValue, $subscriptionNewValue)
- {
- $customerId = 1;
- /** @var CustomerInterface | \PHPUnit_Framework_MockObject_MockObject $result */
- $result = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
- /** @var CustomerRepository | \PHPUnit_Framework_MockObject_MockObject $subject */
- $subject = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
- /** @var CustomerExtensionInterface|\PHPUnit_Framework_MockObject_MockObject $resultExtensionAttributes */
- $resultExtensionAttributes = $this->getMockBuilder(CustomerExtensionInterface::class)
- ->setMethods(['getIsSubscribed', 'setIsSubscribed'])
- ->getMockForAbstractClass();
- $result->expects($this->atLeastOnce())->method('getId')->willReturn($customerId);
- $result->expects($this->any())->method('getExtensionAttributes')->willReturn(null);
- $this->extensionFactoryMock->expects($this->any())
- ->method('create')
- ->willReturn($resultExtensionAttributes);
- $result->expects($this->once())
- ->method('setExtensionAttributes')
- ->with($resultExtensionAttributes)
- ->willReturnSelf();
- $this->customerMock->expects($this->once())
- ->method('getExtensionAttributes')
- ->willReturn($this->customerExtensionMock);
- $resultExtensionAttributes->expects($this->any())
- ->method('getIsSubscribed')
- ->willReturn($subscriptionOriginalValue);
- $this->customerExtensionMock->expects($this->any())
- ->method('getIsSubscribed')
- ->willReturn($subscriptionNewValue);
- if ($subscriptionOriginalValue !== $subscriptionNewValue) {
- if ($subscriptionNewValue) {
- $this->subscriber->expects($this->once())->method('subscribeCustomerById')->with($customerId);
- } else {
- $this->subscriber->expects($this->once())->method('unsubscribeCustomerById')->with($customerId);
- }
- $this->subscriber->expects($this->once())->method('isSubscribed')->willReturn($subscriptionNewValue);
- $resultExtensionAttributes->expects($this->once())->method('setIsSubscribed')->with($subscriptionNewValue);
- }
- $this->assertEquals($result, $this->plugin->afterSave($subject, $result, $this->customerMock));
- }
- /**
- * @return array
- */
- public function afterSaveDataProvider()
- {
- return [
- [true, true],
- [false, false],
- [true, false],
- [false, true],
- ];
- }
- public function testAfterDelete()
- {
- $subject = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
- $customer = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
- $customer->expects($this->once())->method('getEmail')->willReturn('test@test.com');
- $this->subscriber->expects($this->once())->method('loadByEmail')->with('test@test.com')->willReturnSelf();
- $this->subscriber->expects($this->once())->method('getId')->willReturn(1);
- $this->subscriber->expects($this->once())->method('delete')->willReturnSelf();
- $this->assertEquals(true, $this->plugin->afterDelete($subject, true, $customer));
- }
- public function testAroundDeleteById()
- {
- $customerId = 1;
- $deleteCustomerById = function () {
- return true;
- };
- $subject = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
- $customer = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
- $subject->expects($this->once())->method('getById')->willReturn($customer);
- $customer->expects($this->once())->method('getEmail')->willReturn('test@test.com');
- $this->subscriber->expects($this->once())->method('loadByEmail')->with('test@test.com')->willReturnSelf();
- $this->subscriber->expects($this->once())->method('getId')->willReturn(1);
- $this->subscriber->expects($this->once())->method('delete')->willReturnSelf();
- $this->assertEquals(true, $this->plugin->aroundDeleteById($subject, $deleteCustomerById, $customerId));
- }
- /**
- * @param int|null $subscriberStatusKey
- * @param int|null $subscriberStatusValue
- * @param bool $isSubscribed
- * @dataProvider afterGetByIdDataProvider
- * @return void
- */
- public function testAfterGetByIdCreatesExtensionAttributesIfItIsNotSet(
- $subscriberStatusKey,
- $subscriberStatusValue,
- $isSubscribed
- ) {
- $subject = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
- $subscriber = [$subscriberStatusKey => $subscriberStatusValue];
- $this->extensionFactoryMock->expects($this->any())
- ->method('create')
- ->willReturn($this->customerExtensionMock);
- $this->customerMock->expects($this->once())
- ->method('setExtensionAttributes')
- ->with($this->customerExtensionMock)
- ->willReturnSelf();
- $this->customerMock->expects($this->any())
- ->method('getId')
- ->willReturn(1);
- $this->subscriberResourceMock->expects($this->once())
- ->method('loadByCustomerData')
- ->with($this->customerMock)
- ->willReturn($subscriber);
- $this->customerExtensionMock->expects($this->once())->method('setIsSubscribed')->with($isSubscribed);
- $this->assertEquals(
- $this->customerMock,
- $this->plugin->afterGetById($subject, $this->customerMock)
- );
- }
- public function testAfterGetByIdSetsIsSubscribedFlagIfItIsNotSet()
- {
- $subject = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
- $subscriber = ['subscriber_id' => 1, 'subscriber_status' => 1];
- $this->customerMock->expects($this->any())
- ->method('getExtensionAttributes')
- ->willReturn($this->customerExtensionMock);
- $this->customerExtensionMock->expects($this->any())
- ->method('getIsSubscribed')
- ->willReturn(null);
- $this->subscriberResourceMock->expects($this->once())
- ->method('loadByCustomerData')
- ->with($this->customerMock)
- ->willReturn($subscriber);
- $this->customerExtensionMock->expects($this->once())
- ->method('setIsSubscribed')
- ->willReturnSelf();
- $this->assertEquals(
- $this->customerMock,
- $this->plugin->afterGetById($subject, $this->customerMock)
- );
- }
- /**
- * @return array
- */
- public function afterGetByIdDataProvider()
- {
- return [
- ['subscriber_status', 1, true],
- ['subscriber_status', 2, false],
- ['subscriber_status', 3, false],
- ['subscriber_status', 4, false],
- [null, null, false],
- ];
- }
- }
|