Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit f6b0a2a

Browse files
committed
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);
1 parent 466daaa commit f6b0a2a

File tree

1 file changed

+109
-61
lines changed
  • app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product

1 file changed

+109
-61
lines changed

app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/ImageTest.php

Lines changed: 109 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
class ImageTest extends \PHPUnit\Framework\TestCase
1818
{
19+
/**
20+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
21+
*/
22+
protected $objectManager;
23+
1924
/**
2025
* @var AdapterInterface | \PHPUnit_Framework_MockObject_MockObject
2126
*/
@@ -31,41 +36,14 @@ class ImageTest extends \PHPUnit\Framework\TestCase
3136
*/
3237
protected $resourceMock;
3338

34-
/**
35-
* @var Image
36-
*/
37-
protected $imageModel;
38-
39-
/**
40-
* @var int
41-
*/
42-
protected $imagesCount = 50;
43-
44-
/**
45-
* @var int
46-
*/
47-
protected $batchSize = 10;
48-
4939
protected function setUp(): void
5040
{
51-
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
52-
41+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
5342
$this->connectionMock = $this->createMock(AdapterInterface::class);
54-
5543
$this->resourceMock = $this->createMock(ResourceConnection::class);
5644
$this->resourceMock->method('getConnection')->willReturn($this->connectionMock);
5745
$this->resourceMock->method('getTableName')->willReturnArgument(0);
58-
5946
$this->generatorMock = $this->createMock(Generator::class);
60-
61-
$this->imageModel = $objectManager->getObject(
62-
Image::class,
63-
[
64-
'generator' => $this->generatorMock,
65-
'resourceConnection' => $this->resourceMock,
66-
'batchSize' => $this->batchSize
67-
]
68-
);
6947
}
7048

7149
/**
@@ -93,7 +71,11 @@ protected function getVisibleImagesSelectMock(): \PHPUnit_Framework_MockObject_M
9371
return $selectMock;
9472
}
9573

96-
public function testGetCountAllProductImages(): void
74+
/**
75+
* @param int $imagesCount
76+
* @dataProvider dataProvider
77+
*/
78+
public function testGetCountAllProductImages(int $imagesCount): void
9779
{
9880
$selectMock = $this->getVisibleImagesSelectMock();
9981
$selectMock->expects($this->exactly(2))
@@ -113,59 +95,125 @@ public function testGetCountAllProductImages(): void
11395
$this->connectionMock->expects($this->once())
11496
->method('fetchOne')
11597
->with($selectMock)
116-
->willReturn($this->imagesCount);
98+
->willReturn($imagesCount);
99+
100+
$imageModel = $this->objectManager->getObject(
101+
Image::class,
102+
[
103+
'generator' => $this->generatorMock,
104+
'resourceConnection' => $this->resourceMock
105+
]
106+
);
117107

118-
$this->assertSame($this->imagesCount, $this->imageModel->getCountAllProductImages());
108+
$this->assertSame($imagesCount, $imageModel->getCountAllProductImages());
119109
}
120110

121-
public function testGetAllProductImages(): void
111+
/**
112+
* @param int $imagesCount
113+
* @param int $batchSize
114+
* @dataProvider dataProvider
115+
*/
116+
public function testGetAllProductImages(int $imagesCount, int $batchSize): void
122117
{
123-
$getBatchIteratorMock = function ($selectMock, $imagesCount, $batchSize): array {
124-
$result = [];
125-
$count = $imagesCount / $batchSize;
126-
while ($count) {
127-
$count--;
128-
$result[$count] = $selectMock;
129-
}
130-
131-
return $result;
132-
};
133-
134-
$getFetchResults = function ($batchSize): array {
135-
$result = [];
136-
$count = $batchSize;
137-
while ($count) {
138-
$count--;
139-
$result[$count] = $count;
140-
}
141-
142-
return $result;
143-
};
144-
145118
$this->connectionMock->expects($this->once())
146119
->method('select')
147120
->willReturn($this->getVisibleImagesSelectMock());
148121

149-
$fetchResult = $getFetchResults($this->batchSize);
150-
$this->connectionMock->expects($this->exactly($this->imagesCount / $this->batchSize))
122+
$batchCount = (int)ceil($imagesCount / $batchSize);
123+
$fetchResultsCallback = $this->getFetchResultCallbackForBatches($imagesCount, $batchSize);
124+
$this->connectionMock->expects($this->exactly($batchCount))
151125
->method('fetchAll')
152-
->willReturn($fetchResult);
126+
->will($this->returnCallback($fetchResultsCallback));
153127

154128
/** @var Select | \PHPUnit_Framework_MockObject_MockObject $selectMock */
155129
$selectMock = $this->getMockBuilder(Select::class)
156130
->disableOriginalConstructor()
157131
->getMock();
158132

159-
$batchIteratorMock = $getBatchIteratorMock($selectMock, $this->imagesCount, $this->batchSize);
160133
$this->generatorMock->expects($this->once())
161134
->method('generate')
162135
->with(
163136
'value_id',
164137
$selectMock,
165-
$this->batchSize,
138+
$batchSize,
166139
\Magento\Framework\DB\Query\BatchIteratorInterface::NON_UNIQUE_FIELD_ITERATOR
167-
)->willReturn($batchIteratorMock);
140+
)->will($this->returnCallback($this->getBatchIteratorCallback($selectMock, $batchCount)));
168141

169-
$this->assertCount($this->imagesCount, $this->imageModel->getAllProductImages());
142+
$imageModel = $this->objectManager->getObject(
143+
Image::class,
144+
[
145+
'generator' => $this->generatorMock,
146+
'resourceConnection' => $this->resourceMock,
147+
'batchSize' => $batchSize
148+
]
149+
);
150+
151+
$this->assertCount($imagesCount, $imageModel->getAllProductImages());
152+
}
153+
154+
/**
155+
* @param int $imagesCount
156+
* @param int $batchSize
157+
* @return \Closure
158+
*/
159+
protected function getFetchResultCallbackForBatches(int $imagesCount, int $batchSize): \Closure
160+
{
161+
$fetchResultsCallback = function () use (&$imagesCount, $batchSize) {
162+
$batchSize = ($imagesCount >= $batchSize) ? $batchSize : $imagesCount;
163+
$imagesCount -= $batchSize;
164+
165+
$getFetchResults = function ($batchSize): array {
166+
$result = [];
167+
$count = $batchSize;
168+
while ($count) {
169+
$count--;
170+
$result[$count] = $count;
171+
}
172+
173+
return $result;
174+
};
175+
176+
return $getFetchResults($batchSize);
177+
};
178+
179+
return $fetchResultsCallback;
180+
}
181+
182+
/**
183+
* @param Select | \PHPUnit_Framework_MockObject_MockObject $selectMock
184+
* @param int $batchCount
185+
* @return \Closure
186+
*/
187+
protected function getBatchIteratorCallback(
188+
\PHPUnit_Framework_MockObject_MockObject $selectMock,
189+
int $batchCount
190+
): \Closure
191+
{
192+
$getBatchIteratorCallback = function () use ($batchCount, $selectMock): array {
193+
$result = [];
194+
$count = $batchCount;
195+
while ($count) {
196+
$count--;
197+
$result[$count] = $selectMock;
198+
}
199+
200+
return $result;
201+
};
202+
203+
return $getBatchIteratorCallback;
204+
}
205+
206+
/**
207+
* Data Provider
208+
* @return array
209+
*/
210+
public function dataProvider(): array
211+
{
212+
return [
213+
[300, 100],
214+
[139, 100],
215+
[67, 10],
216+
[154, 47]
217+
];
170218
}
171219
}

0 commit comments

Comments
 (0)