Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CategoriesJsonTest.php
Go to the documentation of this file.
1 <?php
9 
13 class CategoriesJsonTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $controller;
19 
23  protected $registryMock;
24 
28  protected $responseMock;
29 
33  protected $requestMock;
34 
38  protected $viewMock;
39 
43  protected $chooserBlockMock;
44 
48  protected $layoutMock;
49 
53  protected $objectManagerMock;
54 
58  protected $resultJson;
59 
60  protected function setUp()
61  {
62  $this->responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
63  $this->requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
64  $this->viewMock = $this->createPartialMock(\Magento\Framework\App\View::class, ['getLayout']);
65  $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManager\ObjectManager::class);
66  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
67 
68  $context = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
69  ->setMethods(['getRequest', 'getResponse', 'getMessageManager', 'getSession'])
70  ->setConstructorArgs(
71  $helper->getConstructArguments(
72  \Magento\Backend\App\Action\Context::class,
73  [
74  'response' => $this->responseMock,
75  'request' => $this->requestMock,
76  'view' => $this->viewMock,
77  'objectManager' => $this->objectManagerMock
78  ]
79  )
80  )
81  ->getMock();
82 
83  $this->resultJson = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
84  ->disableOriginalConstructor()
85  ->getMock();
86  $resultJsonFactory = $this->getMockBuilder(\Magento\Framework\Controller\Result\JsonFactory::class)
87  ->disableOriginalConstructor()
88  ->setMethods(['create'])
89  ->getMock();
90  $resultJsonFactory->expects($this->atLeastOnce())
91  ->method('create')
92  ->willReturn($this->resultJson);
93 
94  $this->layoutMock = $this->createPartialMock(\Magento\Framework\View\Layout::class, ['createBlock']);
95 
96  $layoutFactory = $this->getMockBuilder(\Magento\Framework\View\LayoutFactory::class)
97  ->disableOriginalConstructor()
98  ->setMethods(['create'])
99  ->getMock();
100  $layoutFactory->expects($this->any())
101  ->method('create')
102  ->willReturn($this->layoutMock);
103 
104  $context->expects($this->once())->method('getRequest')->will($this->returnValue($this->requestMock));
105  $context->expects($this->once())->method('getResponse')->will($this->returnValue($this->responseMock));
106  $this->registryMock = $this->createMock(\Magento\Framework\Registry::class);
107  $this->controller = new \Magento\Catalog\Controller\Adminhtml\Category\Widget\CategoriesJson(
108  $context,
109  $layoutFactory,
110  $resultJsonFactory,
111  $this->registryMock
112  );
113  }
114 
115  protected function _getTreeBlock()
116  {
117  $this->chooserBlockMock = $this->createMock(\Magento\Catalog\Block\Adminhtml\Category\Widget\Chooser::class);
118  $this->layoutMock->expects($this->once())->method('createBlock')->will(
119  $this->returnValue($this->chooserBlockMock)
120  );
121  }
122 
123  public function testExecute()
124  {
125  $this->_getTreeBlock();
126  $testCategoryId = 1;
127 
128  $this->requestMock->expects($this->any())->method('getPost')->will($this->returnValue($testCategoryId));
129  $categoryMock = $this->createMock(\Magento\Catalog\Model\Category::class);
130  $categoryMock->expects($this->once())->method('load')->will($this->returnValue($categoryMock));
131  $categoryMock->expects($this->once())->method('getId')->will($this->returnValue($testCategoryId));
132  $this->objectManagerMock->expects($this->once())->method('create')
133  ->with($this->equalTo(\Magento\Catalog\Model\Category::class))->will($this->returnValue($categoryMock));
134 
135  $this->chooserBlockMock->expects($this->once())->method('setSelectedCategories')->will(
136  $this->returnValue($this->chooserBlockMock)
137  );
138  $testHtml = '<div>Some test html</div>';
139  $this->chooserBlockMock->expects($this->once())->method('getTreeJson')->will($this->returnValue($testHtml));
140  $this->resultJson->expects($this->once())->method('setJsonData')->with($testHtml)->willReturnSelf();
141  $this->controller->execute();
142  }
143 }
$helper
Definition: iframe.phtml:13