Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GridTest.php
Go to the documentation of this file.
1 <?php
7 
12 class GridTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $_block;
18 
22  protected $_layoutMock;
23 
27  protected $_columnSetMock;
28 
29  protected function setUp()
30  {
31  $this->_layoutMock = $this->createPartialMock(
32  \Magento\Framework\View\Layout::class,
33  ['getChildName', 'getBlock', 'createBlock', 'helper', 'renameElement', 'unsetChild', 'setChild']
34  );
35  $this->_columnSetMock = $this->_getColumnSetMock();
36 
37  $returnValueMap = [
38  ['grid', 'grid.columnSet', 'grid.columnSet'],
39  ['grid', 'reset_filter_button', 'reset_filter_button'],
40  ['grid', 'search_button', 'search_button'],
41  ];
42  $this->_layoutMock->expects(
43  $this->any()
44  )->method(
45  'getChildName'
46  )->will(
47  $this->returnValueMap($returnValueMap)
48  );
49  $this->_layoutMock->expects(
50  $this->any()
51  )->method(
52  'getBlock'
53  )->with(
54  'grid.columnSet'
55  )->will(
56  $this->returnValue($this->_columnSetMock)
57  );
58  $this->_layoutMock->expects(
59  $this->any()
60  )->method(
61  'createBlock'
62  )->with(
63  \Magento\Backend\Block\Widget\Button::class
64  )->will(
65  $this->returnValue(
66  \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
67  \Magento\Framework\View\LayoutInterface::class
68  )->createBlock(
69  \Magento\Backend\Block\Widget\Button::class
70  )
71  )
72  );
73  $this->_layoutMock->expects(
74  $this->any()
75  )->method(
76  'helper'
77  )->with(
78  \Magento\Framework\Json\Helper\Data::class
79  )->will(
80  $this->returnValue(
81  \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
82  \Magento\Framework\Json\Helper\Data::class
83  )
84  )
85  );
86 
88  \Magento\Framework\View\LayoutInterface::class
89  )->createBlock(
90  \Magento\Backend\Block\Widget\Grid::class
91  );
92  $this->_block->setLayout($this->_layoutMock);
93  $this->_block->setNameInLayout('grid');
94  }
95 
101  protected function _getColumnSetMock()
102  {
104  $directoryList = $objectManager->create(
105  \Magento\Framework\App\Filesystem\DirectoryList::class,
106  ['root' => __DIR__]
107  );
108  return $this->getMockBuilder(\Magento\Backend\Block\Widget\Grid\ColumnSet::class)
109  ->setConstructorArgs(
110  [
111  $objectManager->create(
112  \Magento\Framework\View\Element\Template\Context::class,
113  [
114  'filesystem' => $objectManager->create(
115  \Magento\Framework\Filesystem::class,
116  ['directoryList' => $directoryList]
117  )
118  ]
119  ),
120  $objectManager->create(\Magento\Backend\Model\Widget\Grid\Row\UrlGeneratorFactory::class),
121  $objectManager->create(\Magento\Backend\Model\Widget\Grid\SubTotals::class),
122  $objectManager->create(\Magento\Backend\Model\Widget\Grid\Totals::class)
123  ]
124  )
125  ->getMock();
126  }
127 
128  public function testToHtmlPreparesColumns()
129  {
130  $this->_columnSetMock->expects($this->once())->method('setRendererType');
131  $this->_columnSetMock->expects($this->once())->method('setFilterType');
132  $this->_columnSetMock->expects($this->once())->method('setSortable');
133  $this->_block->setColumnRenderers(['filter' => 'Filter_Class']);
134  $this->_block->setColumnFilters(['filter' => 'Filter_Class']);
135  $this->_block->setSortable(false);
136  $this->_block->toHtml();
137  }
138 
140  {
141  $this->_columnSetMock->expects($this->once())->method('isFilterVisible')->will($this->returnValue(false));
142  $this->_block->getMainButtonsHtml();
143  }
144 
145  public function testGetMassactionBlock()
146  {
149  \Magento\Framework\View\LayoutInterface::class
150  );
152  $block = $layout->createBlock(\Magento\Backend\Block\Widget\Grid\Extended::class, 'block');
153  $child = $layout->addBlock(\Magento\Framework\View\Element\Template::class, 'massaction', 'block');
154  $this->assertSame($child, $block->getMassactionBlock());
155  }
156 }
$objectManager
Definition: bootstrap.php:17
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$block
Definition: block.php:8