/app/code/Magento/ImportExport/Test/Unit/Model/ResourceModel/CollectionByPagesIteratorTest.php

https://gitlab.com/AlexandrSy/magento.xxx · PHP · 84 lines · 50 code · 19 blank · 15 comment · 2 complexity · 1ea3a882cd58bb5f4f46402ed1f4c067 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ImportExport\Test\Unit\Model\ResourceModel;
  7. use \Magento\Framework\Data\Collection\AbstractDb;
  8. /**
  9. * Test class for \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIterator
  10. */
  11. class CollectionByPagesIteratorTest extends \PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * @var \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIterator
  15. */
  16. protected $_resourceModel;
  17. protected function setUp()
  18. {
  19. $this->_resourceModel = new \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIterator();
  20. }
  21. protected function tearDown()
  22. {
  23. unset($this->_resourceModel);
  24. }
  25. /**
  26. * @covers \Magento\ImportExport\Model\ResourceModel\CollectionByPagesIterator::iterate
  27. */
  28. public function testIterate()
  29. {
  30. $pageSize = 2;
  31. $pageCount = 3;
  32. /** @var $callbackMock \PHPUnit_Framework_MockObject_MockObject */
  33. $callbackMock = $this->getMock('stdClass', ['callback']);
  34. $fetchStrategy = $this->getMockForAbstractClass('Magento\Framework\Data\Collection\Db\FetchStrategyInterface');
  35. $select = $this->getMock('Magento\Framework\DB\Select', [], [], '', false);
  36. $entityFactory = $this->getMock('Magento\Framework\Data\Collection\EntityFactory', [], [], '', false);
  37. $logger = $this->getMock('Psr\Log\LoggerInterface');
  38. /** @var $collectionMock AbstractDb|\PHPUnit_Framework_MockObject_MockObject */
  39. $collectionMock = $this->getMockBuilder('Magento\Framework\Data\Collection\AbstractDb')
  40. ->setConstructorArgs([$entityFactory, $logger, $fetchStrategy])
  41. ->setMethods(['clear', 'setPageSize', 'setCurPage', 'count', 'getLastPageNumber', 'getSelect'])
  42. ->getMockForAbstractClass();
  43. $collectionMock->expects($this->any())->method('getSelect')->will($this->returnValue($select));
  44. $collectionMock->expects($this->exactly($pageCount + 1))->method('clear')->will($this->returnSelf());
  45. $collectionMock->expects($this->exactly($pageCount))->method('setPageSize')->will($this->returnSelf());
  46. $collectionMock->expects($this->exactly($pageCount))->method('setCurPage')->will($this->returnSelf());
  47. $collectionMock->expects($this->exactly($pageCount))->method('count')->will($this->returnValue($pageSize));
  48. $collectionMock->expects(
  49. $this->exactly($pageCount)
  50. )->method(
  51. 'getLastPageNumber'
  52. )->will(
  53. $this->returnValue($pageCount)
  54. );
  55. for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) {
  56. for ($rowNumber = 1; $rowNumber <= $pageSize; $rowNumber++) {
  57. $itemId = ($pageNumber - 1) * $pageSize + $rowNumber;
  58. $item = new \Magento\Framework\DataObject(['id' => $itemId]);
  59. $collectionMock->addItem($item);
  60. $callbackMock->expects($this->at($itemId - 1))->method('callback')->with($item);
  61. }
  62. }
  63. $this->_resourceModel->iterate($collectionMock, $pageSize, [[$callbackMock, 'callback']]);
  64. }
  65. }