Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SubTotalsTest.php
Go to the documentation of this file.
1 <?php
7 
8 class SubTotalsTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $_model;
14 
18  protected $_parserMock;
19 
23  protected $_factoryMock;
24 
25  protected function setUp()
26  {
27  $this->_parserMock = $this->createMock(\Magento\Backend\Model\Widget\Grid\Parser::class);
28 
29  $this->_factoryMock = $this->createPartialMock(\Magento\Framework\DataObject\Factory::class, ['create']);
30  $this->_factoryMock->expects(
31  $this->any()
32  )->method(
33  'create'
34  )->with(
35  ['sub_test1' => 3, 'sub_test2' => 2]
36  )->will(
37  $this->returnValue(new \Magento\Framework\DataObject(['sub_test1' => 3, 'sub_test2' => 2]))
38  );
39 
40  $arguments = ['factory' => $this->_factoryMock, 'parser' => $this->_parserMock];
41 
42  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
43  $this->_model = $objectManagerHelper->getObject(
44  \Magento\Backend\Model\Widget\Grid\SubTotals::class,
46  );
47 
48  // setup columns
49  $columns = ['sub_test1' => 'sum', 'sub_test2' => 'avg'];
50  foreach ($columns as $index => $expression) {
51  $this->_model->setColumn($index, $expression);
52  }
53  }
54 
55  protected function tearDown()
56  {
57  unset($this->_parserMock);
58  unset($this->_factoryMock);
59  }
60 
61  public function testCountTotals()
62  {
63  $expected = new \Magento\Framework\DataObject(['sub_test1' => 3, 'sub_test2' => 2]);
64  $this->assertEquals($expected, $this->_model->countTotals($this->_getTestCollection()));
65  }
66 
72  protected function _getTestCollection()
73  {
74  $collection = new \Magento\Framework\Data\Collection(
75  $this->createMock(\Magento\Framework\Data\Collection\EntityFactory::class)
76  );
77  $items = [
78  new \Magento\Framework\DataObject(['sub_test1' => '1', 'sub_test2' => '2']),
79  new \Magento\Framework\DataObject(['sub_test1' => '1', 'sub_test2' => '2']),
80  new \Magento\Framework\DataObject(['sub_test1' => '1', 'sub_test2' => '2']),
81  ];
82  foreach ($items as $item) {
83  $collection->addItem($item);
84  }
85 
86  return $collection;
87  }
88 }
$columns
Definition: default.phtml:15
$arguments
$index
Definition: list.phtml:44
$items