Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StaticFieldTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
16  as FieldTypeConverterInterface;
18  as IndexTypeConverterInterface;
20  as FieldTypeResolver;
22  as FieldIndexResolver;
23 
27 class StaticFieldTest extends \PHPUnit\Framework\TestCase
28 {
32  private $provider;
33 
37  private $eavConfig;
38 
42  private $fieldTypeConverter;
43 
47  private $indexTypeConverter;
48 
52  private $attributeAdapterProvider;
53 
57  private $fieldIndexResolver;
58 
62  private $fieldTypeResolver;
63 
69  protected function setUp()
70  {
71  $this->eavConfig = $this->getMockBuilder(Config::class)
72  ->disableOriginalConstructor()
73  ->setMethods(['getEntityAttributes'])
74  ->getMock();
75  $this->fieldTypeConverter = $this->getMockBuilder(FieldTypeConverterInterface::class)
76  ->disableOriginalConstructor()
77  ->getMockForAbstractClass();
78  $this->indexTypeConverter = $this->getMockBuilder(IndexTypeConverterInterface::class)
79  ->disableOriginalConstructor()
80  ->getMockForAbstractClass();
81  $this->attributeAdapterProvider = $this->getMockBuilder(AttributeProvider::class)
82  ->disableOriginalConstructor()
83  ->setMethods(['getByAttributeCode'])
84  ->getMock();
85  $this->fieldTypeResolver = $this->getMockBuilder(FieldTypeResolver::class)
86  ->disableOriginalConstructor()
87  ->setMethods(['getFieldType'])
88  ->getMock();
89  $this->fieldIndexResolver = $this->getMockBuilder(FieldIndexResolver::class)
90  ->disableOriginalConstructor()
91  ->setMethods(['getFieldIndex'])
92  ->getMock();
93 
94  $objectManager = new ObjectManagerHelper($this);
95 
96  $this->provider = $objectManager->getObject(
97  \Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\StaticField::class,
98  [
99  'eavConfig' => $this->eavConfig,
100  'fieldTypeConverter' => $this->fieldTypeConverter,
101  'indexTypeConverter' => $this->indexTypeConverter,
102  'attributeAdapterProvider' => $this->attributeAdapterProvider,
103  'fieldIndexResolver' => $this->fieldIndexResolver,
104  'fieldTypeResolver' => $this->fieldTypeResolver,
105  ]
106  );
107  }
108 
119  public function testGetAllAttributesTypes(
121  $inputType,
122  $indexType,
123  $isComplexType,
124  $complexType,
125  $expected
126  ) {
127  $this->fieldTypeResolver->expects($this->any())
128  ->method('getFieldType')
129  ->willReturn($inputType);
130  $this->fieldIndexResolver->expects($this->any())
131  ->method('getFieldIndex')
132  ->willReturn($indexType);
133  $this->indexTypeConverter->expects($this->any())
134  ->method('convert')
135  ->willReturn('no');
136 
137  $productAttributeMock = $this->getMockBuilder(AbstractAttribute::class)
138  ->setMethods(['getAttributeCode'])
139  ->disableOriginalConstructor()
140  ->getMockForAbstractClass();
141  $productAttributeMock->expects($this->any())
142  ->method('getAttributeCode')
143  ->willReturn($attributeCode);
144  $this->eavConfig->expects($this->any())->method('getEntityAttributes')
145  ->willReturn([$productAttributeMock]);
146 
147  $attributeMock = $this->getMockBuilder(AttributeAdapter::class)
148  ->disableOriginalConstructor()
149  ->setMethods(['isComplexType', 'getAttributeCode'])
150  ->getMock();
151  $attributeMock->expects($this->any())
152  ->method('isComplexType')
153  ->willReturn($isComplexType);
154  $attributeMock->expects($this->any())
155  ->method('getAttributeCode')
156  ->willReturn($attributeCode);
157  $this->attributeAdapterProvider->expects($this->any())
158  ->method('getByAttributeCode')
159  ->with($this->anything())
160  ->willReturn($attributeMock);
161  $this->fieldTypeConverter->expects($this->any())
162  ->method('convert')
163  ->with($this->anything())
164  ->will($this->returnCallback(
165  function ($type) use ($complexType) {
166  static $callCount = [];
167  $callCount[$type] = !isset($callCount[$type]) ? 1 : ++$callCount[$type];
168 
169  if ($type === 'string') {
170  return 'string';
171  }
172  if ($type === 'string') {
173  return 'string';
174  } elseif ($type === 'float') {
175  return 'float';
176  } else {
177  return $complexType;
178  }
179  }
180  ));
181 
182  $this->assertEquals(
183  $expected,
184  $this->provider->getFields(['storeId' => 1])
185  );
186  }
187 
191  public function attributeProvider()
192  {
193  return [
194  [
195  'category_ids',
196  'select',
197  true,
198  true,
199  'text',
200  [
201  'category_ids' => [
202  'type' => 'select',
203  'index' => true
204  ],
205  'category_ids_value' => [
206  'type' => 'string'
207  ],
208  'store_id' => [
209  'type' => 'string',
210  'index' => 'no'
211  ]
212  ]
213  ],
214  [
215  'attr_code',
216  'text',
217  'no',
218  false,
219  null,
220  [
221  'attr_code' => [
222  'type' => 'text',
223  'index' => 'no'
224  ],
225  'store_id' => [
226  'type' => 'string',
227  'index' => 'no'
228  ]
229  ],
230  ],
231  [
232  'attr_code',
233  'text',
234  null,
235  false,
236  null,
237  [
238  'attr_code' => [
239  'type' => 'text'
240  ],
241  'store_id' => [
242  'type' => 'string',
243  'index' => 'no'
244  ]
245  ]
246  ]
247  ];
248  }
249 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$objectManager
Definition: bootstrap.php:17
testGetAllAttributesTypes( $attributeCode, $inputType, $indexType, $isComplexType, $complexType, $expected)
$type
Definition: item.phtml:13
$attributeCode
Definition: extend.phtml:12