Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RefreshPathTest.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
15 
19 class RefreshPathTest extends \PHPUnit\Framework\TestCase
20 {
24  private $resultJsonFactoryMock;
25 
29  private $contextMock;
30 
34  protected function setUp()
35  {
36  $this->resultJsonFactoryMock = $this->getMockBuilder(JsonFactory::class)
37  ->disableOriginalConstructor()
38  ->setMethods(['create', 'setData'])
39  ->getMock();
40 
41  $this->contextMock = $this->getMockBuilder(Context::class)
42  ->disableOriginalConstructor()
43  ->setMethods(['getRequest'])
44  ->getMock();
45  }
46 
56  private function setObjectProperty($object, string $propertyName, $value) : void
57  {
58  $reflectionClass = new \ReflectionClass($object);
59  $reflectionProperty = $reflectionClass->getProperty($propertyName);
60  $reflectionProperty->setAccessible(true);
61  $reflectionProperty->setValue($object, $value);
62  }
63 
67  public function testExecute() : void
68  {
69  $value = ['id' => 3, 'path' => '1/2/3', 'parentId' => 2];
70  $result = '{"id":3,"path":"1/2/3","parentId":"2"}';
71 
72  $requestMock = $this->getMockForAbstractClass(\Magento\Framework\App\RequestInterface::class);
73 
74  $refreshPath = $this->getMockBuilder(RefreshPath::class)
75  ->setMethods(['getRequest', 'create'])
76  ->setConstructorArgs([
77  $this->contextMock,
78  $this->resultJsonFactoryMock,
79  ])->getMock();
80 
81  $refreshPath->expects($this->any())->method('getRequest')->willReturn($requestMock);
82  $requestMock->expects($this->any())->method('getParam')->with('id')->willReturn($value['id']);
83 
84  $categoryMock = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
85  ->disableOriginalConstructor()
86  ->setMethods(['getPath', 'getParentId', 'getResource'])
87  ->getMock();
88 
89  $categoryMock->expects($this->any())->method('getPath')->willReturn($value['path']);
90  $categoryMock->expects($this->any())->method('getParentId')->willReturn($value['parentId']);
91 
92  $categoryResource = $this->createMock(\Magento\Catalog\Model\ResourceModel\Category::class);
93 
94  $objectManagerMock = $this->getMockBuilder(ObjectManager::class)
95  ->disableOriginalConstructor()
96  ->setMethods(['create'])
97  ->getMock();
98 
99  $this->setObjectProperty($refreshPath, '_objectManager', $objectManagerMock);
100  $this->setObjectProperty($categoryMock, '_resource', $categoryResource);
101 
102  $objectManagerMock->expects($this->once())
103  ->method('create')
104  ->with(\Magento\Catalog\Model\Category::class)
105  ->willReturn($categoryMock);
106 
107  $this->resultJsonFactoryMock->expects($this->any())->method('create')->willReturnSelf();
108  $this->resultJsonFactoryMock->expects($this->any())
109  ->method('setData')
110  ->with($value)
111  ->willReturn($result);
112 
113  $this->assertEquals($result, $refreshPath->execute());
114  }
115 
119  public function testExecuteWithoutCategoryId() : void
120  {
121  $requestMock = $this->getMockForAbstractClass(\Magento\Framework\App\RequestInterface::class);
122 
123  $refreshPath = $this->getMockBuilder(RefreshPath::class)
124  ->setMethods(['getRequest', 'create'])
125  ->setConstructorArgs([
126  $this->contextMock,
127  $this->resultJsonFactoryMock,
128  ])->getMock();
129 
130  $refreshPath->expects($this->any())->method('getRequest')->willReturn($requestMock);
131  $requestMock->expects($this->any())->method('getParam')->with('id')->willReturn(null);
132 
133  $objectManagerMock = $this->getMockBuilder(ObjectManager::class)
134  ->disableOriginalConstructor()
135  ->setMethods(['create'])
136  ->getMock();
137 
138  $this->setObjectProperty($refreshPath, '_objectManager', $objectManagerMock);
139 
140  $objectManagerMock->expects($this->never())
141  ->method('create')
142  ->with(\Magento\Catalog\Model\Category::class)
143  ->willReturnSelf();
144 
145  $refreshPath->execute();
146  }
147 }
$value
Definition: gender.phtml:16
$reflectionClass
Definition: categories.php:25