public function testGetSize() { $countSql = 500; $adapterMock = $this->getMock('Magento\\Framework\\DB\\Adapter\\Pdo\\Mysql', ['select', 'quoteInto', 'prepareSqlCondition', 'fetchOne'], [], '', false); $selectMock = $this->getMock('Magento\\Framework\\DB\\Select', ['orWhere', 'where', 'reset', 'columns'], ['adapter' => $adapterMock]); $selectMock->expects($this->exactly(4))->method('reset'); $selectMock->expects($this->once())->method('columns')->with('COUNT(*)'); $adapterMock->expects($this->once())->method('select')->will($this->returnValue($selectMock)); $adapterMock->expects($this->exactly(2))->method('quoteInto')->will($this->returnValueMap([['testField1=?', 'testValue1', null, null, 'testField1=testValue1'], ['testField4=?', 'testValue4', null, null, 'testField4=testValue4']])); $selectMock->expects($this->once())->method('orWhere')->with('testField1=testValue1'); $selectMock->expects($this->exactly(3))->method('where')->will($this->returnValueMap([['testValue2', $this->returnSelf()], ['testField3 = testValue3', null, \Magento\Framework\DB\Select::TYPE_CONDITION, $this->returnSelf()], ['testField4=testValue4', $this->returnSelf()]])); $adapterMock->expects($this->once())->method('prepareSqlCondition')->with('testField3', 'testValue3')->will($this->returnValue('testField3 = testValue3')); $adapterMock->expects($this->once())->method('fetchOne')->with($selectMock, [])->will($this->returnValue($countSql)); $this->collection->addFilter('testField1', 'testValue1', 'or'); $this->collection->addFilter('testField2', 'testValue2', 'string'); $this->collection->addFilter('testField3', 'testValue3', 'public'); $this->collection->addFilter('testField4', 'testValue4'); $this->collection->setConnection($adapterMock); $this->assertEquals($countSql, $this->collection->getSize()); $this->assertEquals($countSql, $this->collection->getSize()); }
/** * Search collection * * @return \Magento\Framework\Data\Collection\AbstractDb */ public function getSearchCollection() { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); /** @var \Magento\Store\Model\StoreManagerInterface $storeManager */ $storeManager = $objectManager->get('Magento\\Store\\Model\\StoreManagerInterface'); /** @var \Magento\Store\Model\App\Emulation $emulation */ $emulation = $objectManager->create('Magento\\Store\\Model\\App\\Emulation'); if (!$this->searchCollection) { $isEmulation = false; if ($this->getData('store_id') && $this->getData('store_id') != $storeManager->getStore()->getId()) { $emulation->startEnvironmentEmulation($this->getData('store_id')); $isEmulation = true; } $this->searchCollection = $this->buildSearchCollection(); if ($isEmulation) { $this->searchCollection->getSize(); // get size before switch to default store $emulation->stopEnvironmentEmulation(); } } return $this->searchCollection; }