Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractCategoryTest.php
Go to the documentation of this file.
1 <?php
8 
9 class AbstractCategoryTest extends \PHPUnit\Framework\TestCase
10 {
14  protected $objectManager;
15 
19  protected $contextMock;
20 
24  protected $storeManagerMock;
25 
29  protected $requestMock;
30 
34  protected $urlBuilderMock;
35 
39  protected $storeMock;
40 
44  protected $category;
45 
46  protected function setUp()
47  {
48  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
49 
50  $this->contextMock = $this->createMock(\Magento\Backend\Block\Template\Context::class);
51 
52  $this->requestMock = $this->getMockBuilder(
53  \Magento\Framework\App\RequestInterface::class
54  )
55  ->disableOriginalConstructor()
56  ->getMock();
57 
58  $this->contextMock->expects($this->any())
59  ->method('getRequest')
60  ->will($this->returnValue($this->requestMock));
61 
62  $this->urlBuilderMock = $this->getMockBuilder(
63  \Magento\Framework\UrlInterface::class
64  )
65  ->disableOriginalConstructor()
66  ->getMock();
67 
68  $this->storeManagerMock = $this->getMockBuilder(
69  \Magento\Store\Model\StoreManagerInterface::class
70  )
71  ->disableOriginalConstructor()
72  ->getMock();
73 
74  $this->contextMock->expects($this->any())
75  ->method('getStoreManager')
76  ->will($this->returnValue($this->storeManagerMock));
77 
78  $this->storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
79  ->disableOriginalConstructor()
80  ->getMock();
81 
82  $this->contextMock->expects($this->any())
83  ->method('getUrlBuilder')
84  ->will($this->returnValue($this->urlBuilderMock));
85 
86  $this->category = $this->objectManager->getObject(
87  \Magento\Catalog\Block\Adminhtml\Category\AbstractCategory::class,
88  [
89  'context' => $this->contextMock,
90  ]
91  );
92  }
93 
98  public function testGetSaveUrl()
99  {
100  $storeId = 23;
101  $saveUrl = 'save URL';
102  $params = ['_current' => false, '_query' => false, 'store' => $storeId];
103 
104  $this->requestMock->expects($this->once())->method('getParam')->with('store')->willReturn($storeId);
105  $this->storeManagerMock->expects($this->once())
106  ->method('getStore')
107  ->with($storeId)
108  ->willReturn($this->storeMock);
109  $this->storeMock->expects($this->once())->method('getId')->willReturn($storeId);
110 
111  $this->urlBuilderMock->expects($this->once())
112  ->method('getUrl')
113  ->with('catalog/*/save', $params)
114  ->willReturn($saveUrl);
115 
116  $this->assertEquals($saveUrl, $this->category->getSaveUrl());
117  }
118 
119  public function testGetRootIdsFromCache()
120  {
121  $this->category->setData('root_ids', ['ids']);
122  $this->storeManagerMock->expects($this->never())->method('getGroups');
123 
124  $this->assertEquals(['ids'], $this->category->getRootIds());
125  }
126 
127  public function testGetRootIds()
128  {
129  $this->storeManagerMock->expects($this->once())->method('getGroups')->willReturn([$this->storeMock]);
130  $this->storeMock->expects($this->once())->method('getRootCategoryId')->willReturn('storeId');
131 
132  $this->assertEquals([\Magento\Catalog\Model\Category::TREE_ROOT_ID, 'storeId'], $this->category->getRootIds());
133  }
134 }
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18