Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractTotalsTest.php
Go to the documentation of this file.
1 <?php
7 
8 class AbstractTotalsTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $_model;
14 
18  protected $_parserMock;
19 
23  protected $_factoryMock;
24 
30  protected $_columnsValueMap;
31 
32  protected function setUp()
33  {
34  $this->_prepareParserMock();
35  $this->_prepareFactoryMock();
36 
37  $arguments = ['factory' => $this->_factoryMock, 'parser' => $this->_parserMock];
38  $this->_model = $this->getMockForAbstractClass(
39  \Magento\Backend\Model\Widget\Grid\AbstractTotals::class,
40  $arguments,
41  '',
42  true,
43  false,
44  true,
45  []
46  );
47  $this->_model->expects($this->any())->method('_countSum')->will($this->returnValue(2));
48  $this->_model->expects($this->any())->method('_countAverage')->will($this->returnValue(2));
49 
50  $this->_setUpColumns();
51  }
52 
53  protected function tearDown()
54  {
55  unset($this->_parserMock);
56  unset($this->_factoryMock);
57  }
58 
64  protected function _getTestCollection()
65  {
66  $collection = new \Magento\Framework\Data\Collection(
67  $this->createMock(\Magento\Framework\Data\Collection\EntityFactory::class)
68  );
69  $items = [new \Magento\Framework\DataObject(['test1' => '1', 'test2' => '2'])];
70  foreach ($items as $item) {
71  $collection->addItem($item);
72  }
73 
74  return $collection;
75  }
76 
80  protected function _setUpColumns()
81  {
82  $columns = [
83  'test1' => 'sum',
84  'test2' => 'avg',
85  'test3' => 'test1+test2',
86  'test4' => 'test1-test2',
87  'test5' => 'test1*test2',
88  'test6' => 'test1/test2',
89  'test7' => 'test1/0',
90  ];
91 
92  foreach ($columns as $index => $expression) {
93  $this->_model->setColumn($index, $expression);
94  }
95  }
96 
100  protected function _prepareParserMock()
101  {
102  $this->_parserMock = $this->createPartialMock(
103  \Magento\Backend\Model\Widget\Grid\Parser::class,
104  ['parseExpression', 'isOperation']
105  );
106 
107  $columnsValueMap = [
108  ['test1+test2', ['test1', 'test2', '+']],
109  ['test1-test2', ['test1', 'test2', '-']],
110  ['test1*test2', ['test1', 'test2', '*']],
111  ['test1/test2', ['test1', 'test2', '/']],
112  ['test1/0', ['test1', '0', '/']],
113  ];
114  $this->_parserMock->expects(
115  $this->any()
116  )->method(
117  'parseExpression'
118  )->will(
119  $this->returnValueMap($columnsValueMap)
120  );
121 
122  $isOperationValueMap = [
123  ['+', true],
124  ['-', true],
125  ['*', true],
126  ['/', true],
127  ['test1', false],
128  ['test2', false],
129  ['0', false],
130  ];
131  $this->_parserMock->expects(
132  $this->any()
133  )->method(
134  'isOperation'
135  )->will(
136  $this->returnValueMap($isOperationValueMap)
137  );
138  }
139 
143  protected function _prepareFactoryMock()
144  {
145  $this->_factoryMock = $this->createPartialMock(\Magento\Framework\DataObject\Factory::class, ['create']);
146 
147  $createValueMap = [
148  [
149  [
150  'test1' => 2,
151  'test2' => 2,
152  'test3' => 4,
153  'test4' => 0,
154  'test5' => 4,
155  'test6' => 1,
156  'test7' => 0,
157  ],
158  new \Magento\Framework\DataObject(
159  [
160  'test1' => 2,
161  'test2' => 2,
162  'test3' => 4,
163  'test4' => 0,
164  'test5' => 4,
165  'test6' => 1,
166  'test7' => 0,
167  ]
168  ),
169  ],
170  [[], new \Magento\Framework\DataObject()],
171  ];
172  $this->_factoryMock->expects($this->any())->method('create')->will($this->returnValueMap($createValueMap));
173  }
174 
175  public function testColumns()
176  {
177  $expected = [
178  'test1' => 'sum',
179  'test2' => 'avg',
180  'test3' => 'test1+test2',
181  'test4' => 'test1-test2',
182  'test5' => 'test1*test2',
183  'test6' => 'test1/test2',
184  'test7' => 'test1/0',
185  ];
186 
187  $this->assertEquals($expected, $this->_model->getColumns());
188  }
189 
190  public function testCountTotals()
191  {
192  $expected = new \Magento\Framework\DataObject(
193  ['test1' => 2, 'test2' => 2, 'test3' => 4, 'test4' => 0, 'test5' => 4, 'test6' => 1, 'test7' => 0]
194  );
195  $this->assertEquals($expected, $this->_model->countTotals($this->_getTestCollection()));
196  }
197 
198  public function testReset()
199  {
200  $this->_model->countTotals($this->_getTestCollection());
201  $this->_model->reset();
202 
203  $this->assertEquals(new \Magento\Framework\DataObject(), $this->_model->getTotals());
204  $this->assertNotEmpty($this->_model->getColumns());
205  }
206 
207  public function testResetFull()
208  {
209  $this->_model->countTotals($this->_getTestCollection());
210  $this->_model->reset(true);
211 
212  $this->assertEquals(new \Magento\Framework\DataObject(), $this->_model->getTotals());
213  $this->assertEmpty($this->_model->getColumns());
214  }
215 }
$columns
Definition: default.phtml:15
$arguments
$index
Definition: list.phtml:44
$items