|
| 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 | +class RefreshPathTest extends \PHPUnit\Framework\TestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var JsonFactory|\PHPUnit_Framework_MockObject_MockObject |
| 20 | + */ |
| 21 | + private $resultJsonFactoryMock; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var Context|\PHPUnit_Framework_MockObject_MockObject |
| 25 | + */ |
| 26 | + private $contextMock; |
| 27 | + |
| 28 | + /** |
| 29 | + * {@inheritDoc} |
| 30 | + */ |
| 31 | + protected function setUp() |
| 32 | + { |
| 33 | + $this->resultJsonFactoryMock = $this->getMockBuilder(JsonFactory::class) |
| 34 | + ->disableOriginalConstructor() |
| 35 | + ->setMethods(['create', 'setData']) |
| 36 | + ->getMock(); |
| 37 | + |
| 38 | + $this->contextMock = $this->getMockBuilder(Context::class) |
| 39 | + ->disableOriginalConstructor() |
| 40 | + ->setMethods(['getRequest']) |
| 41 | + ->getMock(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Sets object non-public property. |
| 46 | + * |
| 47 | + * @param mixed $object |
| 48 | + * @param string $propertyName |
| 49 | + * @param mixed $value |
| 50 | + * |
| 51 | + * @return void |
| 52 | + */ |
| 53 | + private function setObjectProperty($object, string $propertyName, $value) |
| 54 | + { |
| 55 | + $reflectionClass = new \ReflectionClass($object); |
| 56 | + $reflectionProperty = $reflectionClass->getProperty($propertyName); |
| 57 | + $reflectionProperty->setAccessible(true); |
| 58 | + $reflectionProperty->setValue($object, $value); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @return void |
| 63 | + */ |
| 64 | + public function testExecute() |
| 65 | + { |
| 66 | + $value = ['id' => 3, 'path' => '1/2/3', 'parentId' => 2]; |
| 67 | + $result = '{"id":3,"path":"1/2/3","parentId":"2"}'; |
| 68 | + |
| 69 | + $requestMock = $this->getMockForAbstractClass(\Magento\Framework\App\RequestInterface::class); |
| 70 | + |
| 71 | + $refreshPath = $this->getMockBuilder(RefreshPath::class) |
| 72 | + ->setMethods(['getRequest', 'create']) |
| 73 | + ->setConstructorArgs([ |
| 74 | + $this->contextMock, |
| 75 | + $this->resultJsonFactoryMock, |
| 76 | + ])->getMock(); |
| 77 | + |
| 78 | + $refreshPath->expects($this->any())->method('getRequest')->willReturn($requestMock); |
| 79 | + $requestMock->expects($this->any())->method('getParam')->with('id')->willReturn($value['id']); |
| 80 | + |
| 81 | + $categoryMock = $this->getMockBuilder(\Magento\Catalog\Model\Category::class) |
| 82 | + ->disableOriginalConstructor() |
| 83 | + ->setMethods(['getPath', 'getParentID', 'getResource']) |
| 84 | + ->getMock(); |
| 85 | + |
| 86 | + $categoryMock->expects($this->any())->method('getPath')->willReturn($value['path']); |
| 87 | + $categoryMock->expects($this->any())->method('getParentID')->willReturn($value['parentId']); |
| 88 | + |
| 89 | + $categoryResource = $this->createMock(\Magento\Catalog\Model\ResourceModel\Category::class); |
| 90 | + |
| 91 | + $objectManagerMock = $this->getMockBuilder(ObjectManager::class) |
| 92 | + ->disableOriginalConstructor() |
| 93 | + ->setMethods(['create']) |
| 94 | + ->getMock(); |
| 95 | + |
| 96 | + $this->setObjectProperty($refreshPath, '_objectManager', $objectManagerMock); |
| 97 | + $this->setObjectProperty($categoryMock, '_resource', $categoryResource); |
| 98 | + |
| 99 | + $objectManagerMock->expects($this->once()) |
| 100 | + ->method('create') |
| 101 | + ->with(\Magento\Catalog\Model\Category::class) |
| 102 | + ->willReturn($categoryMock); |
| 103 | + |
| 104 | + $this->resultJsonFactoryMock->expects($this->any())->method('create')->willReturnSelf(); |
| 105 | + $this->resultJsonFactoryMock->expects($this->any()) |
| 106 | + ->method('setData') |
| 107 | + ->with($value) |
| 108 | + ->willReturn($result); |
| 109 | + |
| 110 | + $this->assertEquals($result, $refreshPath->execute()); |
| 111 | + } |
| 112 | +} |
0 commit comments