| 
 | 1 | +<?php  | 
 | 2 | +/**  | 
 | 3 | + * Copyright © 2015 Magento. All rights reserved.  | 
 | 4 | + * See COPYING.txt for license details.  | 
 | 5 | + */  | 
 | 6 | +namespace Magento\Newsletter\Test\Unit\Model;  | 
 | 7 | + | 
 | 8 | +class SubscriberTest extends \PHPUnit_Framework_TestCase  | 
 | 9 | +{  | 
 | 10 | +    /**  | 
 | 11 | +     * @var \Magento\Newsletter\Helper\Data|\PHPUnit_Framework_MockObject_MockObject  | 
 | 12 | +     */  | 
 | 13 | +    protected $newsletterData;  | 
 | 14 | + | 
 | 15 | +    /**  | 
 | 16 | +     * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject  | 
 | 17 | +     */  | 
 | 18 | +    protected $scopeConfig;  | 
 | 19 | + | 
 | 20 | +    /**  | 
 | 21 | +     * @var \Magento\Framework\Mail\Template\TransportBuilder|\PHPUnit_Framework_MockObject_MockObject  | 
 | 22 | +     */  | 
 | 23 | +    protected $transportBuilder;  | 
 | 24 | + | 
 | 25 | +    /**  | 
 | 26 | +     * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject  | 
 | 27 | +     */  | 
 | 28 | +    protected $storeManager;  | 
 | 29 | + | 
 | 30 | +    /**  | 
 | 31 | +     * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject  | 
 | 32 | +     */  | 
 | 33 | +    protected $customerSession;  | 
 | 34 | + | 
 | 35 | +    /**  | 
 | 36 | +     * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject  | 
 | 37 | +     */  | 
 | 38 | +    protected $customerRepository;  | 
 | 39 | + | 
 | 40 | +    /**  | 
 | 41 | +     * @var \Magento\Customer\Api\AccountManagementInterface|\PHPUnit_Framework_MockObject_MockObject  | 
 | 42 | +     */  | 
 | 43 | +    protected $customerAccountManagement;  | 
 | 44 | + | 
 | 45 | +    /**  | 
 | 46 | +     * @var \Magento\Framework\Translate\Inline\StateInterface|\PHPUnit_Framework_MockObject_MockObject  | 
 | 47 | +     */  | 
 | 48 | +    protected $inlineTranslation;  | 
 | 49 | + | 
 | 50 | +    /**  | 
 | 51 | +     * @var \Magento\Newsletter\Model\Resource\Subscriber|\PHPUnit_Framework_MockObject_MockObject  | 
 | 52 | +     */  | 
 | 53 | +    protected $resource;  | 
 | 54 | + | 
 | 55 | +    /**  | 
 | 56 | +     * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager  | 
 | 57 | +     */  | 
 | 58 | +    protected $objectManager;  | 
 | 59 | + | 
 | 60 | +    /**  | 
 | 61 | +     * @var \Magento\Newsletter\Model\Subscriber  | 
 | 62 | +     */  | 
 | 63 | +    protected $subscriber;  | 
 | 64 | + | 
 | 65 | +    public function setUp()  | 
 | 66 | +    {  | 
 | 67 | +        $this->newsletterData = $this->getMock('Magento\Newsletter\Helper\Data', [], [], '', false);  | 
 | 68 | +        $this->scopeConfig = $this->getMock('Magento\Framework\App\Config\ScopeConfigInterface');  | 
 | 69 | +        $this->transportBuilder = $this->getMock(  | 
 | 70 | +            'Magento\Framework\Mail\Template\TransportBuilder',  | 
 | 71 | +            [  | 
 | 72 | +                'setTemplateIdentifier',  | 
 | 73 | +                'setTemplateOptions',  | 
 | 74 | +                'setTemplateVars',  | 
 | 75 | +                'setFrom',  | 
 | 76 | +                'addTo',  | 
 | 77 | +                'getTransport'  | 
 | 78 | +            ],  | 
 | 79 | +            [],  | 
 | 80 | +            '',  | 
 | 81 | +            false  | 
 | 82 | +        );  | 
 | 83 | +        $this->storeManager = $this->getMock('Magento\Store\Model\StoreManagerInterface');  | 
 | 84 | +        $this->customerSession = $this->getMock(  | 
 | 85 | +            'Magento\Customer\Model\Session',  | 
 | 86 | +            [  | 
 | 87 | +                'isLoggedIn',  | 
 | 88 | +                'getCustomerDataObject',  | 
 | 89 | +                'getCustomerId'  | 
 | 90 | +            ],  | 
 | 91 | +            [],  | 
 | 92 | +            '',  | 
 | 93 | +            false  | 
 | 94 | +        );  | 
 | 95 | +        $this->customerRepository = $this->getMock('Magento\Customer\Api\CustomerRepositoryInterface');  | 
 | 96 | +        $this->customerAccountManagement = $this->getMock('Magento\Customer\Api\AccountManagementInterface');  | 
 | 97 | +        $this->inlineTranslation = $this->getMock('Magento\Framework\Translate\Inline\StateInterface');  | 
 | 98 | +        $this->resource = $this->getMock(  | 
 | 99 | +            'Magento\Newsletter\Model\Resource\Subscriber',  | 
 | 100 | +            [  | 
 | 101 | +                'loadByEmail',  | 
 | 102 | +                'getIdFieldName',  | 
 | 103 | +                'save'  | 
 | 104 | +            ],  | 
 | 105 | +            [],  | 
 | 106 | +            '',  | 
 | 107 | +            false  | 
 | 108 | +        );  | 
 | 109 | +        $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);  | 
 | 110 | + | 
 | 111 | +        $this->subscriber = $this->objectManager->getObject(  | 
 | 112 | +            'Magento\Newsletter\Model\Subscriber',  | 
 | 113 | +            [  | 
 | 114 | +                'newsletterData' => $this->newsletterData,  | 
 | 115 | +                'scopeConfig' => $this->scopeConfig,  | 
 | 116 | +                'transportBuilder' => $this->transportBuilder,  | 
 | 117 | +                'storeManager' => $this->storeManager,  | 
 | 118 | +                'customerSession' => $this->customerSession,  | 
 | 119 | +                'customerRepository' => $this->customerRepository,  | 
 | 120 | +                'customerAccountManagement' => $this->customerAccountManagement,  | 
 | 121 | +                'inlineTranslation' => $this->inlineTranslation,  | 
 | 122 | +                'resource' => $this->resource  | 
 | 123 | +            ]  | 
 | 124 | +        );  | 
 | 125 | +    }  | 
 | 126 | + | 
 | 127 | +    public function testSubscribe()  | 
 | 128 | +    {  | 
 | 129 | + | 
 | 130 | +        $this->resource->expects($this->any())->method('loadByEmail')->willReturn(  | 
 | 131 | +            [  | 
 | 132 | +                'subscriber_status' => 3,  | 
 | 133 | +                'subscriber_email' => $email,  | 
 | 134 | +                'name' => 'subscriber_name'  | 
 | 135 | +            ]  | 
 | 136 | +        );  | 
 | 137 | +        $this->resource->expects($this->any())->method('getIdFieldName')->willReturn('id_field');  | 
 | 138 | +        $this->scopeConfig->expects($this->any())->method('getValue')->willReturn(true);  | 
 | 139 | +        $this->customerSession->expects($this->any())->method('isLoggedIn')->willReturn(true);  | 
 | 140 | +        $customerDataModel = $this->getMock('\Magento\Customer\Api\Data\CustomerInterface');  | 
 | 141 | +        $this->customerSession->expects($this->any())->method('getCustomerDataObject')->willReturn($customerDataModel);  | 
 | 142 | +        $this->customerSession->expects($this->any())->method('getCustomerId')->willReturn(1);  | 
 | 143 | +        $customerDataModel->expects($this->any())->method('getEmail')->willReturn($email);  | 
 | 144 | +        $this->customerRepository->expects($this->any())->method('getById')->willReturn($customerDataModel);  | 
 | 145 | +        $customerDataModel->expects($this->any())->method('getStoreId')->willReturn(1);  | 
 | 146 | +        $customerDataModel->expects($this->any())->method('getId')->willReturn(1);  | 
 | 147 | +        $this->transportBuilder->expects($this->any())->method('setTemplateIdentifier')->willReturnSelf();  | 
 | 148 | +        $this->transportBuilder->expects($this->any())->method('setTemplateOptions')->willReturnSelf();  | 
 | 149 | +        $this->transportBuilder->expects($this->any())->method('setTemplateVars')->willReturnSelf();  | 
 | 150 | +        $this->transportBuilder->expects($this->any())->method('setFrom')->willReturnSelf();  | 
 | 151 | +        $this->transportBuilder->expects($this->any())->method('addTo')->willReturnSelf();  | 
 | 152 | +        $storeModel = $this->getMock('\Magento\Store\Model\Store', ['getId'], [], '', false);  | 
 | 153 | +        $this-> scopeConfig-> expects( $this-> any())-> method( 'getValue')-> willReturn( '[email protected]');   | 
 | 154 | +        $this->storeManager->expects($this->any())->method('getStore')->willReturn($storeModel);  | 
 | 155 | +        $storeModel->expects($this->any())->method('getId')->willReturn(1);  | 
 | 156 | +        $transport = $this->getMock('\Magento\Framework\Mail\TransportInterface');  | 
 | 157 | +        $this->transportBuilder->expects($this->any())->method('getTransport')->willReturn($transport);  | 
 | 158 | +        $transport->expects($this->any())->method('sendMessage')->willReturnSelf();  | 
 | 159 | +        $inlineTranslation = $this->getMock('Magento\Framework\Translate\Inline\StateInterface');  | 
 | 160 | +        $inlineTranslation->expects($this->any())->method('resume')->willReturnSelf();  | 
 | 161 | +        $this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();  | 
 | 162 | +        $this->assertEquals(1, $this->subscriber->subscribe($email));  | 
 | 163 | +    }  | 
 | 164 | +}  | 
0 commit comments