Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CollectionByPagesIteratorTest.php
Go to the documentation of this file.
1 <?php
7 
8 use \Magento\Framework\Data\Collection\AbstractDb;
9 
13 class CollectionByPagesIteratorTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $_resourceModel;
19 
20  protected function setUp()
21  {
22  $this->_resourceModel = new \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIterator();
23  }
24 
25  protected function tearDown()
26  {
27  unset($this->_resourceModel);
28  }
29 
33  public function testIterate()
34  {
35  $pageSize = 2;
36  $pageCount = 3;
37 
39  $callbackMock = $this->createPartialMock(\stdClass::class, ['callback']);
40 
41  $fetchStrategy = $this->getMockForAbstractClass(
42  \Magento\Framework\Data\Collection\Db\FetchStrategyInterface::class
43  );
44 
45  $select = $this->createMock(\Magento\Framework\DB\Select::class);
46 
47  $entityFactory = $this->createMock(\Magento\Framework\Data\Collection\EntityFactory::class);
48  $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
49 
51  $collectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection\AbstractDb::class)
52  ->setConstructorArgs([$entityFactory, $logger, $fetchStrategy])
53  ->setMethods(['clear', 'setPageSize', 'setCurPage', 'count', 'getLastPageNumber', 'getSelect'])
54  ->getMockForAbstractClass();
55 
56  $collectionMock->expects($this->any())->method('getSelect')->will($this->returnValue($select));
57 
58  $collectionMock->expects($this->exactly($pageCount + 1))->method('clear')->will($this->returnSelf());
59 
60  $collectionMock->expects($this->exactly($pageCount))->method('setPageSize')->will($this->returnSelf());
61 
62  $collectionMock->expects($this->exactly($pageCount))->method('setCurPage')->will($this->returnSelf());
63 
64  $collectionMock->expects($this->exactly($pageCount))->method('count')->will($this->returnValue($pageSize));
65 
66  $collectionMock->expects(
67  $this->exactly($pageCount)
68  )->method(
69  'getLastPageNumber'
70  )->will(
71  $this->returnValue($pageCount)
72  );
73 
74  for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) {
75  for ($rowNumber = 1; $rowNumber <= $pageSize; $rowNumber++) {
76  $itemId = ($pageNumber - 1) * $pageSize + $rowNumber;
77  $item = new \Magento\Framework\DataObject(['id' => $itemId]);
78  $collectionMock->addItem($item);
79 
80  $callbackMock->expects($this->at($itemId - 1))->method('callback')->with($item);
81  }
82  }
83 
84  $this->_resourceModel->iterate($collectionMock, $pageSize, [[$callbackMock, 'callback']]);
85  }
86 }
$logger