|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Customer\Test\Unit\Model; |
| 7 | + |
| 8 | +use Magento\Customer\Api\Data\OptionInterfaceFactory; |
| 9 | +use Magento\Customer\Api\Data\ValidationRuleInterfaceFactory; |
| 10 | +use Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory; |
| 11 | +use Magento\Customer\Model\AttributeMetadataConverter; |
| 12 | + |
| 13 | +/** |
| 14 | + * Class AttributeMetadataConverterTest |
| 15 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 16 | + * @package Magento\Customer\Test\Unit\Model |
| 17 | + */ |
| 18 | +class AttributeMetadatConverterTest extends \PHPUnit_Framework_TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var OptionInterfaceFactory | \PHPUnit_Framework_MockObject_MockObject |
| 22 | + */ |
| 23 | + private $optionFactory; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var ValidationRuleInterfaceFactory | \PHPUnit_Framework_MockObject_MockObject |
| 27 | + */ |
| 28 | + private $validationRuleFactory; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var AttributeMetadataInterfaceFactory | \PHPUnit_Framework_MockObject_MockObject |
| 32 | + */ |
| 33 | + private $attributeMetadataFactory; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var \Magento\Framework\Api\DataObjectHelper | \PHPUnit_Framework_MockObject_MockObject |
| 37 | + */ |
| 38 | + private $dataObjectHelper; |
| 39 | + |
| 40 | + /** @var AttributeMetadataConverter */ |
| 41 | + private $model; |
| 42 | + |
| 43 | + /** @var \Magento\Customer\Model\Attribute | \PHPUnit_Framework_MockObject_MockObject */ |
| 44 | + private $attribute; |
| 45 | + |
| 46 | + public function setUp() |
| 47 | + { |
| 48 | + $this->optionFactory = $this->getMockBuilder(OptionInterfaceFactory::class) |
| 49 | + ->setMethods(['create']) |
| 50 | + ->disableOriginalConstructor() |
| 51 | + ->getMock(); |
| 52 | + $this->validationRuleFactory = $this->getMockBuilder(ValidationRuleInterfaceFactory::class) |
| 53 | + ->setMethods(['create']) |
| 54 | + ->disableOriginalConstructor() |
| 55 | + ->getMock(); |
| 56 | + $this->attributeMetadataFactory = $this->getMockBuilder(AttributeMetadataInterfaceFactory::class) |
| 57 | + ->setMethods(['create']) |
| 58 | + ->disableOriginalConstructor() |
| 59 | + ->getMock(); |
| 60 | + $this->dataObjectHelper = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class) |
| 61 | + ->disableOriginalConstructor() |
| 62 | + ->getMock(); |
| 63 | + $this->attribute = $this->getMockBuilder(\Magento\Customer\Model\Attribute::class) |
| 64 | + ->disableOriginalConstructor() |
| 65 | + ->getMock(); |
| 66 | + |
| 67 | + $this->model = new AttributeMetadataConverter( |
| 68 | + $this->optionFactory, |
| 69 | + $this->validationRuleFactory, |
| 70 | + $this->attributeMetadataFactory, |
| 71 | + $this->dataObjectHelper |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @return array |
| 77 | + */ |
| 78 | + private function prepareValidateRules() |
| 79 | + { |
| 80 | + return [ |
| 81 | + 'one' => 'numeric', |
| 82 | + 'two' => 'alphanumeric' |
| 83 | + ]; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @return array |
| 88 | + */ |
| 89 | + private function prepareOptions() |
| 90 | + { |
| 91 | + return [ |
| 92 | + [ |
| 93 | + 'label' => 'few_values', |
| 94 | + 'value' => [ |
| 95 | + [1], [2] |
| 96 | + ] |
| 97 | + ], |
| 98 | + [ |
| 99 | + 'label' => 'one_value', |
| 100 | + 'value' => 1 |
| 101 | + ] |
| 102 | + ]; |
| 103 | + } |
| 104 | + |
| 105 | + public function testCreateAttributeMetadataTestWithSource() |
| 106 | + { |
| 107 | + $validatedRules = $this->prepareValidateRules(); |
| 108 | + $options = $this->prepareOptions(); |
| 109 | + $optionDataObjectForSimpleValue1 = $this->getMockBuilder(\Magento\Customer\Model\Data\Option::class) |
| 110 | + ->disableOriginalConstructor() |
| 111 | + ->getMock(); |
| 112 | + $optionDataObjectForSimpleValue2 = $this->getMockBuilder(\Magento\Customer\Model\Data\Option::class) |
| 113 | + ->disableOriginalConstructor() |
| 114 | + ->getMock(); |
| 115 | + $optionObject1 = $this->getMock(\Magento\Customer\Api\Data\OptionInterface::class); |
| 116 | + $optionObject2 = $this->getMock(\Magento\Customer\Api\Data\OptionInterface::class); |
| 117 | + $this->optionFactory->expects($this->exactly(4)) |
| 118 | + ->method('create') |
| 119 | + ->will( |
| 120 | + $this->onConsecutiveCalls( |
| 121 | + $optionDataObjectForSimpleValue2, |
| 122 | + $optionObject1, |
| 123 | + $optionObject2, |
| 124 | + $optionDataObjectForSimpleValue1 |
| 125 | + ) |
| 126 | + ); |
| 127 | + $source = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Source\AbstractSource::class) |
| 128 | + ->disableOriginalConstructor() |
| 129 | + ->getMock(); |
| 130 | + $source->expects($this->once()) |
| 131 | + ->method('getAllOptions') |
| 132 | + ->willReturn($options); |
| 133 | + $this->attribute->expects($this->once()) |
| 134 | + ->method('usesSource') |
| 135 | + ->willReturn(true); |
| 136 | + $this->attribute->expects($this->once()) |
| 137 | + ->method('getSource') |
| 138 | + ->willReturn($source); |
| 139 | + $optionDataObjectForSimpleValue1->expects($this->once()) |
| 140 | + ->method('setValue') |
| 141 | + ->with(1); |
| 142 | + $optionDataObjectForSimpleValue2->expects($this->once()) |
| 143 | + ->method('setLabel') |
| 144 | + ->with('few_values'); |
| 145 | + $optionDataObjectForSimpleValue1->expects($this->once()) |
| 146 | + ->method('setLabel') |
| 147 | + ->with('one_value'); |
| 148 | + $this->dataObjectHelper->expects($this->exactly(2)) |
| 149 | + ->method('populateWithArray') |
| 150 | + ->withConsecutive( |
| 151 | + [$optionObject1, ['1'], \Magento\Customer\Api\Data\OptionInterface::class], |
| 152 | + [$optionObject2, ['2'], \Magento\Customer\Api\Data\OptionInterface::class] |
| 153 | + ); |
| 154 | + $validationRule1 = $this->getMock(\Magento\Customer\Api\Data\ValidationRuleInterface::class); |
| 155 | + $validationRule2 = $this->getMock(\Magento\Customer\Api\Data\ValidationRuleInterface::class); |
| 156 | + $this->validationRuleFactory->expects($this->exactly(2)) |
| 157 | + ->method('create') |
| 158 | + ->will($this->onConsecutiveCalls($validationRule1, $validationRule2)); |
| 159 | + $validationRule1->expects($this->once()) |
| 160 | + ->method('setValue') |
| 161 | + ->with('numeric'); |
| 162 | + $validationRule1->expects($this->once()) |
| 163 | + ->method('setName') |
| 164 | + ->with('one') |
| 165 | + ->willReturnSelf(); |
| 166 | + $validationRule2->expects($this->once()) |
| 167 | + ->method('setValue') |
| 168 | + ->with('alphanumeric'); |
| 169 | + $validationRule2->expects($this->once()) |
| 170 | + ->method('setName') |
| 171 | + ->with('two') |
| 172 | + ->willReturnSelf(); |
| 173 | + $attributeMetaData = $this->getMockBuilder(\Magento\Customer\Model\Data\AttributeMetadata::class) |
| 174 | + ->disableOriginalConstructor() |
| 175 | + ->enableProxyingToOriginalMethods() |
| 176 | + ->getMock(); |
| 177 | + $this->attribute->expects($this->once()) |
| 178 | + ->method('getValidateRules') |
| 179 | + ->willReturn($validatedRules); |
| 180 | + $this->attributeMetadataFactory->expects($this->once()) |
| 181 | + ->method('create') |
| 182 | + ->willReturn($attributeMetaData); |
| 183 | + $frontend = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend::class) |
| 184 | + ->disableOriginalConstructor() |
| 185 | + ->getMock(); |
| 186 | + $this->attribute->expects($this->once()) |
| 187 | + ->method('getFrontend') |
| 188 | + ->willReturn($frontend); |
| 189 | + $optionDataObjectForSimpleValue2->expects($this->once()) |
| 190 | + ->method('setOptions') |
| 191 | + ->with([$optionObject1, $optionObject2]); |
| 192 | + $this->model->createMetadataAttribute($this->attribute); |
| 193 | + } |
| 194 | +} |
0 commit comments