From f114f2aa61decaaecff02ebc6e2cabcc12569bd3 Mon Sep 17 00:00:00 2001 From: Vladyslav Podorozhnyi Date: Thu, 25 Oct 2018 15:18:26 +0300 Subject: [PATCH 01/10] magento/magento2#18387: catalog:images:resize fails to process all images -> Possible underlying Magento/Framework/DB/Query/Generator issue - fix getCountAllProductImages select and cover class with unit tests. (cherry picked from commit 6a6079d) --- .../Model/ResourceModel/Product/Image.php | 8 +- .../Model/ResourceModel/Product/ImageTest.php | 171 ++++++++++++++++++ 2 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Image.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Image.php index 5f83f9826abb5..1a515a3df82a4 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Image.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Image.php @@ -80,7 +80,13 @@ public function getAllProductImages(): \Generator */ public function getCountAllProductImages(): int { - $select = $this->getVisibleImagesSelect()->reset('columns')->columns('count(*)'); + $select = $this->getVisibleImagesSelect() + ->reset('columns') + ->reset('distinct') + ->columns( + new \Zend_Db_Expr('count(distinct value)') + ); + return (int) $this->connection->fetchOne($select); } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php new file mode 100644 index 0000000000000..16e245ba640cb --- /dev/null +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php @@ -0,0 +1,171 @@ +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->generatorMock = $this->createMock(Generator::class); + + $this->imageModel = $objectManager->getObject( + Image::class, + [ + 'generator' => $this->generatorMock, + 'resourceConnection' => $this->resourceMock, + 'batchSize' => $this->batchSize + ] + ); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject + */ + protected function getVisibleImagesSelectMock(): \PHPUnit_Framework_MockObject_MockObject + { + $selectMock = $this->getMockBuilder(Select::class) + ->disableOriginalConstructor() + ->getMock(); + $selectMock->expects($this->once()) + ->method('distinct') + ->willReturnSelf(); + $selectMock->expects($this->once()) + ->method('from') + ->with( + ['images' => Gallery::GALLERY_TABLE], + 'value as filepath' + )->willReturnSelf(); + $selectMock->expects($this->once()) + ->method('where') + ->with('disabled = 0') + ->willReturnSelf(); + + return $selectMock; + } + + public function testGetCountAllProductImages(): void + { + $selectMock = $this->getVisibleImagesSelectMock(); + $selectMock->expects($this->exactly(2)) + ->method('reset') + ->withConsecutive( + ['columns'], + ['distinct'] + )->willReturnSelf(); + $selectMock->expects($this->once()) + ->method('columns') + ->with(new \Zend_Db_Expr('count(distinct value)')) + ->willReturnSelf(); + + $this->connectionMock->expects($this->once()) + ->method('select') + ->willReturn($selectMock); + $this->connectionMock->expects($this->once()) + ->method('fetchOne') + ->with($selectMock) + ->willReturn($this->imagesCount); + + $this->assertSame($this->imagesCount, $this->imageModel->getCountAllProductImages()); + } + + public function testGetAllProductImages(): void + { + $getBatchIteratorMock = function ($selectMock, $imagesCount, $batchSize): array { + $result = []; + $count = $imagesCount / $batchSize; + while ($count) { + $count--; + $result[$count] = $selectMock; + } + + return $result; + }; + + $getAllProductImagesSelectFetchResults = function ($batchSize): array { + $result = []; + $count = $batchSize; + while ($count) { + $count--; + $result[$count] = $count; + } + + return $result; + }; + + $this->connectionMock->expects($this->once()) + ->method('select') + ->willReturn($this->getVisibleImagesSelectMock()); + + $fetchResult = $getAllProductImagesSelectFetchResults($this->batchSize); + $this->connectionMock->expects($this->exactly($this->imagesCount / $this->batchSize)) + ->method('fetchAll') + ->willReturn($fetchResult); + + /** @var Select | \PHPUnit_Framework_MockObject_MockObject $selectMock */ + $selectMock = $this->getMockBuilder(Select::class) + ->disableOriginalConstructor() + ->getMock(); + + $batchIteratorMock = $getBatchIteratorMock($selectMock, $this->imagesCount, $this->batchSize); + $this->generatorMock->expects($this->once()) + ->method('generate') + ->with( + 'value_id', + $selectMock, + $this->batchSize, + \Magento\Framework\DB\Query\BatchIteratorInterface::NON_UNIQUE_FIELD_ITERATOR + )->willReturn($batchIteratorMock); + + $this->assertCount($this->imagesCount, $this->imageModel->getAllProductImages()); + } +} From 95eeb9f6a022b1195b838965f65486a503d57137 Mon Sep 17 00:00:00 2001 From: Vladyslav Podorozhnyi Date: Thu, 25 Oct 2018 18:15:55 +0300 Subject: [PATCH 02/10] magento/magento2#18387: catalog:images:resize fails to process all images -> Possible underlying Magento/Framework/DB/Query/Generator issue - fix code style; (cherry picked from commit 466daaa) --- .../Test/Unit/Model/ResourceModel/Product/ImageTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php index 16e245ba640cb..9c783e237ab24 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php @@ -131,7 +131,7 @@ public function testGetAllProductImages(): void return $result; }; - $getAllProductImagesSelectFetchResults = function ($batchSize): array { + $getFetchResults = function ($batchSize): array { $result = []; $count = $batchSize; while ($count) { @@ -146,7 +146,7 @@ public function testGetAllProductImages(): void ->method('select') ->willReturn($this->getVisibleImagesSelectMock()); - $fetchResult = $getAllProductImagesSelectFetchResults($this->batchSize); + $fetchResult = $getFetchResults($this->batchSize); $this->connectionMock->expects($this->exactly($this->imagesCount / $this->batchSize)) ->method('fetchAll') ->willReturn($fetchResult); From 84590ded5ed62edede5a12a68d384433e51ce0f3 Mon Sep 17 00:00:00 2001 From: Sviatoslav Mankivskyi Date: Sat, 27 Oct 2018 00:50:31 +0300 Subject: [PATCH 03/10] ENGCOM-3292 catalog:images:resize total images count calculates incorrectly #18387: #18807 (cherry picked from commit e1a1c7b) --- .../Magento/Catalog/Model/ResourceModel/Product/Image.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Image.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Image.php index 1a515a3df82a4..068580927b96a 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Image.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Image.php @@ -12,8 +12,8 @@ use Magento\Framework\DB\Select; use Magento\Framework\App\ResourceConnection; -/** - * Class for fast retrieval of all product images +/* + * Class for retrieval of all product images */ class Image { @@ -76,6 +76,7 @@ public function getAllProductImages(): \Generator /** * Get the number of unique pictures of products + * * @return int */ public function getCountAllProductImages(): int @@ -91,6 +92,8 @@ public function getCountAllProductImages(): int } /** + * Return Select to fetch all products images + * * @return Select */ private function getVisibleImagesSelect(): Select From 4653d3e007500133b1b55012b469fd782738c680 Mon Sep 17 00:00:00 2001 From: Vladyslav Podorozhnyi Date: Sat, 27 Oct 2018 17:58:00 +0300 Subject: [PATCH 04/10] magento/magento2#18387: catalog:images:resize fails to process all images -> Possible underlying Magento/Framework/DB/Query/Generator issue - fix unit test - now it can test also not only full batches, but partial as well (139 images with batch size 100); (cherry picked from commit f6b0a2a) --- .../Model/ResourceModel/Product/ImageTest.php | 170 +++++++++++------- 1 file changed, 109 insertions(+), 61 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php index 9c783e237ab24..1866138cf16b6 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php @@ -16,6 +16,11 @@ class ImageTest extends \PHPUnit\Framework\TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + protected $objectManager; + /** * @var AdapterInterface | \PHPUnit_Framework_MockObject_MockObject */ @@ -31,41 +36,14 @@ class ImageTest extends \PHPUnit\Framework\TestCase */ protected $resourceMock; - /** - * @var Image - */ - protected $imageModel; - - /** - * @var int - */ - protected $imagesCount = 50; - - /** - * @var int - */ - protected $batchSize = 10; - protected function setUp(): void { - $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->generatorMock = $this->createMock(Generator::class); - - $this->imageModel = $objectManager->getObject( - Image::class, - [ - 'generator' => $this->generatorMock, - 'resourceConnection' => $this->resourceMock, - 'batchSize' => $this->batchSize - ] - ); } /** @@ -93,7 +71,11 @@ protected function getVisibleImagesSelectMock(): \PHPUnit_Framework_MockObject_M return $selectMock; } - public function testGetCountAllProductImages(): void + /** + * @param int $imagesCount + * @dataProvider dataProvider + */ + public function testGetCountAllProductImages(int $imagesCount): void { $selectMock = $this->getVisibleImagesSelectMock(); $selectMock->expects($this->exactly(2)) @@ -113,59 +95,125 @@ public function testGetCountAllProductImages(): void $this->connectionMock->expects($this->once()) ->method('fetchOne') ->with($selectMock) - ->willReturn($this->imagesCount); + ->willReturn($imagesCount); + + $imageModel = $this->objectManager->getObject( + Image::class, + [ + 'generator' => $this->generatorMock, + 'resourceConnection' => $this->resourceMock + ] + ); - $this->assertSame($this->imagesCount, $this->imageModel->getCountAllProductImages()); + $this->assertSame($imagesCount, $imageModel->getCountAllProductImages()); } - public function testGetAllProductImages(): void + /** + * @param int $imagesCount + * @param int $batchSize + * @dataProvider dataProvider + */ + public function testGetAllProductImages(int $imagesCount, int $batchSize): void { - $getBatchIteratorMock = function ($selectMock, $imagesCount, $batchSize): array { - $result = []; - $count = $imagesCount / $batchSize; - while ($count) { - $count--; - $result[$count] = $selectMock; - } - - return $result; - }; - - $getFetchResults = function ($batchSize): array { - $result = []; - $count = $batchSize; - while ($count) { - $count--; - $result[$count] = $count; - } - - return $result; - }; - $this->connectionMock->expects($this->once()) ->method('select') ->willReturn($this->getVisibleImagesSelectMock()); - $fetchResult = $getFetchResults($this->batchSize); - $this->connectionMock->expects($this->exactly($this->imagesCount / $this->batchSize)) + $batchCount = (int)ceil($imagesCount / $batchSize); + $fetchResultsCallback = $this->getFetchResultCallbackForBatches($imagesCount, $batchSize); + $this->connectionMock->expects($this->exactly($batchCount)) ->method('fetchAll') - ->willReturn($fetchResult); + ->will($this->returnCallback($fetchResultsCallback)); /** @var Select | \PHPUnit_Framework_MockObject_MockObject $selectMock */ $selectMock = $this->getMockBuilder(Select::class) ->disableOriginalConstructor() ->getMock(); - $batchIteratorMock = $getBatchIteratorMock($selectMock, $this->imagesCount, $this->batchSize); $this->generatorMock->expects($this->once()) ->method('generate') ->with( 'value_id', $selectMock, - $this->batchSize, + $batchSize, \Magento\Framework\DB\Query\BatchIteratorInterface::NON_UNIQUE_FIELD_ITERATOR - )->willReturn($batchIteratorMock); + )->will($this->returnCallback($this->getBatchIteratorCallback($selectMock, $batchCount))); - $this->assertCount($this->imagesCount, $this->imageModel->getAllProductImages()); + $imageModel = $this->objectManager->getObject( + Image::class, + [ + 'generator' => $this->generatorMock, + 'resourceConnection' => $this->resourceMock, + 'batchSize' => $batchSize + ] + ); + + $this->assertCount($imagesCount, $imageModel->getAllProductImages()); + } + + /** + * @param int $imagesCount + * @param int $batchSize + * @return \Closure + */ + protected function getFetchResultCallbackForBatches(int $imagesCount, int $batchSize): \Closure + { + $fetchResultsCallback = function () use (&$imagesCount, $batchSize) { + $batchSize = ($imagesCount >= $batchSize) ? $batchSize : $imagesCount; + $imagesCount -= $batchSize; + + $getFetchResults = function ($batchSize): array { + $result = []; + $count = $batchSize; + while ($count) { + $count--; + $result[$count] = $count; + } + + return $result; + }; + + return $getFetchResults($batchSize); + }; + + return $fetchResultsCallback; + } + + /** + * @param Select | \PHPUnit_Framework_MockObject_MockObject $selectMock + * @param int $batchCount + * @return \Closure + */ + protected function getBatchIteratorCallback( + \PHPUnit_Framework_MockObject_MockObject $selectMock, + int $batchCount + ): \Closure + { + $getBatchIteratorCallback = function () use ($batchCount, $selectMock): array { + $result = []; + $count = $batchCount; + while ($count) { + $count--; + $result[$count] = $selectMock; + } + + return $result; + }; + + return $getBatchIteratorCallback; + } + + /** + * Data Provider + * @return array + */ + public function dataProvider(): array + { + return [ + [300, 100], + [139, 100], + [67, 10], + [154, 47] + ]; } } From d2e53455f4a79a3dc71e9e267259bfabc799a577 Mon Sep 17 00:00:00 2001 From: Vladyslav Podorozhnyi Date: Sat, 27 Oct 2018 17:59:22 +0300 Subject: [PATCH 05/10] magento/magento2#18387: catalog:images:resize fails to process all images -> Possible underlying Magento/Framework/DB/Query/Generator issue - fix unit test - now it can test also not only full batches, but partial as well (139 images with batch size 100); (cherry picked from commit 546e7ce) --- .../Test/Unit/Model/ResourceModel/Product/ImageTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php index 1866138cf16b6..1a1eebf30f7f6 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php @@ -210,10 +210,12 @@ protected function getBatchIteratorCallback( public function dataProvider(): array { return [ + [300, 300], [300, 100], [139, 100], [67, 10], - [154, 47] + [154, 47], + [0, 100] ]; } } From 2539a716f5d54c66c89f64965d6fb6762b6c90e8 Mon Sep 17 00:00:00 2001 From: Vladyslav Podorozhnyi Date: Sat, 27 Oct 2018 18:08:39 +0300 Subject: [PATCH 06/10] magento/magento2#18387: catalog:images:resize fails to process all images -> Possible underlying Magento/Framework/DB/Query/Generator issue - remove method return type 'void' which can be used in php7.1 or higher. --- .../Test/Unit/Model/ResourceModel/Product/ImageTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php index 1a1eebf30f7f6..233f59172ee4d 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php @@ -36,7 +36,7 @@ class ImageTest extends \PHPUnit\Framework\TestCase */ protected $resourceMock; - protected function setUp(): void + protected function setUp() { $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->connectionMock = $this->createMock(AdapterInterface::class); @@ -75,7 +75,7 @@ protected function getVisibleImagesSelectMock(): \PHPUnit_Framework_MockObject_M * @param int $imagesCount * @dataProvider dataProvider */ - public function testGetCountAllProductImages(int $imagesCount): void + public function testGetCountAllProductImages(int $imagesCount) { $selectMock = $this->getVisibleImagesSelectMock(); $selectMock->expects($this->exactly(2)) @@ -113,7 +113,7 @@ public function testGetCountAllProductImages(int $imagesCount): void * @param int $batchSize * @dataProvider dataProvider */ - public function testGetAllProductImages(int $imagesCount, int $batchSize): void + public function testGetAllProductImages(int $imagesCount, int $batchSize) { $this->connectionMock->expects($this->once()) ->method('select') From 76bd08924e7e0f8a3fb88b3990623e5872b10760 Mon Sep 17 00:00:00 2001 From: Anton Evers Date: Mon, 11 Dec 2017 14:11:02 +0200 Subject: [PATCH 07/10] magento/magento2#18387: catalog:images:resize fails to process all images -> Possible underlying Magento/Framework/DB/Query/Generator issue Ranged selects always miss the last range Fix unit test. 11 batches expected to handle 105 items: 1: 1-10 2: 11-20 3: 21-30 4: 31-40 5: 41-50 6: 51-60 7: 61-70 8: 71-80 9: 81-90 10: 91-100 11: 101-105 (cherry picked from commit 6c24c0e) --- .../Magento/Framework/DB/Query/BatchRangeIterator.php | 4 ++-- .../Framework/Test/Unit/DB/Query/BatchRangeIteratorTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/DB/Query/BatchRangeIterator.php b/lib/internal/Magento/Framework/DB/Query/BatchRangeIterator.php index cc2f5a91f73fd..af0ddc9b18b9f 100644 --- a/lib/internal/Magento/Framework/DB/Query/BatchRangeIterator.php +++ b/lib/internal/Magento/Framework/DB/Query/BatchRangeIterator.php @@ -107,7 +107,7 @@ public function __construct( public function current() { if (null === $this->currentSelect) { - $this->isValid = ($this->currentOffset + $this->batchSize) <= $this->totalItemCount; + $this->isValid = $this->currentOffset < $this->totalItemCount; $this->currentSelect = $this->initSelectObject(); } return $this->currentSelect; @@ -138,7 +138,7 @@ public function next() if (null === $this->currentSelect) { $this->current(); } - $this->isValid = ($this->batchSize + $this->currentOffset) <= $this->totalItemCount; + $this->isValid = $this->currentOffset < $this->totalItemCount; $select = $this->initSelectObject(); if ($this->isValid) { $this->iteration++; diff --git a/lib/internal/Magento/Framework/Test/Unit/DB/Query/BatchRangeIteratorTest.php b/lib/internal/Magento/Framework/Test/Unit/DB/Query/BatchRangeIteratorTest.php index 22fdf0a05686a..9e2014c1b070a 100644 --- a/lib/internal/Magento/Framework/Test/Unit/DB/Query/BatchRangeIteratorTest.php +++ b/lib/internal/Magento/Framework/Test/Unit/DB/Query/BatchRangeIteratorTest.php @@ -116,6 +116,6 @@ public function testIterations() $iterations++; } - $this->assertEquals(10, $iterations); + $this->assertEquals(11, $iterations); } } From b81b5503a676778793eac3a321d59fd3741935ca Mon Sep 17 00:00:00 2001 From: Vladyslav Podorozhnyi Date: Mon, 11 Dec 2017 14:11:02 +0200 Subject: [PATCH 08/10] magento/magento2#18387: catalog:images:resize fails to process all images -> Possible underlying Magento/Framework/DB/Query/Generator issue - fix code style; --- .../Test/Unit/Model/ResourceModel/Product/ImageTest.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php index 233f59172ee4d..16b8ae43640cb 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php @@ -187,9 +187,8 @@ protected function getFetchResultCallbackForBatches(int $imagesCount, int $batch protected function getBatchIteratorCallback( \PHPUnit_Framework_MockObject_MockObject $selectMock, int $batchCount - ): \Closure - { - $getBatchIteratorCallback = function () use ($batchCount, $selectMock): array { + ): \Closure { + $iteratorCallback = function () use ($batchCount, $selectMock): array { $result = []; $count = $batchCount; while ($count) { @@ -200,7 +199,7 @@ protected function getBatchIteratorCallback( return $result; }; - return $getBatchIteratorCallback; + return $iteratorCallback; } /** From 051d82db553171cd784f543747ccff926287605d Mon Sep 17 00:00:00 2001 From: Sviatoslav Mankivskyi Date: Mon, 29 Oct 2018 19:58:56 +0200 Subject: [PATCH 09/10] ENGCOM-3292: catalog:images:resize total images count calculates incorrectly #18387: #18807 (cherry picked from commit f38a4fc) --- app/code/Magento/Catalog/Model/ResourceModel/Product/Image.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Image.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Image.php index 068580927b96a..77f67480619e0 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Image.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Image.php @@ -12,7 +12,7 @@ use Magento\Framework\DB\Select; use Magento\Framework\App\ResourceConnection; -/* +/** * Class for retrieval of all product images */ class Image From 10a1a881e8c7cbf983b800fe6479127e159a86e2 Mon Sep 17 00:00:00 2001 From: Vladyslav Podorozhnyi Date: Mon, 11 Dec 2017 14:11:02 +0200 Subject: [PATCH 10/10] magento/magento2#18387: catalog:images:resize fails to process all images -> Possible underlying Magento/Framework/DB/Query/Generator issue - fix code style; (cherry picked from commit c95ce3c) --- .../Model/ResourceModel/Product/ImageTest.php | 55 ++++++++++++------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php index 16b8ae43640cb..44f66b6cbf66e 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php @@ -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 { @@ -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() { - $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() @@ -105,7 +110,10 @@ public function testGetCountAllProductImages(int $imagesCount) ] ); - $this->assertSame($imagesCount, $imageModel->getCountAllProductImages()); + $this->assertSame( + $imagesCount, + $imageModel->getCountAllProductImages() + ); } /** @@ -113,8 +121,10 @@ public function testGetCountAllProductImages(int $imagesCount) * @param int $batchSize * @dataProvider dataProvider */ - public function testGetAllProductImages(int $imagesCount, int $batchSize) - { + public function testGetAllProductImages( + int $imagesCount, + int $batchSize + ) { $this->connectionMock->expects($this->once()) ->method('select') ->willReturn($this->getVisibleImagesSelectMock()); @@ -125,7 +135,7 @@ public function testGetAllProductImages(int $imagesCount, int $batchSize) ->method('fetchAll') ->will($this->returnCallback($fetchResultsCallback)); - /** @var Select | \PHPUnit_Framework_MockObject_MockObject $selectMock */ + /** @var Select | MockObject $selectMock */ $selectMock = $this->getMockBuilder(Select::class) ->disableOriginalConstructor() ->getMock(); @@ -136,8 +146,12 @@ public function testGetAllProductImages(int $imagesCount, int $batchSize) '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, @@ -156,10 +170,13 @@ public function testGetAllProductImages(int $imagesCount, int $batchSize) * @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 { @@ -180,12 +197,12 @@ 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 { $iteratorCallback = function () use ($batchCount, $selectMock): array {