Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BatchIteratorTest.php
Go to the documentation of this file.
1 <?php
7 
11 
12 class BatchIteratorTest extends \PHPUnit\Framework\TestCase
13 {
17  private $model;
18 
22  private $selectMock;
23 
27  private $wrapperSelectMock;
28 
32  private $connectionMock;
33 
37  private $batchSize;
38 
42  private $correlationName;
43 
47  private $rangeField;
48 
52  private $rangeFieldAlias;
53 
59  protected function setUp()
60  {
61  $this->batchSize = 10;
62  $this->correlationName = 'correlationName';
63  $this->rangeField = 'rangeField';
64  $this->rangeFieldAlias = 'rangeFieldAlias';
65 
66  $this->selectMock = $this->createMock(Select::class);
67  $this->wrapperSelectMock = $this->createMock(Select::class);
68  $this->connectionMock = $this->createMock(AdapterInterface::class);
69  $this->connectionMock->expects($this->any())->method('select')->willReturn($this->wrapperSelectMock);
70  $this->selectMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock);
71  $this->connectionMock->expects($this->any())->method('quoteIdentifier')->willReturnArgument(0);
72 
73  $this->model = new BatchIterator(
74  $this->selectMock,
75  $this->batchSize,
76  $this->correlationName,
77  $this->rangeField,
78  $this->rangeFieldAlias
79  );
80  }
81 
89  public function testCurrent()
90  {
91  $filed = $this->correlationName . '.' . $this->rangeField;
92 
93  $this->selectMock->expects($this->once())->method('where')->with($filed . ' > ?', 0);
94  $this->selectMock->expects($this->once())->method('limit')->with($this->batchSize);
95  $this->selectMock->expects($this->once())->method('order')->with($filed . ' ASC');
96  $this->assertEquals($this->selectMock, $this->model->current());
97  $this->assertEquals($this->selectMock, $this->model->current());
98  $this->assertEquals(0, $this->model->key());
99  }
100 
129  public function testIterations()
130  {
131  $startCallIndex = 3;
132  $stepCall = 4;
133 
134  $this->connectionMock->expects($this->at($startCallIndex))
135  ->method('fetchRow')
136  ->willReturn(['max' => 10, 'cnt' => 10]);
137 
138  $this->connectionMock->expects($this->at($startCallIndex += $stepCall))
139  ->method('fetchRow')
140  ->willReturn(['max' => 20, 'cnt' => 10]);
141 
142  $this->connectionMock->expects($this->at($startCallIndex += $stepCall))
143  ->method('fetchRow')
144  ->willReturn(['max' => 25, 'cnt' => 5]);
145 
146  $this->connectionMock->expects($this->at($startCallIndex += $stepCall))
147  ->method('fetchRow')
148  ->willReturn(['max' => null, 'cnt' => 0]);
149 
154  $iteration = 0;
155  $result = [];
156  foreach ($this->model as $key => $select) {
157  $result[] = $select;
158  $this->assertEquals($iteration, $key);
159  $iteration++;
160  }
161  $this->assertCount(3, $result);
162  }
163 
173  public function testNext()
174  {
175  $filed = $this->correlationName . '.' . $this->rangeField;
176  $this->selectMock->expects($this->at(0))->method('where')->with($filed . ' > ?', 0);
177  $this->selectMock->expects($this->exactly(3))->method('limit')->with($this->batchSize);
178  $this->selectMock->expects($this->exactly(3))->method('order')->with($filed . ' ASC');
179  $this->selectMock->expects($this->at(3))->method('where')->with($filed . ' > ?', 25);
180 
181  $this->wrapperSelectMock->expects($this->exactly(3))->method('from')->with(
182  $this->selectMock,
183  [
184  new \Zend_Db_Expr('MAX(' . $this->rangeFieldAlias . ') as max'),
185  new \Zend_Db_Expr('COUNT(*) as cnt')
186  ]
187  );
188  $this->connectionMock->expects($this->exactly(3))
189  ->method('fetchRow')
190  ->with($this->wrapperSelectMock)
191  ->willReturn(['max' => 25, 'cnt' => 10]);
192 
193  $this->assertEquals($this->selectMock, $this->model->next());
194  $this->assertEquals(1, $this->model->key());
195 
196  $this->assertEquals($this->selectMock, $this->model->next());
197  $this->assertEquals($this->selectMock, $this->model->current());
198  $this->assertEquals(2, $this->model->key());
199  }
200 }