16
16
17
17
class ImageTest extends \PHPUnit \Framework \TestCase
18
18
{
19
+ /**
20
+ * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
21
+ */
22
+ protected $ objectManager ;
23
+
19
24
/**
20
25
* @var AdapterInterface | \PHPUnit_Framework_MockObject_MockObject
21
26
*/
@@ -31,41 +36,14 @@ class ImageTest extends \PHPUnit\Framework\TestCase
31
36
*/
32
37
protected $ resourceMock ;
33
38
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
-
49
39
protected function setUp (): void
50
40
{
51
- $ objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
52
-
41
+ $ this ->objectManager = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
53
42
$ this ->connectionMock = $ this ->createMock (AdapterInterface::class);
54
-
55
43
$ this ->resourceMock = $ this ->createMock (ResourceConnection::class);
56
44
$ this ->resourceMock ->method ('getConnection ' )->willReturn ($ this ->connectionMock );
57
45
$ this ->resourceMock ->method ('getTableName ' )->willReturnArgument (0 );
58
-
59
46
$ 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
- );
69
47
}
70
48
71
49
/**
@@ -93,7 +71,11 @@ protected function getVisibleImagesSelectMock(): \PHPUnit_Framework_MockObject_M
93
71
return $ selectMock ;
94
72
}
95
73
96
- public function testGetCountAllProductImages (): void
74
+ /**
75
+ * @param int $imagesCount
76
+ * @dataProvider dataProvider
77
+ */
78
+ public function testGetCountAllProductImages (int $ imagesCount ): void
97
79
{
98
80
$ selectMock = $ this ->getVisibleImagesSelectMock ();
99
81
$ selectMock ->expects ($ this ->exactly (2 ))
@@ -113,59 +95,125 @@ public function testGetCountAllProductImages(): void
113
95
$ this ->connectionMock ->expects ($ this ->once ())
114
96
->method ('fetchOne ' )
115
97
->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
+ );
117
107
118
- $ this ->assertSame ($ this -> imagesCount , $ this -> imageModel ->getCountAllProductImages ());
108
+ $ this ->assertSame ($ imagesCount , $ imageModel ->getCountAllProductImages ());
119
109
}
120
110
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
122
117
{
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
-
145
118
$ this ->connectionMock ->expects ($ this ->once ())
146
119
->method ('select ' )
147
120
->willReturn ($ this ->getVisibleImagesSelectMock ());
148
121
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 ))
151
125
->method ('fetchAll ' )
152
- ->willReturn ( $ fetchResult );
126
+ ->will ( $ this -> returnCallback ( $ fetchResultsCallback ) );
153
127
154
128
/** @var Select | \PHPUnit_Framework_MockObject_MockObject $selectMock */
155
129
$ selectMock = $ this ->getMockBuilder (Select::class)
156
130
->disableOriginalConstructor ()
157
131
->getMock ();
158
132
159
- $ batchIteratorMock = $ getBatchIteratorMock ($ selectMock , $ this ->imagesCount , $ this ->batchSize );
160
133
$ this ->generatorMock ->expects ($ this ->once ())
161
134
->method ('generate ' )
162
135
->with (
163
136
'value_id ' ,
164
137
$ selectMock ,
165
- $ this -> batchSize ,
138
+ $ batchSize ,
166
139
\Magento \Framework \DB \Query \BatchIteratorInterface::NON_UNIQUE_FIELD_ITERATOR
167
- )->willReturn ( $ batchIteratorMock );
140
+ )->will ( $ this -> returnCallback ( $ this -> getBatchIteratorCallback ( $ selectMock , $ batchCount )) );
168
141
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
+ ];
170
218
}
171
219
}
0 commit comments