|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Customer\Test\Unit\Model; |
| 7 | + |
| 8 | +use Magento\Customer\Model\CustomerExtractor; |
| 9 | + |
| 10 | +class CustomerExtractorTest extends \PHPUnit_Framework_TestCase |
| 11 | +{ |
| 12 | + /** @var CustomerExtractor */ |
| 13 | + protected $customerExtractor; |
| 14 | + |
| 15 | + /** @var \Magento\Customer\Model\Metadata\FormFactory|\PHPUnit_Framework_MockObject_MockObject */ |
| 16 | + protected $formFactory; |
| 17 | + |
| 18 | + /** @var \Magento\Customer\Api\Data\CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject */ |
| 19 | + protected $customerFactory; |
| 20 | + |
| 21 | + /** @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 22 | + protected $storeManager; |
| 23 | + |
| 24 | + /** @var \Magento\Customer\Api\GroupManagementInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 25 | + protected $customerGroupManagement; |
| 26 | + |
| 27 | + /** @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject */ |
| 28 | + protected $dataObjectHelper; |
| 29 | + |
| 30 | + /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 31 | + protected $request; |
| 32 | + |
| 33 | + /** @var \Magento\Customer\Model\Metadata\Form|\PHPUnit_Framework_MockObject_MockObject */ |
| 34 | + protected $customerForm; |
| 35 | + |
| 36 | + /** @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 37 | + protected $customerData; |
| 38 | + |
| 39 | + /** @var \Magento\Store\Api\Data\StoreInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 40 | + protected $store; |
| 41 | + |
| 42 | + /** @var \Magento\Customer\Api\Data\GroupInterface|\PHPUnit_Framework_MockObject_MockObject */ |
| 43 | + protected $customerGroup; |
| 44 | + |
| 45 | + public function setUp() |
| 46 | + { |
| 47 | + $this->formFactory = $this->getMockForAbstractClass( |
| 48 | + 'Magento\Customer\Model\Metadata\FormFactory', |
| 49 | + [], |
| 50 | + '', |
| 51 | + false, |
| 52 | + false, |
| 53 | + true, |
| 54 | + ['create'] |
| 55 | + ); |
| 56 | + $this->customerFactory = $this->getMockForAbstractClass( |
| 57 | + 'Magento\Customer\Api\Data\CustomerInterfaceFactory', |
| 58 | + [], |
| 59 | + '', |
| 60 | + false, |
| 61 | + false, |
| 62 | + true, |
| 63 | + ['create'] |
| 64 | + ); |
| 65 | + $this->storeManager = $this->getMockForAbstractClass( |
| 66 | + 'Magento\Store\Model\StoreManagerInterface', |
| 67 | + [], |
| 68 | + '', |
| 69 | + false |
| 70 | + ); |
| 71 | + $this->customerGroupManagement = $this->getMockForAbstractClass( |
| 72 | + 'Magento\Customer\Api\GroupManagementInterface', |
| 73 | + [], |
| 74 | + '', |
| 75 | + false |
| 76 | + ); |
| 77 | + $this->dataObjectHelper = $this->getMock('Magento\Framework\Api\DataObjectHelper', [], [], '', false); |
| 78 | + $this->request = $this->getMockForAbstractClass('Magento\Framework\App\RequestInterface', [], '', false); |
| 79 | + $this->customerForm = $this->getMock('Magento\Customer\Model\Metadata\Form', [], [], '', false); |
| 80 | + $this->customerData = $this->getMockForAbstractClass( |
| 81 | + 'Magento\Customer\Api\Data\CustomerInterface', |
| 82 | + [], |
| 83 | + '', |
| 84 | + false |
| 85 | + ); |
| 86 | + $this->store = $this->getMockForAbstractClass( |
| 87 | + 'Magento\Store\Api\Data\StoreInterface', |
| 88 | + [], |
| 89 | + '', |
| 90 | + false |
| 91 | + ); |
| 92 | + $this->customerGroup = $this->getMockForAbstractClass( |
| 93 | + 'Magento\Customer\Api\Data\GroupInterface', |
| 94 | + [], |
| 95 | + '', |
| 96 | + false |
| 97 | + ); |
| 98 | + $this->customerExtractor = new CustomerExtractor( |
| 99 | + $this->formFactory, |
| 100 | + $this->customerFactory, |
| 101 | + $this->storeManager, |
| 102 | + $this->customerGroupManagement, |
| 103 | + $this->dataObjectHelper |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + public function testExtract() |
| 108 | + { |
| 109 | + $customerData = [ |
| 110 | + 'firstname' => 'firstname', |
| 111 | + 'lastname' => 'firstname', |
| 112 | + 'email' => 'email.example.com', |
| 113 | + ]; |
| 114 | + |
| 115 | + $this->formFactory->expects($this->once()) |
| 116 | + ->method('create') |
| 117 | + ->with('customer', 'form-code') |
| 118 | + ->willReturn($this->customerForm); |
| 119 | + $this->customerForm->expects($this->once()) |
| 120 | + ->method('extractData') |
| 121 | + ->with($this->request) |
| 122 | + ->willReturn($customerData); |
| 123 | + $this->customerForm->expects($this->once()) |
| 124 | + ->method('getAllowedAttributes') |
| 125 | + ->willReturn(['group_id' => 'attribute object']); |
| 126 | + $this->customerFactory->expects($this->once()) |
| 127 | + ->method('create') |
| 128 | + ->willReturn($this->customerData); |
| 129 | + $this->dataObjectHelper->expects($this->once()) |
| 130 | + ->method('populateWithArray') |
| 131 | + ->with($this->customerData, $customerData, '\Magento\Customer\Api\Data\CustomerInterface') |
| 132 | + ->willReturn($this->customerData); |
| 133 | + $this->storeManager->expects($this->once()) |
| 134 | + ->method('getStore') |
| 135 | + ->willReturn($this->store); |
| 136 | + $this->store->expects($this->exactly(2)) |
| 137 | + ->method('getId') |
| 138 | + ->willReturn(1); |
| 139 | + $this->customerGroupManagement->expects($this->once()) |
| 140 | + ->method('getDefaultGroup') |
| 141 | + ->with(1) |
| 142 | + ->willReturn($this->customerGroup); |
| 143 | + $this->customerGroup->expects($this->once()) |
| 144 | + ->method('getId') |
| 145 | + ->willReturn(1); |
| 146 | + $this->customerData->expects($this->once()) |
| 147 | + ->method('setGroupId') |
| 148 | + ->with(1); |
| 149 | + $this->store->expects($this->once()) |
| 150 | + ->method('getWebsiteId') |
| 151 | + ->willReturn(1); |
| 152 | + $this->customerData->expects($this->once()) |
| 153 | + ->method('setWebsiteId') |
| 154 | + ->with(1); |
| 155 | + $this->customerData->expects($this->once()) |
| 156 | + ->method('setStoreId') |
| 157 | + ->with(1); |
| 158 | + |
| 159 | + $this->assertSame($this->customerData, $this->customerExtractor->extract('form-code', $this->request)); |
| 160 | + } |
| 161 | +} |
0 commit comments