b374k
m1n1 1.01
Apache/2.4.41 (Ubuntu)
Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64
uid=33(www-data) gid=33(www-data) groups=33(www-data)
server ip : 62.171.164.128 | your ip : 127.0.0.1
safemode OFF
 >  / home / dev2.destoffenstraat.com / vendor / magento / framework / Test / Unit / DB / Query /
Filename/home/dev2.destoffenstraat.com/vendor/magento/framework/Test/Unit/DB/Query/GeneratorTest.php
Size6.26 kb
Permissionrw-r--r--
Ownerroot : root
Create time17-Aug-2025 10:26
Last modified07-Jan-2021 21:08
Last accessed23-Aug-2025 03:56
Actionsedit | rename | delete | download (gzip)
Viewtext | code | image
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\Test\Unit\DB\Query;

use Magento\Framework\DB\Query\BatchIterator;
use Magento\Framework\DB\Query\BatchIteratorFactory;
use Magento\Framework\DB\Query\BatchRangeIteratorFactory;
use Magento\Framework\DB\Query\Generator;
use Magento\Framework\DB\Select;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class GeneratorTest extends TestCase
{
/**
* @var Generator
*/
private $model;

/**
* @var MockObject
*/
private $selectMock;

/**
* @var MockObject
*/
private $factoryMock;

/**
* @var MockObject
*/
private $iteratorMock;

/**
* @var MockObject
*/
private $rangeFactoryMock;

/**
* Setup test dependencies.
*/
protected function setUp(): void
{
$this->factoryMock = $this->createMock(BatchIteratorFactory::class);
$this->rangeFactoryMock = $this->createPartialMock(BatchRangeIteratorFactory::class, ['create']);
$this->selectMock = $this->createMock(Select::class);
$this->iteratorMock = $this->createMock(BatchIterator::class);
$this->model = new Generator($this->factoryMock, $this->rangeFactoryMock);
}

/**
* Test success generate.
* @return void
*/
public function testGenerate()
{
$map = [
[
Select::FROM,
[
'cp' => ['joinType' => Select::FROM]
]
],
[
Select::COLUMNS,
[
['cp', 'entity_id', 'product_id']
]
]
];
$this->selectMock->expects($this->exactly(2))->method('getPart')->willReturnMap($map);
$this->factoryMock->expects($this->once())->method('create')->with(
[
'select' => $this->selectMock,
'batchSize' => 100,
'correlationName' => 'cp',
'rangeField' => 'entity_id',
'rangeFieldAlias' => 'product_id'
]
)->willReturn($this->iteratorMock);
$this->assertEquals($this->iteratorMock, $this->model->generate('entity_id', $this->selectMock, 100));
}

/**
* Test batch generation with invalid select object.
*
* @return void
*/
public function testGenerateWithoutFromPart()
{
$this->expectException('Magento\Framework\Exception\LocalizedException');
$this->expectExceptionMessage('The select object must have the correct "FROM" part. Verify and try again.');
$map = [
[Select::FROM, []],
[
Select::COLUMNS,
[
['cp', 'entity_id', 'product_id']
]
]
];
$this->selectMock->expects($this->any())->method('getPart')->willReturnMap($map);
$this->factoryMock->expects($this->never())->method('create');
$this->model->generate('entity_id', $this->selectMock, 100);
}

/**
* Test generate batches with rangeField without alias.
* @return void
*/
public function testGenerateWithRangeFieldWithoutAlias()
{
$map = [
[
Select::FROM,
[
'cp' => ['joinType' => Select::FROM]
]
],
[
Select::COLUMNS,
[
['cp', 'entity_id', null]
]
]
];
$this->selectMock->expects($this->exactly(2))->method('getPart')->willReturnMap($map);
$this->factoryMock->expects($this->once())->method('create')->with(
[
'select' => $this->selectMock,
'batchSize' => 100,
'correlationName' => 'cp',
'rangeField' => 'entity_id',
'rangeFieldAlias' => 'entity_id'
]
)->willReturn($this->iteratorMock);
$this->assertEquals($this->iteratorMock, $this->model->generate('entity_id', $this->selectMock, 100));
}

/**
* Test generate batches with wild-card.
*
* @return void
*/
public function testGenerateWithInvalidWithWildcard()
{
$map = [
[
Select::FROM,
[
'cp' => ['joinType' => Select::FROM]
]
],
[
Select::COLUMNS,
[
['cp', '*', null]
]
]
];
$this->selectMock->expects($this->exactly(2))->method('getPart')->willReturnMap($map);
$this->factoryMock->expects($this->once())->method('create')->with(
[
'select' => $this->selectMock,
'batchSize' => 100,
'correlationName' => 'cp',
'rangeField' => 'entity_id',
'rangeFieldAlias' => 'entity_id'
]
)->willReturn($this->iteratorMock);
$this->assertEquals($this->iteratorMock, $this->model->generate('entity_id', $this->selectMock, 100));
}

/**
* Test success generate with non-unique strategy.
* @return void
*/
public function testGenerateWithNonUniqueStrategy()
{
$map = [
[
Select::FROM,
[
'cp' => ['joinType' => Select::FROM]
]
],
[
Select::COLUMNS,
[
['cp', 'entity_id', 'product_id']
]
]
];
$this->selectMock->expects($this->exactly(2))->method('getPart')->willReturnMap($map);
$this->factoryMock->expects($this->once())->method('create')->with(
[
'select' => $this->selectMock,
'batchSize' => 100,
'correlationName' => 'cp',
'rangeField' => 'entity_id',
'rangeFieldAlias' => 'product_id'
]
)->willReturn($this->iteratorMock);
$this->assertEquals(
$this->iteratorMock,
$this->model->generate('entity_id', $this->selectMock, 100, 'non_unique')
);
}
}