BatchProviderTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Indexer\Test\Unit;
  7. use \Magento\Framework\DB\Adapter\AdapterInterface;
  8. use \Magento\Framework\DB\Select;
  9. use \Magento\Framework\Indexer\BatchProvider;
  10. class BatchProviderTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var BatchProvider
  14. */
  15. private $model;
  16. protected function setUp()
  17. {
  18. $this->model = new BatchProvider();
  19. }
  20. /**
  21. * @param int $batchSize preferable batch size
  22. * @param int $maxLinkFieldValue maximum value of the entity identifier in the table
  23. * @param int $expectedResult list of expected consecutive entity ID ranges (batches)
  24. *
  25. * @dataProvider getBatchesDataProvider
  26. */
  27. public function testGetBatches($batchSize, $maxLinkFieldValue, $expectedResult)
  28. {
  29. $tableName = 'test_table';
  30. $linkField = 'id';
  31. $selectMock = $this->createMock(Select::class);
  32. $adapterMock = $this->createMock(AdapterInterface::class);
  33. $selectMock->expects($this->once())->method('from')->willReturnSelf();
  34. $adapterMock->expects($this->once())->method('select')->willReturn($selectMock);
  35. $adapterMock->expects($this->once())->method('fetchOne')->with($selectMock, [])->willReturn($maxLinkFieldValue);
  36. $batches = $this->model->getBatches($adapterMock, $tableName, $linkField, $batchSize);
  37. foreach ($batches as $index => $batch) {
  38. $this->assertEquals($expectedResult[$index], $batch);
  39. }
  40. }
  41. /**
  42. * @return array
  43. */
  44. public function getBatchesDataProvider()
  45. {
  46. return [
  47. [200, 600, [['from' => 1, 'to' => 200], ['from' => 201, 'to' => 400], ['from' => 401, 'to' => 600]]],
  48. [200, 555, [['from' => 1, 'to' => 200], ['from' => 201, 'to' => 400], ['from' => 401, 'to' => 555]]],
  49. [200, 10, [['from' => 1, 'to' => 10]]],
  50. [200, 0, []],
  51. ];
  52. }
  53. public function testGetBatchIds()
  54. {
  55. $selectMock = $this->createMock(Select::class);
  56. $adapterMock = $this->createMock(AdapterInterface::class);
  57. $selectMock->expects($this->once())->method('where')->with('(entity_id BETWEEN 10 AND 100)')->willReturnSelf();
  58. $adapterMock->expects($this->atLeastOnce())->method('quote')->willReturnArgument(0);
  59. $adapterMock->expects($this->once())->method('fetchCol')->with($selectMock, [])->willReturn([1, 2, 3]);
  60. $this->assertEquals(
  61. [1, 2, 3],
  62. $this->model->getBatchIds($adapterMock, $selectMock, ['from' => 10, 'to' => 100])
  63. );
  64. }
  65. }