|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Reports\Test\Unit\Model\Resource\Report\Quote; |
| 7 | + |
| 8 | +use \Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 9 | +use \Magento\Reports\Model\Resource\Quote\Collection as Collection; |
| 10 | + |
| 11 | +class CollectionTest extends \PHPUnit_Framework_TestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager |
| 15 | + */ |
| 16 | + protected $objectManager; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 20 | + */ |
| 21 | + protected $selectMock; |
| 22 | + |
| 23 | + protected function setUp() |
| 24 | + { |
| 25 | + $this->objectManager = new ObjectManager($this); |
| 26 | + $this->selectMock = $this->getMock('\Magento\Framework\DB\Select', [], [], '', false); |
| 27 | + } |
| 28 | + |
| 29 | + public function testGetSelectCountSql() |
| 30 | + { |
| 31 | + /** @var $collection \PHPUnit_Framework_MockObject_MockObject */ |
| 32 | + $constructArgs = $this->objectManager |
| 33 | + ->getConstructArguments('Magento\Reports\Model\Resource\Quote\Collection'); |
| 34 | + $collection = $this->getMock( |
| 35 | + 'Magento\Reports\Model\Resource\Quote\Collection', |
| 36 | + ['prepareActiveCartItems', 'getSelect'], |
| 37 | + $constructArgs, |
| 38 | + '', |
| 39 | + false |
| 40 | + ); |
| 41 | + |
| 42 | + $collection->expects($this->once())->method('getSelect')->willReturn($this->selectMock); |
| 43 | + $this->selectMock->expects($this->atLeastOnce())->method('reset')->willReturnSelf(); |
| 44 | + $this->selectMock->expects($this->once()) |
| 45 | + ->method('columns') |
| 46 | + ->with('COUNT(DISTINCT main_table.entity_id)') |
| 47 | + ->willReturnSelf(); |
| 48 | + $this->assertEquals($this->selectMock, $collection->getSelectCountSql()); |
| 49 | + } |
| 50 | + |
| 51 | + public function testPrepareActiveCartItems() |
| 52 | + { |
| 53 | + /** @var $collection \PHPUnit_Framework_MockObject_MockObject */ |
| 54 | + $constructArgs = $this->objectManager |
| 55 | + ->getConstructArguments('Magento\Reports\Model\Resource\Quote\Collection'); |
| 56 | + $collection = $this->getMock( |
| 57 | + 'Magento\Reports\Model\Resource\Quote\Collection', |
| 58 | + ['getSelect', 'getTable'], |
| 59 | + $constructArgs, |
| 60 | + '', |
| 61 | + false |
| 62 | + ); |
| 63 | + |
| 64 | + $collection->expects($this->once())->method('getSelect')->willReturn($this->selectMock); |
| 65 | + $this->selectMock->expects($this->once())->method('reset')->willReturnSelf(); |
| 66 | + $this->selectMock->expects($this->once())->method('from')->willReturnSelf(); |
| 67 | + $this->selectMock->expects($this->atLeastOnce())->method('columns')->willReturnSelf(); |
| 68 | + $this->selectMock->expects($this->once())->method('joinInner')->willReturnSelf(); |
| 69 | + $this->selectMock->expects($this->once())->method('where')->willReturnSelf(); |
| 70 | + $this->selectMock->expects($this->once())->method('group')->willReturnSelf(); |
| 71 | + $collection->expects($this->exactly(2))->method('getTable')->willReturn('table'); |
| 72 | + $collection->prepareActiveCartItems(); |
| 73 | + } |
| 74 | + |
| 75 | + public function testLoadWithFilter() |
| 76 | + { |
| 77 | + /** @var $collection \PHPUnit_Framework_MockObject_MockObject */ |
| 78 | + $constructArgs = $this->objectManager |
| 79 | + ->getConstructArguments('Magento\Reports\Model\Resource\Quote\Collection'); |
| 80 | + $constructArgs['eventManager'] = $this->getMock('Magento\Framework\Event\ManagerInterface', [], [], '', false); |
| 81 | + $readConnectionMock = $this->getMock('Magento\Framework\DB\Adapter\AdapterInterface', [], [], '', false); |
| 82 | + $resourceMock = $this->getMock('\Magento\Quote\Model\Resource\Quote', [], [], '', false); |
| 83 | + $constructArgs['resource'] = $resourceMock; |
| 84 | + $productResourceMock = $this->getMock('\Magento\Catalog\Model\Resource\Product\Collection', [], [], '', false); |
| 85 | + $constructArgs['productResource'] = $productResourceMock; |
| 86 | + |
| 87 | + $collection = $this->getMock( |
| 88 | + 'Magento\Reports\Model\Resource\Quote\Collection', |
| 89 | + [ |
| 90 | + '_beforeLoad', |
| 91 | + '_renderFilters', |
| 92 | + '_renderOrders', |
| 93 | + '_renderLimit', |
| 94 | + 'printLogQuery', |
| 95 | + 'getData', |
| 96 | + '_setIsLoaded', |
| 97 | + 'setConnection', |
| 98 | + '_initSelect', |
| 99 | + 'getTable', |
| 100 | + 'getItems', |
| 101 | + 'getOrdersSubSelect', |
| 102 | + ], |
| 103 | + $constructArgs |
| 104 | + ); |
| 105 | + //load() |
| 106 | + $collection->expects($this->once())->method('_beforeLoad')->willReturnSelf(); |
| 107 | + $collection->expects($this->once())->method('_renderFilters')->willReturnSelf(); |
| 108 | + $collection->expects($this->once())->method('_renderOrders')->willReturnSelf(); |
| 109 | + $collection->expects($this->once())->method('_renderLimit')->willReturnSelf(); |
| 110 | + $collection->expects($this->once())->method('printLogQuery')->willReturnSelf(); |
| 111 | + $collection->expects($this->once())->method('getData')->willReturn(null); |
| 112 | + $collection->expects($this->once())->method('_setIsLoaded')->willReturnSelf(); |
| 113 | + |
| 114 | + //productLoad() |
| 115 | + $productAttributeMock = $this->getMock( |
| 116 | + '\Magento\Eav\Model\Entity\Attribute\AbstractAttribute', |
| 117 | + [], |
| 118 | + [], |
| 119 | + '', |
| 120 | + false |
| 121 | + ); |
| 122 | + $priceAttributeMock = $this->getMock( |
| 123 | + '\Magento\Eav\Model\Entity\Attribute\AbstractAttribute', |
| 124 | + [], |
| 125 | + [], |
| 126 | + '', |
| 127 | + false |
| 128 | + ); |
| 129 | + $productResourceMock->expects($this->once()) |
| 130 | + ->method('getConnection') |
| 131 | + ->with('read') |
| 132 | + ->willReturn($readConnectionMock); |
| 133 | + $productResourceMock->expects($this->any()) |
| 134 | + ->method('getAttribute') |
| 135 | + ->willReturnMap([['name', $productAttributeMock], ['price', $priceAttributeMock]]); |
| 136 | + $productResourceMock->expects($this->once())->method('getSelect')->willReturn($this->selectMock); |
| 137 | + $this->selectMock->expects($this->once())->method('reset')->willReturnSelf(); |
| 138 | + $this->selectMock->expects($this->once())->method('from')->willReturnSelf(); |
| 139 | + $this->selectMock->expects($this->once())->method('useStraightJoin')->willReturnSelf(); |
| 140 | + $this->selectMock->expects($this->exactly(2))->method('joinInner')->willReturnSelf(); |
| 141 | + $this->selectMock->expects($this->once())->method('joinLeft')->willReturnSelf(); |
| 142 | + $this->selectMock->expects($this->once())->method('columns')->willReturnSelf(); |
| 143 | + |
| 144 | + $productAttributeMock->expects($this->once())->method('getBackend')->willReturnSelf(); |
| 145 | + $priceAttributeMock->expects($this->once())->method('getBackend')->willReturnSelf(); |
| 146 | + $readConnectionMock->expects($this->once())->method('fetchAssoc')->willReturn([1, 2, 3]); |
| 147 | + |
| 148 | + //_afterLoad() |
| 149 | + $collection->expects($this->once())->method('getItems')->willReturn([]); |
| 150 | + |
| 151 | + $collection->loadWithFilter(); |
| 152 | + } |
| 153 | +} |
0 commit comments