Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
catalog:images:resize fails to process all images -> Possible underlying Magento/Framework/DB/Query/Generator issue
- fix code style;
  • Loading branch information
vpodorozh committed Oct 30, 2018
1 parent 546e7ce commit c95ce3c
Showing 1 changed file with 39 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Magento\Framework\DB\Select;
use Magento\Framework\App\ResourceConnection;
use Magento\Catalog\Model\ResourceModel\Product\Gallery;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
use Magento\Framework\DB\Query\BatchIteratorInterface;

class ImageTest extends \PHPUnit\Framework\TestCase
{
Expand All @@ -22,34 +24,37 @@ class ImageTest extends \PHPUnit\Framework\TestCase
protected $objectManager;

/**
* @var AdapterInterface | \PHPUnit_Framework_MockObject_MockObject
* @var AdapterInterface | MockObject
*/
protected $connectionMock;

/**
* @var Generator | \PHPUnit_Framework_MockObject_MockObject
* @var Generator | MockObject
*/
protected $generatorMock;

/**
* @var ResourceConnection | \PHPUnit_Framework_MockObject_MockObject
* @var ResourceConnection | MockObject
*/
protected $resourceMock;

protected function setUp(): void
{
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->objectManager =
new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->connectionMock = $this->createMock(AdapterInterface::class);
$this->resourceMock = $this->createMock(ResourceConnection::class);
$this->resourceMock->method('getConnection')->willReturn($this->connectionMock);
$this->resourceMock->method('getTableName')->willReturnArgument(0);
$this->resourceMock->method('getConnection')
->willReturn($this->connectionMock);
$this->resourceMock->method('getTableName')
->willReturnArgument(0);
$this->generatorMock = $this->createMock(Generator::class);
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject
* @return MockObject
*/
protected function getVisibleImagesSelectMock(): \PHPUnit_Framework_MockObject_MockObject
protected function getVisibleImagesSelectMock(): MockObject
{
$selectMock = $this->getMockBuilder(Select::class)
->disableOriginalConstructor()
Expand Down Expand Up @@ -105,16 +110,21 @@ public function testGetCountAllProductImages(int $imagesCount): void
]
);

$this->assertSame($imagesCount, $imageModel->getCountAllProductImages());
$this->assertSame(
$imagesCount,
$imageModel->getCountAllProductImages()
);
}

/**
* @param int $imagesCount
* @param int $batchSize
* @dataProvider dataProvider
*/
public function testGetAllProductImages(int $imagesCount, int $batchSize): void
{
public function testGetAllProductImages(
int $imagesCount,
int $batchSize
): void {
$this->connectionMock->expects($this->once())
->method('select')
->willReturn($this->getVisibleImagesSelectMock());
Expand All @@ -125,7 +135,7 @@ public function testGetAllProductImages(int $imagesCount, int $batchSize): void
->method('fetchAll')
->will($this->returnCallback($fetchResultsCallback));

/** @var Select | \PHPUnit_Framework_MockObject_MockObject $selectMock */
/** @var Select | MockObject $selectMock */
$selectMock = $this->getMockBuilder(Select::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -136,8 +146,12 @@ public function testGetAllProductImages(int $imagesCount, int $batchSize): void
'value_id',
$selectMock,
$batchSize,
\Magento\Framework\DB\Query\BatchIteratorInterface::NON_UNIQUE_FIELD_ITERATOR
)->will($this->returnCallback($this->getBatchIteratorCallback($selectMock, $batchCount)));
BatchIteratorInterface::NON_UNIQUE_FIELD_ITERATOR
)->will(
$this->returnCallback(
$this->getBatchIteratorCallback($selectMock, $batchCount)
)
);

$imageModel = $this->objectManager->getObject(
Image::class,
Expand All @@ -156,10 +170,13 @@ public function testGetAllProductImages(int $imagesCount, int $batchSize): void
* @param int $batchSize
* @return \Closure
*/
protected function getFetchResultCallbackForBatches(int $imagesCount, int $batchSize): \Closure
{
protected function getFetchResultCallbackForBatches(
int $imagesCount,
int $batchSize
): \Closure {
$fetchResultsCallback = function () use (&$imagesCount, $batchSize) {
$batchSize = ($imagesCount >= $batchSize) ? $batchSize : $imagesCount;
$batchSize =
($imagesCount >= $batchSize) ? $batchSize : $imagesCount;
$imagesCount -= $batchSize;

$getFetchResults = function ($batchSize): array {
Expand All @@ -180,16 +197,15 @@ protected function getFetchResultCallbackForBatches(int $imagesCount, int $batch
}

/**
* @param Select | \PHPUnit_Framework_MockObject_MockObject $selectMock
* @param Select | MockObject $selectMock
* @param int $batchCount
* @return \Closure
*/
protected function getBatchIteratorCallback(
\PHPUnit_Framework_MockObject_MockObject $selectMock,
MockObject $selectMock,
int $batchCount
): \Closure
{
$getBatchIteratorCallback = function () use ($batchCount, $selectMock): array {
): \Closure {
$iteratorCallback = function () use ($batchCount, $selectMock): array {
$result = [];
$count = $batchCount;
while ($count) {
Expand All @@ -200,7 +216,7 @@ protected function getBatchIteratorCallback(
return $result;
};

return $getBatchIteratorCallback;
return $iteratorCallback;
}

/**
Expand Down

0 comments on commit c95ce3c

Please sign in to comment.