Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DynamicFieldTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
17  as FieldTypeConverterInterface;
19  as IndexTypeConverterInterface;
26  as FieldNameResolver;
27 
31 class DynamicFieldTest extends \PHPUnit\Framework\TestCase
32 {
36  private $provider;
37 
41  private $groupRepository;
42 
46  private $searchCriteriaBuilder;
47 
51  private $fieldTypeConverter;
52 
56  private $indexTypeConverter;
57 
61  private $attributeAdapterProvider;
62 
66  private $categoryList;
67 
71  private $fieldNameResolver;
72 
78  protected function setUp()
79  {
80  $this->groupRepository = $this->getMockBuilder(GroupRepositoryInterface::class)
81  ->disableOriginalConstructor()
82  ->getMock();
83  $this->searchCriteriaBuilder = $this->getMockBuilder(SearchCriteriaBuilder::class)
84  ->disableOriginalConstructor()
85  ->getMock();
86  $this->fieldTypeConverter = $this->getMockBuilder(FieldTypeConverterInterface::class)
87  ->disableOriginalConstructor()
88  ->getMock();
89  $this->indexTypeConverter = $this->getMockBuilder(IndexTypeConverterInterface::class)
90  ->disableOriginalConstructor()
91  ->getMock();
92  $this->attributeAdapterProvider = $this->getMockBuilder(AttributeProvider::class)
93  ->disableOriginalConstructor()
94  ->setMethods(['getByAttributeCode', 'getByAttribute'])
95  ->getMock();
96  $this->fieldNameResolver = $this->getMockBuilder(FieldNameResolver::class)
97  ->disableOriginalConstructor()
98  ->setMethods(['getFieldName'])
99  ->getMock();
100  $this->categoryList = $this->getMockBuilder(CategoryListInterface::class)
101  ->disableOriginalConstructor()
102  ->getMock();
103 
104  $objectManager = new ObjectManagerHelper($this);
105 
106  $this->provider = $objectManager->getObject(
107  \Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\DynamicField::class,
108  [
109  'groupRepository' => $this->groupRepository,
110  'searchCriteriaBuilder' => $this->searchCriteriaBuilder,
111  'fieldTypeConverter' => $this->fieldTypeConverter,
112  'indexTypeConverter' => $this->indexTypeConverter,
113  'attributeAdapterProvider' => $this->attributeAdapterProvider,
114  'categoryList' => $this->categoryList,
115  'fieldNameResolver' => $this->fieldNameResolver,
116  ]
117  );
118  }
119 
129  public function testGetAllAttributesTypes(
130  $complexType,
131  $categoryId,
132  $groupId,
133  $expected
134  ) {
135  $searchCriteria = $this->getMockBuilder(SearchCriteria::class)
136  ->disableOriginalConstructor()
137  ->getMock();
138  $this->searchCriteriaBuilder->expects($this->any())
139  ->method('create')
140  ->willReturn($searchCriteria);
141  $categorySearchResults = $this->getMockBuilder(CategorySearchResultsInterface::class)
142  ->disableOriginalConstructor()
143  ->setMethods(['getItems'])
144  ->getMockForAbstractClass();
145  $groupSearchResults = $this->getMockBuilder(GroupSearchResultsInterface::class)
146  ->disableOriginalConstructor()
147  ->setMethods(['getItems'])
148  ->getMockForAbstractClass();
149  $group = $this->getMockBuilder(GroupInterface::class)
150  ->disableOriginalConstructor()
151  ->setMethods(['getId'])
152  ->getMockForAbstractClass();
153  $group->expects($this->any())
154  ->method('getId')
155  ->willReturn($groupId);
156  $groupSearchResults->expects($this->any())
157  ->method('getItems')
158  ->willReturn([$group]);
159  $category = $this->getMockBuilder(CategoryInterface::class)
160  ->disableOriginalConstructor()
161  ->setMethods(['getId'])
162  ->getMockForAbstractClass();
163  $category->expects($this->any())
164  ->method('getId')
165  ->willReturn($categoryId);
166  $categorySearchResults->expects($this->any())
167  ->method('getItems')
168  ->willReturn([$category]);
169  $this->categoryList->expects($this->any())
170  ->method('getList')
171  ->willReturn($categorySearchResults);
172 
173  $categoryAttributeMock = $this->getMockBuilder(AttributeAdapter::class)
174  ->disableOriginalConstructor()
175  ->setMethods(['getAttributeCode'])
176  ->getMock();
177  $categoryAttributeMock->expects($this->any())
178  ->method('getAttributeCode')
179  ->willReturn('category');
180  $positionAttributeMock = $this->getMockBuilder(AttributeAdapter::class)
181  ->disableOriginalConstructor()
182  ->setMethods(['getAttributeCode'])
183  ->getMock();
184  $positionAttributeMock->expects($this->any())
185  ->method('getAttributeCode')
186  ->willReturn('position');
187 
188  $this->fieldNameResolver->expects($this->any())
189  ->method('getFieldName')
190  ->will($this->returnCallback(
191  function ($attribute) use ($categoryId) {
192  static $callCount = [];
193  $attributeCode = $attribute->getAttributeCode();
194  $callCount[$attributeCode] = !isset($callCount[$attributeCode]) ? 1 : ++$callCount[$attributeCode];
195 
196  if ($attributeCode === 'category') {
197  return 'category_name_' . $categoryId;
198  } elseif ($attributeCode === 'position') {
199  return 'position_' . $categoryId;
200  } elseif ($attributeCode === 'price') {
201  return 'price_' . $categoryId . '_1';
202  }
203  }
204  ));
205  $priceAttributeMock = $this->getMockBuilder(AttributeAdapter::class)
206  ->disableOriginalConstructor()
207  ->setMethods(['getAttributeCode'])
208  ->getMock();
209  $priceAttributeMock->expects($this->any())
210  ->method('getAttributeCode')
211  ->willReturn('price');
212  $this->indexTypeConverter->expects($this->any())
213  ->method('convert')
214  ->willReturn('no_index');
215  $this->groupRepository->expects($this->any())
216  ->method('getList')
217  ->willReturn($groupSearchResults);
218  $this->attributeAdapterProvider->expects($this->any())
219  ->method('getByAttributeCode')
220  ->with($this->anything())
221  ->will($this->returnCallback(
222  function ($code) use (
223  $categoryAttributeMock,
224  $positionAttributeMock,
225  $priceAttributeMock
226  ) {
227  static $callCount = [];
228  $callCount[$code] = !isset($callCount[$code]) ? 1 : ++$callCount[$code];
229 
230  if ($code === 'position') {
231  return $positionAttributeMock;
232  } elseif ($code === 'category_name') {
233  return $categoryAttributeMock;
234  } elseif ($code === 'price') {
235  return $priceAttributeMock;
236  }
237  }
238  ));
239  $this->fieldTypeConverter->expects($this->any())
240  ->method('convert')
241  ->with($this->anything())
242  ->will($this->returnCallback(
243  function ($type) use ($complexType) {
244  static $callCount = [];
245  $callCount[$type] = !isset($callCount[$type]) ? 1 : ++$callCount[$type];
246 
247  if ($type === 'string') {
248  return 'string';
249  }
250  if ($type === 'string') {
251  return 'string';
252  } elseif ($type === 'float') {
253  return 'float';
254  } else {
255  return $complexType;
256  }
257  }
258  ));
259 
260  $this->assertEquals(
261  $expected,
262  $this->provider->getFields(['websiteId' => 1])
263  );
264  }
265 
269  public function attributeProvider()
270  {
271  return [
272  [
273  'text',
274  1,
275  1,
276  [
277  'category_name_1' => [
278  'type' => 'string',
279  'index' => 'no_index'
280  ],
281  'position_1' => [
282  'type' => 'string',
283  'index' => 'no_index'
284  ],
285  'price_1_1' => [
286  'type' => 'float',
287  'store' => true
288  ]
289  ]
290  ],
291  [
292  null,
293  1,
294  1,
295  [
296  'category_name_1' => [
297  'type' => 'string',
298  'index' => 'no_index'
299  ],
300  'position_1' => [
301  'type' => 'string',
302  'index' => 'no_index'
303  ],
304  'price_1_1' => [
305  'type' => 'float',
306  'store' => true
307  ]
308  ],
309  ],
310  [
311  null,
312  1,
313  1,
314  [
315  'category_name_1' => [
316  'type' => 'string',
317  'index' => 'no_index'
318  ],
319  'position_1' => [
320  'type' => 'string',
321  'index' => 'no_index'
322  ],
323  'price_1_1' => [
324  'type' => 'float',
325  'store' => true
326  ]
327  ]
328  ]
329  ];
330  }
331 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$objectManager
Definition: bootstrap.php:17
$group
Definition: sections.phtml:16
$searchCriteria
$type
Definition: item.phtml:13
$attributeCode
Definition: extend.phtml:12
$code
Definition: info.phtml:12