AbstractTotalsTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Test\Unit\Model\Widget\Grid;
  7. class AbstractTotalsTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var $_model \PHPUnit_Framework_MockObject_MockObject
  11. */
  12. protected $_model;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $_parserMock;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $_factoryMock;
  21. /**
  22. * Columns map for parserMock return expressions
  23. *
  24. * @var array
  25. */
  26. protected $_columnsValueMap;
  27. protected function setUp()
  28. {
  29. $this->_prepareParserMock();
  30. $this->_prepareFactoryMock();
  31. $arguments = ['factory' => $this->_factoryMock, 'parser' => $this->_parserMock];
  32. $this->_model = $this->getMockForAbstractClass(
  33. \Magento\Backend\Model\Widget\Grid\AbstractTotals::class,
  34. $arguments,
  35. '',
  36. true,
  37. false,
  38. true,
  39. []
  40. );
  41. $this->_model->expects($this->any())->method('_countSum')->will($this->returnValue(2));
  42. $this->_model->expects($this->any())->method('_countAverage')->will($this->returnValue(2));
  43. $this->_setUpColumns();
  44. }
  45. protected function tearDown()
  46. {
  47. unset($this->_parserMock);
  48. unset($this->_factoryMock);
  49. }
  50. /**
  51. * Retrieve test collection
  52. *
  53. * @return \Magento\Framework\Data\Collection
  54. */
  55. protected function _getTestCollection()
  56. {
  57. $collection = new \Magento\Framework\Data\Collection(
  58. $this->createMock(\Magento\Framework\Data\Collection\EntityFactory::class)
  59. );
  60. $items = [new \Magento\Framework\DataObject(['test1' => '1', 'test2' => '2'])];
  61. foreach ($items as $item) {
  62. $collection->addItem($item);
  63. }
  64. return $collection;
  65. }
  66. /**
  67. * Prepare tested model by setting columns
  68. */
  69. protected function _setUpColumns()
  70. {
  71. $columns = [
  72. 'test1' => 'sum',
  73. 'test2' => 'avg',
  74. 'test3' => 'test1+test2',
  75. 'test4' => 'test1-test2',
  76. 'test5' => 'test1*test2',
  77. 'test6' => 'test1/test2',
  78. 'test7' => 'test1/0',
  79. ];
  80. foreach ($columns as $index => $expression) {
  81. $this->_model->setColumn($index, $expression);
  82. }
  83. }
  84. /**
  85. * Prepare parser mock by setting test expressions for columns and operation used
  86. */
  87. protected function _prepareParserMock()
  88. {
  89. $this->_parserMock = $this->createPartialMock(
  90. \Magento\Backend\Model\Widget\Grid\Parser::class,
  91. ['parseExpression', 'isOperation']
  92. );
  93. $columnsValueMap = [
  94. ['test1+test2', ['test1', 'test2', '+']],
  95. ['test1-test2', ['test1', 'test2', '-']],
  96. ['test1*test2', ['test1', 'test2', '*']],
  97. ['test1/test2', ['test1', 'test2', '/']],
  98. ['test1/0', ['test1', '0', '/']],
  99. ];
  100. $this->_parserMock->expects(
  101. $this->any()
  102. )->method(
  103. 'parseExpression'
  104. )->will(
  105. $this->returnValueMap($columnsValueMap)
  106. );
  107. $isOperationValueMap = [
  108. ['+', true],
  109. ['-', true],
  110. ['*', true],
  111. ['/', true],
  112. ['test1', false],
  113. ['test2', false],
  114. ['0', false],
  115. ];
  116. $this->_parserMock->expects(
  117. $this->any()
  118. )->method(
  119. 'isOperation'
  120. )->will(
  121. $this->returnValueMap($isOperationValueMap)
  122. );
  123. }
  124. /**
  125. * Prepare factory mock for setting possible values
  126. */
  127. protected function _prepareFactoryMock()
  128. {
  129. $this->_factoryMock = $this->createPartialMock(\Magento\Framework\DataObject\Factory::class, ['create']);
  130. $createValueMap = [
  131. [
  132. [
  133. 'test1' => 2,
  134. 'test2' => 2,
  135. 'test3' => 4,
  136. 'test4' => 0,
  137. 'test5' => 4,
  138. 'test6' => 1,
  139. 'test7' => 0,
  140. ],
  141. new \Magento\Framework\DataObject(
  142. [
  143. 'test1' => 2,
  144. 'test2' => 2,
  145. 'test3' => 4,
  146. 'test4' => 0,
  147. 'test5' => 4,
  148. 'test6' => 1,
  149. 'test7' => 0,
  150. ]
  151. ),
  152. ],
  153. [[], new \Magento\Framework\DataObject()],
  154. ];
  155. $this->_factoryMock->expects($this->any())->method('create')->will($this->returnValueMap($createValueMap));
  156. }
  157. public function testColumns()
  158. {
  159. $expected = [
  160. 'test1' => 'sum',
  161. 'test2' => 'avg',
  162. 'test3' => 'test1+test2',
  163. 'test4' => 'test1-test2',
  164. 'test5' => 'test1*test2',
  165. 'test6' => 'test1/test2',
  166. 'test7' => 'test1/0',
  167. ];
  168. $this->assertEquals($expected, $this->_model->getColumns());
  169. }
  170. public function testCountTotals()
  171. {
  172. $expected = new \Magento\Framework\DataObject(
  173. ['test1' => 2, 'test2' => 2, 'test3' => 4, 'test4' => 0, 'test5' => 4, 'test6' => 1, 'test7' => 0]
  174. );
  175. $this->assertEquals($expected, $this->_model->countTotals($this->_getTestCollection()));
  176. }
  177. public function testReset()
  178. {
  179. $this->_model->countTotals($this->_getTestCollection());
  180. $this->_model->reset();
  181. $this->assertEquals(new \Magento\Framework\DataObject(), $this->_model->getTotals());
  182. $this->assertNotEmpty($this->_model->getColumns());
  183. }
  184. public function testResetFull()
  185. {
  186. $this->_model->countTotals($this->_getTestCollection());
  187. $this->_model->reset(true);
  188. $this->assertEquals(new \Magento\Framework\DataObject(), $this->_model->getTotals());
  189. $this->assertEmpty($this->_model->getColumns());
  190. }
  191. }