|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Customer\Test\Unit\Block\Adminhtml\Edit\Tab; |
| 7 | + |
| 8 | +use Magento\Customer\Controller\RegistryConstants; |
| 9 | + |
| 10 | +class NewsletterTest extends \PHPUnit_Framework_TestCase |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @var \Magento\Customer\Block\Adminhtml\Edit\Tab\Newsletter |
| 14 | + */ |
| 15 | + protected $model; |
| 16 | + |
| 17 | + /** |
| 18 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 19 | + */ |
| 20 | + protected $contextMock; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 24 | + */ |
| 25 | + protected $registryMock; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 29 | + */ |
| 30 | + protected $formFactoryMock; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 34 | + */ |
| 35 | + protected $subscriberFactoryMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 39 | + */ |
| 40 | + protected $accountManagementMock; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 44 | + */ |
| 45 | + protected $urlBuilderMock; |
| 46 | + |
| 47 | + public function setUp() |
| 48 | + { |
| 49 | + $this->contextMock = $this->getMock('\Magento\Backend\Block\Template\Context', [], [], '', false); |
| 50 | + $this->registryMock = $this->getMock('\Magento\Framework\Registry', [], [], '', false); |
| 51 | + $this->formFactoryMock = $this->getMock('\Magento\Framework\Data\FormFactory', [], [], '', false); |
| 52 | + $this->subscriberFactoryMock = $this->getMock( |
| 53 | + '\Magento\Newsletter\Model\SubscriberFactory', |
| 54 | + ['create'], |
| 55 | + [], |
| 56 | + '', |
| 57 | + false |
| 58 | + ); |
| 59 | + $this->accountManagementMock = $this->getMock( |
| 60 | + '\Magento\Customer\Api\AccountManagementInterface', |
| 61 | + [], |
| 62 | + [], |
| 63 | + '', |
| 64 | + false |
| 65 | + ); |
| 66 | + $this->urlBuilderMock = $this->getMock('\Magento\Framework\UrlInterface', [], [], '', false); |
| 67 | + $this->contextMock->expects($this->once())->method('getUrlBuilder')->willReturn($this->urlBuilderMock); |
| 68 | + |
| 69 | + $this->model = new \Magento\Customer\Block\Adminhtml\Edit\Tab\Newsletter( |
| 70 | + $this->contextMock, |
| 71 | + $this->registryMock, |
| 72 | + $this->formFactoryMock, |
| 73 | + $this->subscriberFactoryMock, |
| 74 | + $this->accountManagementMock |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + public function testInitFormCanNotShowTab() |
| 79 | + { |
| 80 | + $this->registryMock->expects($this->once())->method('registry')->with(RegistryConstants::CURRENT_CUSTOMER_ID) |
| 81 | + ->willReturn(false); |
| 82 | + $this->assertSame($this->model, $this->model->initForm()); |
| 83 | + } |
| 84 | + |
| 85 | + public function testInitForm() |
| 86 | + { |
| 87 | + $subscriberMock = $this->getMock('\Magento\Newsletter\Model\Subscriber', [], [], '', false); |
| 88 | + $fieldsetMock = $this->getMock('\Magento\Framework\Data\Form\Element\Fieldset', [], [], '', false); |
| 89 | + $elementMock = $this->getMock('\Magento\Framework\Data\Form\Element\AbstractElement', [], [], '', false); |
| 90 | + $formMock = $this->getMock( |
| 91 | + '\Magento\Framework\Data\Form', |
| 92 | + ['setHtmlIdPrefix', 'addFieldset', 'setValues', 'getElement', 'setForm', 'setParent', 'setBaseUrl'], |
| 93 | + [], |
| 94 | + '', |
| 95 | + false |
| 96 | + ); |
| 97 | + $this->registryMock->expects($this->atLeastOnce())->method('registry')->willReturn($subscriberMock); |
| 98 | + $this->formFactoryMock->expects($this->once())->method('create')->willReturn($formMock); |
| 99 | + $formMock->expects($this->once())->method('setHtmlIdPrefix')->with('_newsletter'); |
| 100 | + $this->subscriberFactoryMock->expects($this->once())->method('create')->willReturn($subscriberMock); |
| 101 | + $subscriberMock->expects($this->once())->method('loadByCustomerId')->with($subscriberMock)->willReturnSelf(); |
| 102 | + $this->registryMock->expects($this->once())->method('register')->with('subscriber', $subscriberMock); |
| 103 | + $formMock->expects($this->once())->method('addFieldset')->willReturn($fieldsetMock); |
| 104 | + $this->accountManagementMock->expects($this->once())->method('isReadOnly')->with($subscriberMock) |
| 105 | + ->willReturn(false); |
| 106 | + $subscriberMock->expects($this->once())->method('isSubscribed')->willReturn(true); |
| 107 | + $formMock->expects($this->once())->method('getElement')->willReturn($elementMock); |
| 108 | + $this->urlBuilderMock->expects($this->once())->method('getBaseUrl')->willReturn('domain.com'); |
| 109 | + $this->assertSame($this->model, $this->model->initForm()); |
| 110 | + } |
| 111 | +} |
0 commit comments