|
6 | 6 | use Illuminate\Database\Query\Grammars\Grammar; |
7 | 7 | use Illuminate\Database\Query\Processors\Processor; |
8 | 8 | use Illuminate\Pagination\Factory; |
| 9 | +use Illuminate\Support\Collection; |
9 | 10 | use L4\Tests\BackwardCompatibleTestCase; |
10 | 11 | use Mockery as m; |
| 12 | +use Mockery\MockInterface; |
11 | 13 |
|
12 | 14 | class DatabaseQueryBuilderTest extends BackwardCompatibleTestCase |
13 | 15 | { |
@@ -1363,6 +1365,108 @@ public function testMergeBuildersBindingOrder(): void |
1363 | 1365 | $this->assertEquals(['foo', 'bar', 'baz'], $builder->getBindings()); |
1364 | 1366 | } |
1365 | 1367 |
|
| 1368 | + public function testChunkByIdOnArrays(): void |
| 1369 | + { |
| 1370 | + $builder = $this->getMockQueryBuilder(); |
| 1371 | + $builder->orders[] = ['column' => 'foobar', 'direction' => 'asc']; |
| 1372 | + |
| 1373 | + $chunk1 = [['someIdField' => 1], ['someIdField' => 2]]; |
| 1374 | + $chunk2 = [['someIdField' => 10], ['someIdField' => 11]]; |
| 1375 | + $chunk3 = []; |
| 1376 | + $builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'someIdField')->andReturnSelf(); |
| 1377 | + $builder->shouldReceive('forPageAfterId')->once()->with(2, 2, 'someIdField')->andReturnSelf(); |
| 1378 | + $builder->shouldReceive('forPageAfterId')->once()->with(2, 11, 'someIdField')->andReturnSelf(); |
| 1379 | + $builder->shouldReceive('get')->times(3)->andReturn($chunk1, $chunk2, $chunk3); |
| 1380 | + |
| 1381 | + $callbackAssertor = m::mock(stdClass::class); |
| 1382 | + $callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1); |
| 1383 | + $callbackAssertor->shouldReceive('doSomething')->once()->with($chunk2); |
| 1384 | + $callbackAssertor->shouldReceive('doSomething')->never()->with($chunk3); |
| 1385 | + |
| 1386 | + $builder->chunkById(2, function ($results) use ($callbackAssertor) { |
| 1387 | + $callbackAssertor->doSomething($results); |
| 1388 | + }, 'someIdField'); |
| 1389 | + } |
| 1390 | + |
| 1391 | + public function testChunkPaginatesUsingIdWithLastChunkComplete(): void |
| 1392 | + { |
| 1393 | + $builder = $this->getMockQueryBuilder(); |
| 1394 | + $builder->orders[] = ['column' => 'foobar', 'direction' => 'asc']; |
| 1395 | + |
| 1396 | + $chunk1 = [(object) ['someIdField' => 1], (object) ['someIdField' => 2]]; |
| 1397 | + $chunk2 = [(object) ['someIdField' => 10], (object) ['someIdField' => 11]]; |
| 1398 | + $chunk3 = []; |
| 1399 | + $builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'someIdField')->andReturnSelf(); |
| 1400 | + $builder->shouldReceive('forPageAfterId')->once()->with(2, 2, 'someIdField')->andReturnSelf(); |
| 1401 | + $builder->shouldReceive('forPageAfterId')->once()->with(2, 11, 'someIdField')->andReturnSelf(); |
| 1402 | + $builder->shouldReceive('get')->times(3)->andReturn($chunk1, $chunk2, $chunk3); |
| 1403 | + |
| 1404 | + $callbackAssertor = m::mock(stdClass::class); |
| 1405 | + $callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1); |
| 1406 | + $callbackAssertor->shouldReceive('doSomething')->once()->with($chunk2); |
| 1407 | + $callbackAssertor->shouldReceive('doSomething')->never()->with($chunk3); |
| 1408 | + |
| 1409 | + $builder->chunkById(2, function ($results) use ($callbackAssertor) { |
| 1410 | + $callbackAssertor->doSomething($results); |
| 1411 | + }, 'someIdField'); |
| 1412 | + } |
| 1413 | + |
| 1414 | + public function testChunkPaginatesUsingIdWithLastChunkPartial(): void |
| 1415 | + { |
| 1416 | + $builder = $this->getMockQueryBuilder(); |
| 1417 | + $builder->orders[] = ['column' => 'foobar', 'direction' => 'asc']; |
| 1418 | + |
| 1419 | + $chunk1 = [(object) ['someIdField' => 1], (object) ['someIdField' => 2]]; |
| 1420 | + $chunk2 = [(object) ['someIdField' => 10]]; |
| 1421 | + $builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'someIdField')->andReturnSelf(); |
| 1422 | + $builder->shouldReceive('forPageAfterId')->once()->with(2, 2, 'someIdField')->andReturnSelf(); |
| 1423 | + $builder->shouldReceive('get')->times(2)->andReturn($chunk1, $chunk2); |
| 1424 | + |
| 1425 | + $callbackAssertor = m::mock(stdClass::class); |
| 1426 | + $callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1); |
| 1427 | + $callbackAssertor->shouldReceive('doSomething')->once()->with($chunk2); |
| 1428 | + |
| 1429 | + $builder->chunkById(2, function ($results) use ($callbackAssertor) { |
| 1430 | + $callbackAssertor->doSomething($results); |
| 1431 | + }, 'someIdField'); |
| 1432 | + } |
| 1433 | + |
| 1434 | + public function testChunkPaginatesUsingIdWithCountZero(): void |
| 1435 | + { |
| 1436 | + $builder = $this->getMockQueryBuilder(); |
| 1437 | + $builder->orders[] = ['column' => 'foobar', 'direction' => 'asc']; |
| 1438 | + |
| 1439 | + $chunk = []; |
| 1440 | + $builder->shouldReceive('forPageAfterId')->once()->with(0, 0, 'someIdField')->andReturnSelf(); |
| 1441 | + $builder->shouldReceive('get')->times(1)->andReturn($chunk); |
| 1442 | + |
| 1443 | + $callbackAssertor = m::mock(stdClass::class); |
| 1444 | + $callbackAssertor->shouldReceive('doSomething')->never(); |
| 1445 | + |
| 1446 | + $builder->chunkById(0, function ($results) use ($callbackAssertor) { |
| 1447 | + $callbackAssertor->doSomething($results); |
| 1448 | + }, 'someIdField'); |
| 1449 | + } |
| 1450 | + |
| 1451 | + public function testChunkPaginatesUsingIdWithAlias(): void |
| 1452 | + { |
| 1453 | + $builder = $this->getMockQueryBuilder(); |
| 1454 | + $builder->orders[] = ['column' => 'foobar', 'direction' => 'asc']; |
| 1455 | + |
| 1456 | + $chunk1 = [(object) ['table_id' => 1], (object) ['table_id' => 10]]; |
| 1457 | + $chunk2 = []; |
| 1458 | + $builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'table.id')->andReturnSelf(); |
| 1459 | + $builder->shouldReceive('forPageAfterId')->once()->with(2, 10, 'table.id')->andReturnSelf(); |
| 1460 | + $builder->shouldReceive('get')->times(2)->andReturn($chunk1, $chunk2); |
| 1461 | + |
| 1462 | + $callbackAssertor = m::mock(stdClass::class); |
| 1463 | + $callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1); |
| 1464 | + $callbackAssertor->shouldReceive('doSomething')->never()->with($chunk2); |
| 1465 | + |
| 1466 | + $builder->chunkById(2, function ($results) use ($callbackAssertor) { |
| 1467 | + $callbackAssertor->doSomething($results); |
| 1468 | + }, 'table.id', 'table_id'); |
| 1469 | + } |
1366 | 1470 |
|
1367 | 1471 | protected function getBuilder(): Builder |
1368 | 1472 | { |
@@ -1411,4 +1515,16 @@ protected function getMySqlBuilderWithProcessor(): Builder |
1411 | 1515 | return new Builder(m::mock(ConnectionInterface::class), $grammar, $processor); |
1412 | 1516 | } |
1413 | 1517 |
|
| 1518 | + /** |
| 1519 | + * @return MockInterface|\Illuminate\Database\Query\Builder |
| 1520 | + */ |
| 1521 | + protected function getMockQueryBuilder(): MockInterface|Builder |
| 1522 | + { |
| 1523 | + return m::mock(Builder::class, [ |
| 1524 | + m::mock(ConnectionInterface::class), |
| 1525 | + new Grammar, |
| 1526 | + m::mock(Processor::class), |
| 1527 | + ])->makePartial()->shouldAllowMockingProtectedMethods(); |
| 1528 | + } |
| 1529 | + |
1414 | 1530 | } |
0 commit comments