|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Framework\Test\Unit\DB\Query; |
| 7 | + |
| 8 | +use Magento\Framework\DB\Query\BatchIterator; |
| 9 | +use Magento\Framework\DB\Select; |
| 10 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 11 | + |
| 12 | +class BatchIteratorTest extends \PHPUnit_Framework_TestCase |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @var BatchIterator |
| 16 | + */ |
| 17 | + private $model; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 21 | + */ |
| 22 | + private $selectMock; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 26 | + */ |
| 27 | + private $wrapperSelectMock; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 31 | + */ |
| 32 | + private $connectionMock; |
| 33 | + |
| 34 | + private $batchSize; |
| 35 | + private $correlationName; |
| 36 | + private $rangeField; |
| 37 | + private $rangeFieldAlias; |
| 38 | + |
| 39 | + protected function setUp() |
| 40 | + { |
| 41 | + $this->batchSize = 10; |
| 42 | + $this->correlationName = 'correlationName'; |
| 43 | + $this->rangeField = 'rangeField'; |
| 44 | + $this->rangeFieldAlias = 'rangeFieldAlias'; |
| 45 | + |
| 46 | + $this->selectMock = $this->getMock(Select::class, [], [], '', false, false); |
| 47 | + $this->wrapperSelectMock = $this->getMock(Select::class, [], [], '', false, false); |
| 48 | + $this->connectionMock = $this->getMock(AdapterInterface::class); |
| 49 | + $this->connectionMock->expects($this->any())->method('select')->willReturn($this->wrapperSelectMock); |
| 50 | + $this->selectMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock); |
| 51 | + $this->connectionMock->expects($this->any())->method('quoteIdentifier')->willReturnArgument(0); |
| 52 | + |
| 53 | + $this->model = new BatchIterator( |
| 54 | + $this->selectMock, |
| 55 | + $this->batchSize, |
| 56 | + $this->correlationName, |
| 57 | + $this->rangeField, |
| 58 | + $this->rangeFieldAlias |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Test steps: |
| 64 | + * 1. $iterator->current(); |
| 65 | + * 2. $iterator->current(); |
| 66 | + * 3. $iterator->key(); |
| 67 | + */ |
| 68 | + public function testCurrent() |
| 69 | + { |
| 70 | + $filed = $this->correlationName . '.' . $this->rangeField; |
| 71 | + |
| 72 | + $this->selectMock->expects($this->once())->method('where')->with($filed . ' > ?', 0); |
| 73 | + $this->selectMock->expects($this->once())->method('limit')->with($this->batchSize); |
| 74 | + $this->selectMock->expects($this->once())->method('order')->with($filed . ' ASC'); |
| 75 | + $this->assertEquals($this->selectMock, $this->model->current()); |
| 76 | + $this->assertEquals($this->selectMock, $this->model->current()); |
| 77 | + $this->assertEquals(0, $this->model->key()); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * SQL: select * from users |
| 82 | + * Batch size: 10 |
| 83 | + * IDS: [1 - 25] |
| 84 | + * Items count: 25 |
| 85 | + * Expected batches: [1-10, 11-20, 20-25] |
| 86 | + * |
| 87 | + * Test steps: |
| 88 | + * 1. $iterator->rewind(); |
| 89 | + * 2. $iterator->valid(); |
| 90 | + * 3. $iterator->current(); |
| 91 | + * 4. $iterator->key(); |
| 92 | + * |
| 93 | + * 1. $iterator->next() |
| 94 | + * 2. $iterator->valid(); |
| 95 | + * 3. $iterator->current(); |
| 96 | + * 4. $iterator->key(); |
| 97 | + * |
| 98 | + * 1. $iterator->next() |
| 99 | + * 2. $iterator->valid(); |
| 100 | + * 3. $iterator->current(); |
| 101 | + * 4. $iterator->key(); |
| 102 | + * |
| 103 | + * |
| 104 | + * 1. $iterator->next() |
| 105 | + * 2. $iterator->valid(); |
| 106 | + * |
| 107 | + */ |
| 108 | + public function testIterations() |
| 109 | + { |
| 110 | + $startCallIndex = 3; |
| 111 | + $stepCall = 4; |
| 112 | + |
| 113 | + $this->connectionMock->expects($this->at($startCallIndex)) |
| 114 | + ->method('fetchRow') |
| 115 | + ->willReturn(['max' => 10, 'cnt' => 10]); |
| 116 | + |
| 117 | + $this->connectionMock->expects($this->at($startCallIndex += $stepCall)) |
| 118 | + ->method('fetchRow') |
| 119 | + ->willReturn(['max' => 20, 'cnt' => 10]); |
| 120 | + |
| 121 | + $this->connectionMock->expects($this->at($startCallIndex += $stepCall)) |
| 122 | + ->method('fetchRow') |
| 123 | + ->willReturn(['max' => 25, 'cnt' => 5]); |
| 124 | + |
| 125 | + $this->connectionMock->expects($this->at($startCallIndex += $stepCall)) |
| 126 | + ->method('fetchRow') |
| 127 | + ->willReturn(['max' => null, 'cnt' => 0]); |
| 128 | + |
| 129 | + /** |
| 130 | + * Test 3 iterations |
| 131 | + * [1-10, 11-20, 20-25] |
| 132 | + */ |
| 133 | + $iteration = 0; |
| 134 | + $result = []; |
| 135 | + foreach ($this->model as $key => $select) { |
| 136 | + $result[] = $select; |
| 137 | + $this->assertEquals($iteration, $key); |
| 138 | + $iteration++; |
| 139 | + } |
| 140 | + $this->assertCount(3, $result); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Test steps: |
| 145 | + * 1. $iterator->next(); |
| 146 | + * 2. $iterator->key() |
| 147 | + * 3. $iterator->next(); |
| 148 | + * 4. $iterator->current() |
| 149 | + * 5. $iterator->key() |
| 150 | + */ |
| 151 | + public function testNext() |
| 152 | + { |
| 153 | + $filed = $this->correlationName . '.' . $this->rangeField; |
| 154 | + $this->selectMock->expects($this->at(0))->method('where')->with($filed . ' > ?', 0); |
| 155 | + $this->selectMock->expects($this->exactly(3))->method('limit')->with($this->batchSize); |
| 156 | + $this->selectMock->expects($this->exactly(3))->method('order')->with($filed . ' ASC'); |
| 157 | + $this->selectMock->expects($this->at(3))->method('where')->with($filed . ' > ?', 25); |
| 158 | + |
| 159 | + $this->wrapperSelectMock->expects($this->exactly(3))->method('from')->with( |
| 160 | + $this->selectMock, |
| 161 | + [ |
| 162 | + new \Zend_Db_Expr('MAX(' . $this->rangeFieldAlias . ') as max'), |
| 163 | + new \Zend_Db_Expr('COUNT(*) as cnt') |
| 164 | + ] |
| 165 | + ); |
| 166 | + $this->connectionMock->expects($this->exactly(3)) |
| 167 | + ->method('fetchRow') |
| 168 | + ->with($this->wrapperSelectMock) |
| 169 | + ->willReturn([ |
| 170 | + 'max' => 25, |
| 171 | + 'cnt' => 10 |
| 172 | + ] |
| 173 | + ); |
| 174 | + |
| 175 | + $this->assertEquals($this->selectMock, $this->model->next()); |
| 176 | + $this->assertEquals(1, $this->model->key()); |
| 177 | + |
| 178 | + $this->assertEquals($this->selectMock, $this->model->next()); |
| 179 | + $this->assertEquals($this->selectMock, $this->model->current()); |
| 180 | + $this->assertEquals(2, $this->model->key()); |
| 181 | + } |
| 182 | +} |
0 commit comments