|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SpeckCatalogTest\Service; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Extensions\Database\TestCase; |
|
6
|
|
|
|
|
7
|
|
|
class ProductTest extends \PHPUnit_Framework_TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
public function testFind() |
|
10
|
|
|
{ |
|
11
|
|
|
$product = new \SpeckCatalog\Model\Product; |
|
12
|
|
|
|
|
13
|
|
|
$service = $this->getMock('\SpeckCatalog\Service\Product', array('populate')); |
|
14
|
|
|
$service->expects($this->atLeastOnce()) |
|
15
|
|
|
->method('populate') |
|
16
|
|
|
->with($product) |
|
17
|
|
|
->will($this->returnValue($product)); |
|
18
|
|
|
$mockMapper = $this->getMock('\SpeckCatalog\Mapper\Product'); |
|
19
|
|
|
$mockMapper->expects($this->once()) |
|
20
|
|
|
->method('find') |
|
21
|
|
|
->with(array('product_id' => 321)) |
|
22
|
|
|
->will($this->returnValue($product)); |
|
23
|
|
|
$service->setEntityMapper($mockMapper); |
|
24
|
|
|
|
|
25
|
|
|
$return = $service->find(array('product_id' => 321), true); |
|
26
|
|
|
$this->assertInstanceOf('\SpeckCatalog\Model\Product', $return); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testGetFullProduct() |
|
30
|
|
|
{ |
|
31
|
|
|
$product = new \SpeckCatalog\Model\Product; |
|
32
|
|
|
|
|
33
|
|
|
$service = $this->getMock('\SpeckCatalog\Service\Product', array('find', 'populate')); |
|
34
|
|
|
$service->expects($this->once()) |
|
35
|
|
|
->method('populate') |
|
36
|
|
|
->with($product) |
|
37
|
|
|
->will($this->returnValue($product)); |
|
38
|
|
|
$service->expects($this->once()) |
|
39
|
|
|
->method('find') |
|
40
|
|
|
->with(array('product_id' => 4321)) |
|
41
|
|
|
->will($this->returnValue($product)); |
|
42
|
|
|
|
|
43
|
|
|
$return = $service->getFullProduct(4321); |
|
44
|
|
|
$this->assertInstanceOf('\SpeckCatalog\Model\Product', $return); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testUpdateGetsValuesFromDataForOriginalValsFromArray() |
|
48
|
|
|
{ |
|
49
|
|
|
$data = array('product_id' => 1); |
|
50
|
|
|
$mockMapper = $this->getMock('\SpeckCatalog\Mapper\AbstractMapper'); |
|
51
|
|
|
$mockMapper->expects($this->once()) |
|
52
|
|
|
->method('update') |
|
53
|
|
|
->with($data, $data); |
|
54
|
|
|
$service = $this->getService(); |
|
55
|
|
|
$service->setEntityMapper($mockMapper); |
|
56
|
|
|
$service->update($data, null); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testUpdateGetsValuesFromDataForOriginalValsFromProduct() |
|
60
|
|
|
{ |
|
61
|
|
|
$data = array('product_id' => 97); |
|
62
|
|
|
$product = new \SpeckCatalog\Model\Product(); |
|
63
|
|
|
$product->setProductId(97); |
|
64
|
|
|
$mockMapper = $this->getMock('\SpeckCatalog\Mapper\AbstractMapper'); |
|
65
|
|
|
$mockMapper->expects($this->once()) |
|
66
|
|
|
->method('update') |
|
67
|
|
|
->with($product, $data); |
|
68
|
|
|
$service = $this->getService(); |
|
69
|
|
|
$service->setEntityMapper($mockMapper); |
|
70
|
|
|
$service->update($product, null); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getService() |
|
74
|
|
|
{ |
|
75
|
|
|
return new \SpeckCatalog\Service\Product(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testPopulatePopulatesProduct() |
|
79
|
|
|
{ |
|
80
|
|
|
$this->markTestIncomplete('Tests was broken/obsoleted'); |
|
81
|
|
|
$service = $this->getService(); |
|
82
|
|
|
|
|
83
|
|
|
$optionService = $this->getMock('\SpeckCatalog\Service\Option'); |
|
84
|
|
|
$optionService->expects($this->once()) |
|
85
|
|
|
->method('getByProductId') |
|
86
|
|
|
->will($this->returnValue(array(new \SpeckCatalog\Model\Option\Relational))); |
|
87
|
|
|
$service->setOptionService($optionService); |
|
88
|
|
|
|
|
89
|
|
|
$imageService = $this->getMock('\SpeckCatalog\Service\Image'); |
|
90
|
|
|
$imageService->expects($this->once()) |
|
91
|
|
|
->method('getImages') |
|
92
|
|
|
->will($this->returnValue(array(new \SpeckCatalog\Model\ProductImage\Relational))); |
|
93
|
|
|
$service->setImageService($imageService); |
|
94
|
|
|
|
|
95
|
|
|
$documentService = $this->getMock('\SpeckCatalog\Service\Document'); |
|
96
|
|
|
$documentService->expects($this->once()) |
|
97
|
|
|
->method('getDocuments') |
|
98
|
|
|
->will($this->returnValue(array(new \SpeckCatalog\Model\Document\Relational))); |
|
99
|
|
|
$service->setDocumentService($documentService); |
|
100
|
|
|
|
|
101
|
|
|
$productUomService = $this->getMock('\SpeckCatalog\Service\ProductUom'); |
|
102
|
|
|
$productUomService->expects($this->once()) |
|
103
|
|
|
->method('getByProductId') |
|
104
|
|
|
->will($this->returnValue(array(new \SpeckCatalog\Model\ProductUom\Relational))); |
|
105
|
|
|
$service->setProductUomService($productUomService); |
|
106
|
|
|
|
|
107
|
|
|
$specService = $this->getMock('\SpeckCatalog\Service\Spec'); |
|
108
|
|
|
$specService->expects($this->once()) |
|
109
|
|
|
->method('getByProductId') |
|
110
|
|
|
->will($this->returnValue(array(new \SpeckCatalog\Model\Spec\Relational))); |
|
111
|
|
|
$service->setSpecService($specService); |
|
112
|
|
|
|
|
113
|
|
|
$companyService = $this->getMock('\SpeckCatalog\Service\Company'); |
|
114
|
|
|
$companyService->expects($this->once()) |
|
115
|
|
|
->method('findById') |
|
116
|
|
|
->will($this->returnValue(new \SpeckCatalog\Model\Company\Relational)); |
|
117
|
|
|
$service->setCompanyService($companyService); |
|
118
|
|
|
|
|
119
|
|
|
$product = new \SpeckCatalog\Model\Product\Relational; |
|
120
|
|
|
$product->setProductId('111'); |
|
121
|
|
|
$service->populate($product); |
|
122
|
|
|
|
|
123
|
|
|
$options = $product->getOptions(); |
|
124
|
|
|
$this->assertTrue(is_array($options)); |
|
125
|
|
|
$this->assertTrue(count($options) === 1); |
|
126
|
|
|
|
|
127
|
|
|
$images = $product->getImages(); |
|
128
|
|
|
$this->assertTrue(is_array($images)); |
|
129
|
|
|
$this->assertTrue(count($images) === 1); |
|
130
|
|
|
|
|
131
|
|
|
$documents = $product->getDocuments(); |
|
132
|
|
|
$this->assertTrue(is_array($documents)); |
|
133
|
|
|
$this->assertTrue(count($documents) === 1); |
|
134
|
|
|
|
|
135
|
|
|
$uoms = $product->getUoms(); |
|
136
|
|
|
$this->assertTrue(is_array($uoms)); |
|
137
|
|
|
$this->assertTrue(count($uoms) === 1); |
|
138
|
|
|
|
|
139
|
|
|
$specs = $product->getSpecs(); |
|
140
|
|
|
$this->assertTrue(is_array($specs)); |
|
141
|
|
|
$this->assertTrue(count($specs) === 1); |
|
142
|
|
|
|
|
143
|
|
|
$manufacturer = $product->getManufacturer(); |
|
144
|
|
|
$this->assertInstanceOf('\SpeckCatalog\Model\Company', $manufacturer); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function testAddOptionWithModelsCallsAddOptionAndReturnsOption() |
|
148
|
|
|
{ |
|
149
|
|
|
$mockMapper = $this->getMock('\SpeckCatalog\Mapper\Product'); |
|
150
|
|
|
$mockMapper->expects($this->once()) |
|
151
|
|
|
->method('addOption') |
|
152
|
|
|
->with(1, 2); |
|
153
|
|
|
$mockOptionService = $this->getMock('\SpeckCatalog\Service\Option'); |
|
154
|
|
|
$mockOptionService->expects($this->once()) |
|
155
|
|
|
->method('find') |
|
156
|
|
|
->with(array('option_id' => 2)) |
|
157
|
|
|
->will($this->returnValue(new \SpeckCatalog\Model\Option)); |
|
158
|
|
|
|
|
159
|
|
|
$service = $this->getService(); |
|
160
|
|
|
$service->setEntityMapper($mockMapper); |
|
161
|
|
|
$service->setOptionService($mockOptionService); |
|
162
|
|
|
|
|
163
|
|
|
$return = $service->addOption(1, 2); |
|
164
|
|
|
$this->assertInstanceOf('\SpeckCatalog\Model\Option', $return); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
public function testAddOptionWithIdsCallsAddOptionAndReturnsOption() |
|
169
|
|
|
{ |
|
170
|
|
|
$mockMapper = $this->getMock('\SpeckCatalog\Mapper\Product'); |
|
171
|
|
|
$mockMapper->expects($this->once()) |
|
172
|
|
|
->method('addOption') |
|
173
|
|
|
->with(1, 2); |
|
174
|
|
|
$mockOptionService = $this->getMock('\SpeckCatalog\Service\Option'); |
|
175
|
|
|
$mockOptionService->expects($this->once()) |
|
176
|
|
|
->method('find') |
|
177
|
|
|
->with(array('option_id' => 2)) |
|
178
|
|
|
->will($this->returnValue(new \SpeckCatalog\Model\Option)); |
|
179
|
|
|
|
|
180
|
|
|
$service = $this->getService(); |
|
181
|
|
|
$service->setEntityMapper($mockMapper); |
|
182
|
|
|
$service->setOptionService($mockOptionService); |
|
183
|
|
|
|
|
184
|
|
|
$product = new \SpeckCatalog\Model\Product(); |
|
185
|
|
|
$product->setProductId(1); |
|
186
|
|
|
$option = new \SpeckCatalog\Model\Option(); |
|
187
|
|
|
$option->setOptionId(2); |
|
188
|
|
|
$return = $service->addOption($product, $option); |
|
189
|
|
|
$this->assertInstanceOf('\SpeckCatalog\Model\Option', $return); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function testSortOptionsCallsSortOptionsOnMapper() |
|
193
|
|
|
{ |
|
194
|
|
|
$mockMapper = $this->getMock('\SpeckCatalog\Mapper\Product'); |
|
195
|
|
|
$mockMapper->expects($this->once()) |
|
196
|
|
|
->method('sortOptions') |
|
197
|
|
|
->with(1, array(1,2)); |
|
198
|
|
|
$service = $this->getService(); |
|
199
|
|
|
$service->setEntityMapper($mockMapper); |
|
200
|
|
|
$service->sortOptions(1, array(1,2)); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function testRemoveOptionCallsRemoveOptionOnMapper() |
|
204
|
|
|
{ |
|
205
|
|
|
$mockMapper = $this->getMock('\SpeckCatalog\Mapper\Product'); |
|
206
|
|
|
$mockMapper->expects($this->once()) |
|
207
|
|
|
->method('removeOption') |
|
208
|
|
|
->with(1, 2); |
|
209
|
|
|
$service = $this->getService(); |
|
210
|
|
|
$service->setEntityMapper($mockMapper); |
|
211
|
|
|
$service->removeOption(array('product_id' => 1), array('option_id' => 2)); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
public function testInsertCallsInsertAndReturnsNewProduct() |
|
215
|
|
|
{ |
|
216
|
|
|
$product = new \SpeckCatalog\Model\Product; |
|
217
|
|
|
$mockMapper = $this->getMock('\SpeckCatalog\Mapper\Product'); |
|
218
|
|
|
$mockMapper->expects($this->once()) |
|
219
|
|
|
->method('insert') |
|
220
|
|
|
->with($product) |
|
221
|
|
|
->will($this->returnValue(1)); |
|
222
|
|
|
$mockMapper->expects($this->once()) |
|
223
|
|
|
->method('find') |
|
224
|
|
|
->will($this->returnValue(new \SpeckCatalog\Model\Product)); |
|
225
|
|
|
|
|
226
|
|
|
$service = $this->getService(); |
|
227
|
|
|
$service->setEntityMapper($mockMapper); |
|
228
|
|
|
$return = $service->insert($product); |
|
229
|
|
|
$this->assertInstanceOf('\SpeckCatalog\Model\Product', $return); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
public function testGetByCategoryIdSetsPaginatorAndCallsGetByCategoryIdOnMapper() |
|
233
|
|
|
{ |
|
234
|
|
|
$mockMapper = $this->getMock('\SpeckCatalog\Mapper\Product'); |
|
235
|
|
|
$mockMapper->expects($this->once()) |
|
236
|
|
|
->method('usePaginator'); |
|
237
|
|
|
$mockMapper->expects($this->once()) |
|
238
|
|
|
->method('getByCategoryId') |
|
239
|
|
|
->with(1); |
|
240
|
|
|
|
|
241
|
|
|
$service = $this->getService(); |
|
242
|
|
|
$service->setEntityMapper($mockMapper); |
|
243
|
|
|
$return = $service->getByCategoryId(1); |
|
|
|
|
|
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
public function testPopulateForPricingPopulatesRecursiveOptionsAndProductUoms() |
|
247
|
|
|
{ |
|
248
|
|
|
//method not implemented yet |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
public function testGetOptionService() |
|
252
|
|
|
{ |
|
253
|
|
|
$service = $this->getService(); |
|
254
|
|
|
$service->setOptionService($this->getMock('\SpeckCatalog\Service\Option')); |
|
255
|
|
|
$return = $service->getOptionService(); |
|
256
|
|
|
$this->assertInstanceOf('\SpeckCatalog\Service\Option', $return); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
public function testGetOptionServiceGetsOptionServiceFromServiceManager() |
|
260
|
|
|
{ |
|
261
|
|
|
$service = $this->getService(); |
|
262
|
|
|
$mockServiceManager = $this->getMock('\Zend\ServiceManager\ServiceManager'); |
|
263
|
|
|
$mockServiceManager->expects($this->once()) |
|
264
|
|
|
->method('get') |
|
265
|
|
|
->with('speckcatalog_option_service') |
|
266
|
|
|
->will($this->returnValue(new \SpeckCatalog\Service\Option)); |
|
267
|
|
|
|
|
268
|
|
|
$service->setServiceLocator($mockServiceManager); |
|
269
|
|
|
$service->getOptionService(); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
public function testGetProductUomService() |
|
273
|
|
|
{ |
|
274
|
|
|
$service = $this->getService(); |
|
275
|
|
|
$service->setProductUomService($this->getMock('\SpeckCatalog\Service\ProductUom')); |
|
276
|
|
|
$return = $service->getProductUomService(); |
|
277
|
|
|
$this->assertInstanceOf('\SpeckCatalog\Service\ProductUom', $return); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
public function testGetProductUomServiceGetsProductUomServiceFromServiceManager() |
|
281
|
|
|
{ |
|
282
|
|
|
$service = $this->getService(); |
|
283
|
|
|
$mockServiceManager = $this->getMock('\Zend\ServiceManager\ServiceManager'); |
|
284
|
|
|
$mockServiceManager->expects($this->once()) |
|
285
|
|
|
->method('get') |
|
286
|
|
|
->with('speckcatalog_product_uom_service') |
|
287
|
|
|
->will($this->returnValue(new \SpeckCatalog\Service\ProductUom)); |
|
288
|
|
|
|
|
289
|
|
|
$service->setServiceLocator($mockServiceManager); |
|
290
|
|
|
$service->getProductUomService(); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
public function testGetImageService() |
|
294
|
|
|
{ |
|
295
|
|
|
$service = $this->getService(); |
|
296
|
|
|
$service->setImageService($this->getMock('\SpeckCatalog\Service\Image')); |
|
297
|
|
|
$return = $service->getImageService(); |
|
298
|
|
|
$this->assertInstanceOf('\SpeckCatalog\Service\Image', $return); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
public function testGetImageServiceGetsImageServiceFromServiceManager() |
|
302
|
|
|
{ |
|
303
|
|
|
$service = $this->getService(); |
|
304
|
|
|
$mockServiceManager = $this->getMock('\Zend\ServiceManager\ServiceManager'); |
|
305
|
|
|
$mockServiceManager->expects($this->once()) |
|
306
|
|
|
->method('get') |
|
307
|
|
|
->with('speckcatalog_product_image_service') |
|
308
|
|
|
->will($this->returnValue(new \SpeckCatalog\Service\Image)); |
|
309
|
|
|
|
|
310
|
|
|
$service->setServiceLocator($mockServiceManager); |
|
311
|
|
|
$service->getImageService(); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
public function testGetDocumentService() |
|
315
|
|
|
{ |
|
316
|
|
|
$service = $this->getService(); |
|
317
|
|
|
$service->setDocumentService($this->getMock('\SpeckCatalog\Service\Document')); |
|
318
|
|
|
$return = $service->getDocumentService(); |
|
319
|
|
|
$this->assertInstanceOf('\SpeckCatalog\Service\Document', $return); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
public function testGetDocumentServiceGetsImageServiceFromServiceManager() |
|
323
|
|
|
{ |
|
324
|
|
|
$service = $this->getService(); |
|
325
|
|
|
$mockServiceManager = $this->getMock('\Zend\ServiceManager\ServiceManager'); |
|
326
|
|
|
$mockServiceManager->expects($this->once()) |
|
327
|
|
|
->method('get') |
|
328
|
|
|
->with('speckcatalog_document_service') |
|
329
|
|
|
->will($this->returnValue(new \SpeckCatalog\Service\Document)); |
|
330
|
|
|
|
|
331
|
|
|
$service->setServiceLocator($mockServiceManager); |
|
332
|
|
|
$service->getDocumentService(); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
public function testGetSpecService() |
|
336
|
|
|
{ |
|
337
|
|
|
$service = $this->getService(); |
|
338
|
|
|
$service->setSpecService($this->getMock('\SpeckCatalog\Service\Spec')); |
|
339
|
|
|
$return = $service->getSpecService(); |
|
340
|
|
|
$this->assertInstanceOf('\SpeckCatalog\Service\Spec', $return); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
public function testGetSpecServiceGetsImageServiceFromServiceManager() |
|
344
|
|
|
{ |
|
345
|
|
|
$service = $this->getService(); |
|
346
|
|
|
$mockServiceManager = $this->getMock('\Zend\ServiceManager\ServiceManager'); |
|
347
|
|
|
$mockServiceManager->expects($this->once()) |
|
348
|
|
|
->method('get') |
|
349
|
|
|
->with('speckcatalog_spec_service') |
|
350
|
|
|
->will($this->returnValue(new \SpeckCatalog\Service\Spec)); |
|
351
|
|
|
|
|
352
|
|
|
$service->setServiceLocator($mockServiceManager); |
|
353
|
|
|
$service->getSpecService(); |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
public function testGetCompanyService() |
|
357
|
|
|
{ |
|
358
|
|
|
$service = $this->getService(); |
|
359
|
|
|
$service->setCompanyService($this->getMock('\SpeckCatalog\Service\Company')); |
|
360
|
|
|
$return = $service->getCompanyService(); |
|
361
|
|
|
$this->assertInstanceOf('\SpeckCatalog\Service\Company', $return); |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
public function testGetCompanyServiceGetsImageServiceFromServiceManager() |
|
365
|
|
|
{ |
|
366
|
|
|
$service = $this->getService(); |
|
367
|
|
|
$mockServiceManager = $this->getMock('\Zend\ServiceManager\ServiceManager'); |
|
368
|
|
|
$mockServiceManager->expects($this->once()) |
|
369
|
|
|
->method('get') |
|
370
|
|
|
->with('speckcatalog_company_service') |
|
371
|
|
|
->will($this->returnValue(new \SpeckCatalog\Service\Company)); |
|
372
|
|
|
|
|
373
|
|
|
$service->setServiceLocator($mockServiceManager); |
|
374
|
|
|
$service->getCompanyService(); |
|
375
|
|
|
} |
|
376
|
|
|
} |
|
377
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.