Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PositionTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
16 
20 class PositionTest extends \PHPUnit\Framework\TestCase
21 {
25  private $resolver;
26 
30  private $storeManager;
31 
35  private $coreRegistry;
36 
42  protected function setUp()
43  {
44  $this->storeManager = $this->getMockBuilder(StoreManager::class)
45  ->disableOriginalConstructor()
46  ->setMethods(['getStore'])
47  ->getMockForAbstractClass();
48  $this->coreRegistry = $this->getMockBuilder(Registry::class)
49  ->disableOriginalConstructor()
50  ->setMethods(['registry'])
51  ->getMock();
52 
53  $objectManager = new ObjectManagerHelper($this);
54 
55  $this->resolver = $objectManager->getObject(
56  \Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\Resolver\Position::class,
57  [
58  'storeManager' => $this->storeManager,
59  'coreRegistry' => $this->coreRegistry,
60  ]
61  );
62  }
63 
72  public function testGetFieldName($attributeCode, $context, $fromRegistry, $expected)
73  {
74  $attributeMock = $this->getMockBuilder(AttributeAdapter::class)
75  ->disableOriginalConstructor()
76  ->setMethods(['getAttributeCode'])
77  ->getMock();
78  $attributeMock->expects($this->any())
79  ->method('getAttributeCode')
80  ->willReturn($attributeCode);
81  $store = $this->getMockBuilder(StoreInterface::class)
82  ->disableOriginalConstructor()
83  ->setMethods(['getRootCategoryId'])
84  ->getMockForAbstractClass();
85  $store->expects($this->any())
86  ->method('getRootCategoryId')
87  ->willReturn(2);
88  $this->storeManager->expects($this->any())
89  ->method('getStore')
90  ->willReturn($store);
91  $category = null;
92  if ($fromRegistry) {
93  $category = $this->getMockBuilder(CategoryInterface::class)
94  ->disableOriginalConstructor()
95  ->setMethods(['getId'])
96  ->getMockForAbstractClass();
97  $category->expects($this->any())
98  ->method('getId')
99  ->willReturn(1);
100  }
101  $this->coreRegistry->expects($this->any())
102  ->method('registry')
103  ->willReturn($category);
104 
105  $this->assertEquals(
106  $expected,
107  $this->resolver->getFieldName($attributeMock, $context)
108  );
109  }
110 
114  public function getFieldNameProvider()
115  {
116  return [
117  ['position', [], true, 'position_category_1'],
118  ['position', [], false, 'position_category_2'],
119  ['position', ['categoryId' => 2], false, 'position_category_2'],
120  ['price', ['categoryId' => 2], false, ''],
121  ];
122  }
123 }
$objectManager
Definition: bootstrap.php:17
$attributeCode
Definition: extend.phtml:12