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