Skip to content

Commit

Permalink
Flush totalRecords when clearing data collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Nezbritskiy committed Mar 31, 2019
1 parent e457037 commit 2038d7d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ public function addAttributeToFilter($attribute, $condition = null, $joinType =

if (!empty($conditionSql)) {
$this->getSelect()->where($conditionSql, null, \Magento\Framework\DB\Select::TYPE_CONDITION);
$this->invalidateSize();
$this->_totalRecords = null;
} else {
throw new \Magento\Framework\Exception\LocalizedException(
__('Invalid attribute identifier for filter (%1)', get_class($attribute))
Expand Down Expand Up @@ -1720,15 +1720,4 @@ public function removeAllFieldsFromSelect()
return $this->removeAttributeToSelect();
}

/**
* Invalidates "Total Records Count".
* Invalidates saved "Total Records Count" attribute with last counting,
* so a next calling of method getSize() will query new total records count.
*
* @return void
*/
private function invalidateSize(): void
{
$this->_totalRecords = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,4 @@ public function testToHtmlCartItem()
$this->assertContains('$10.00', $html);
$this->assertContains('catalog/product/edit/id/1', $html);
}

/**
* Verify that the customer has a single item in his cart.
*
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/quote.php
*/
public function testGetCollection()
{
$this->assertEquals(1, $this->block->getCollection()->getSize());
}
}
1 change: 1 addition & 0 deletions lib/internal/Magento/Framework/Data/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ public function clear()
{
$this->_setIsLoaded(false);
$this->_items = [];
$this->_totalRecords = null;
return $this;
}

Expand Down
21 changes: 21 additions & 0 deletions lib/internal/Magento/Framework/Test/Unit/Data/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ public function testWalk()
);
}

/**
* Ensure that getSize works correctly with clear
*
*/
public function testClearTotalRecords()
{
$objOne = new \Magento\Framework\DataObject(['id' => 1, 'name' => 'one']);
$objTwo = new \Magento\Framework\DataObject(['id' => 2, 'name' => 'two']);
$objThree = new \Magento\Framework\DataObject(['id' => 3, 'name' => 'three']);

/** @noinspection PhpUnhandledExceptionInspection */
$this->collection->addItem($objOne);
/** @noinspection PhpUnhandledExceptionInspection */
$this->collection->addItem($objTwo);
/** @noinspection PhpUnhandledExceptionInspection */
$this->collection->addItem($objThree);
$this->assertEquals(3, $this->collection->getSize());
$this->collection->clear();
$this->assertEquals(0, $this->collection->getSize());
}

/**
* Callback function.
*
Expand Down

0 comments on commit 2038d7d

Please sign in to comment.