|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Category; |
| 10 | + |
| 11 | +use Magento\Framework\Controller\Result\JsonFactory; |
| 12 | +use Magento\Catalog\Controller\Adminhtml\Category\RefreshPath; |
| 13 | +use Magento\Backend\App\Action\Context; |
| 14 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 15 | + |
| 16 | +/** |
| 17 | + * Test for class RefreshPath. |
| 18 | + */ |
| 19 | +class RefreshPathTest extends \PHPUnit\Framework\TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var JsonFactory|\PHPUnit_Framework_MockObject_MockObject |
| 23 | + */ |
| 24 | + private $resultJsonFactoryMock; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var Context|\PHPUnit_Framework_MockObject_MockObject |
| 28 | + */ |
| 29 | + private $contextMock; |
| 30 | + |
| 31 | + /** |
| 32 | + * {@inheritDoc} |
| 33 | + */ |
| 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 | + |
| 47 | + /** |
| 48 | + * Sets object non-public property. |
| 49 | + * |
| 50 | + * @param mixed $object |
| 51 | + * @param string $propertyName |
| 52 | + * @param mixed $value |
| 53 | + * |
| 54 | + * @return void |
| 55 | + */ |
| 56 | + private function setObjectProperty($object, string $propertyName, $value) |
| 57 | + { |
| 58 | + $reflectionClass = new \ReflectionClass($object); |
| 59 | + $reflectionProperty = $reflectionClass->getProperty($propertyName); |
| 60 | + $reflectionProperty->setAccessible(true); |
| 61 | + $reflectionProperty->setValue($object, $value); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @return void |
| 66 | + */ |
| 67 | + public function testExecute() |
| 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 | + |
| 116 | + /** |
| 117 | + * @return void |
| 118 | + */ |
| 119 | + public function testExecuteWithoutCategoryId() |
| 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 | +} |
0 commit comments