Skip to content

Commit b08a1e8

Browse files
MAGETWO-92693: Some improvements on product create|edit page in admin area
1 parent c3aa634 commit b08a1e8

File tree

1 file changed

+43
-23
lines changed

1 file changed

+43
-23
lines changed

app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ protected function setUp()
195195
'load',
196196
'getOptions',
197197
'getSku',
198+
'getId',
198199
'hasGalleryAttribute',
199200
'getMediaConfig',
200201
'getMediaAttributes',
@@ -282,7 +283,7 @@ protected function setUp()
282283
*/
283284
public function testGetAbsentProduct()
284285
{
285-
$this->productFactory->expects($this->once())->method('create')
286+
$this->productFactory->expects($this->never())->method('create')
286287
->will($this->returnValue($this->product));
287288
$this->resourceModelMock->expects($this->once())->method('getIdBySku')->with('test_sku')
288289
->will($this->returnValue(null));
@@ -293,38 +294,41 @@ public function testGetAbsentProduct()
293294
public function testCreateCreatesProduct()
294295
{
295296
$sku = 'test_sku';
296-
$this->productFactory->expects($this->once())->method('create')
297-
->will($this->returnValue($this->product));
298297
$this->resourceModelMock->expects($this->once())->method('getIdBySku')->with($sku)
299298
->will($this->returnValue('test_id'));
299+
$this->productFactory->expects($this->once())->method('create')
300+
->will($this->returnValue($this->product));
300301
$this->product->expects($this->once())->method('load')->with('test_id');
301-
$this->product->expects($this->once())->method('getSku')->willReturn($sku);
302+
$this->product->expects($this->any())->method('getId')->willReturn('test_id');
303+
$this->product->expects($this->any())->method('getSku')->willReturn($sku);
302304
$this->assertEquals($this->product, $this->model->get($sku));
303305
}
304306

305307
public function testGetProductInEditMode()
306308
{
307309
$sku = 'test_sku';
308-
$this->productFactory->expects($this->once())->method('create')
309-
->will($this->returnValue($this->product));
310310
$this->resourceModelMock->expects($this->once())->method('getIdBySku')->with($sku)
311311
->will($this->returnValue('test_id'));
312+
$this->productFactory->expects($this->once())->method('create')
313+
->will($this->returnValue($this->product));
312314
$this->product->expects($this->once())->method('setData')->with('_edit_mode', true);
313315
$this->product->expects($this->once())->method('load')->with('test_id');
314-
$this->product->expects($this->once())->method('getSku')->willReturn($sku);
316+
$this->product->expects($this->any())->method('getId')->willReturn('test_id');
317+
$this->product->expects($this->any())->method('getSku')->willReturn($sku);
315318
$this->assertEquals($this->product, $this->model->get($sku, true));
316319
}
317320

318321
public function testGetBySkuWithSpace()
319322
{
320323
$trimmedSku = 'test_sku';
321324
$sku = 'test_sku ';
322-
$this->productFactory->expects($this->once())->method('create')
323-
->will($this->returnValue($this->product));
324325
$this->resourceModelMock->expects($this->once())->method('getIdBySku')->with($sku)
325326
->will($this->returnValue('test_id'));
327+
$this->productFactory->expects($this->once())->method('create')
328+
->will($this->returnValue($this->product));
326329
$this->product->expects($this->once())->method('load')->with('test_id');
327-
$this->product->expects($this->once())->method('getSku')->willReturn($trimmedSku);
330+
$this->product->expects($this->any())->method('getId')->willReturn('test_id');
331+
$this->product->expects($this->any())->method('getSku')->willReturn($trimmedSku);
328332
$this->assertEquals($this->product, $this->model->get($sku));
329333
}
330334

@@ -333,12 +337,12 @@ public function testGetWithSetStoreId()
333337
$productId = 123;
334338
$sku = 'test-sku';
335339
$storeId = 7;
336-
$this->productFactory->expects($this->once())->method('create')->willReturn($this->product);
337340
$this->resourceModelMock->expects($this->once())->method('getIdBySku')->with($sku)->willReturn($productId);
341+
$this->productFactory->expects($this->once())->method('create')->willReturn($this->product);
338342
$this->product->expects($this->once())->method('setData')->with('store_id', $storeId);
339343
$this->product->expects($this->once())->method('load')->with($productId);
340-
$this->product->expects($this->once())->method('getId')->willReturn($productId);
341-
$this->product->expects($this->once())->method('getSku')->willReturn($sku);
344+
$this->product->expects($this->any())->method('getId')->willReturn($productId);
345+
$this->product->expects($this->any())->method('getSku')->willReturn($sku);
342346
$this->assertSame($this->product, $this->model->get($sku, false, $storeId));
343347
}
344348

@@ -498,13 +502,13 @@ public function testGetForcedReload()
498502
$editMode = false;
499503
$storeId = 0;
500504

505+
$this->resourceModelMock->expects($this->exactly(2))->method('getIdBySku')
506+
->with($sku)->willReturn($id);
501507
$this->productFactory->expects($this->exactly(2))->method('create')
502508
->will($this->returnValue($this->product));
503509
$this->product->expects($this->exactly(2))->method('load');
504-
$this->product->expects($this->exactly(2))->method('getId')->willReturn($sku);
505-
$this->resourceModelMock->expects($this->exactly(2))->method('getIdBySku')
506-
->with($sku)->willReturn($id);
507-
$this->product->expects($this->exactly(2))->method('getSku')->willReturn($sku);
510+
$this->product->expects($this->any())->method('getId')->willReturn($id);
511+
$this->product->expects($this->any())->method('getSku')->willReturn($sku);
508512

509513
$this->assertEquals($this->product, $this->model->get($sku, $editMode, $storeId));
510514
//second invocation should just return from cache
@@ -550,8 +554,9 @@ public function testGetBySkuFromCacheInitializedInGetById()
550554

551555
public function testSaveExisting()
552556
{
557+
$id = 100;
553558
$this->storeManagerMock->expects($this->any())->method('getWebsites')->willReturn([1 => 'default']);
554-
$this->resourceModelMock->expects($this->any())->method('getIdBySku')->will($this->returnValue(100));
559+
$this->resourceModelMock->expects($this->any())->method('getIdBySku')->will($this->returnValue($id));
555560
$this->productFactory->expects($this->any())
556561
->method('create')
557562
->will($this->returnValue($this->product));
@@ -565,15 +570,20 @@ public function testSaveExisting()
565570
->will($this->returnValue($this->productData));
566571
$this->product->expects($this->once())->method('getWebsiteIds')->willReturn([]);
567572
$this->product->expects($this->atLeastOnce())->method('getSku')->willReturn($this->productData['sku']);
573+
$this->product->expects($this->at(0))->method('getId')->willReturn(null);
574+
$this->product->expects($this->any())->method('getId')->willReturn($id);
568575

569576
$this->assertEquals($this->product, $this->model->save($this->product));
570577
}
571578

572579
public function testSaveNew()
573580
{
581+
$id = 100;
574582
$this->storeManagerMock->expects($this->any())->method('getWebsites')->willReturn([1 => 'default']);
575583
$this->resourceModelMock->expects($this->at(0))->method('getIdBySku')->will($this->returnValue(null));
576-
$this->resourceModelMock->expects($this->at(3))->method('getIdBySku')->will($this->returnValue(100));
584+
$this->resourceModelMock->expects($this->at(3))->method('getIdBySku')->will($this->returnValue($id));
585+
$this->product->expects($this->at(0))->method('getId')->willReturn(null);
586+
$this->product->expects($this->any())->method('getId')->willReturn($id);
577587
$this->productFactory->expects($this->any())
578588
->method('create')
579589
->will($this->returnValue($this->product));
@@ -603,7 +613,7 @@ public function testSaveUnableToSaveException()
603613
$this->resourceModelMock->expects($this->exactly(1))
604614
->method('getIdBySku')
605615
->willReturn(null);
606-
$this->productFactory->expects($this->exactly(2))
616+
$this->productFactory->expects($this->exactly(1))
607617
->method('create')
608618
->will($this->returnValue($this->product));
609619
$this->initializationHelperMock->expects($this->never())->method('initialize');
@@ -633,7 +643,7 @@ public function testSaveException()
633643
$this->resourceModelMock->expects($this->exactly(1))
634644
->method('getIdBySku')
635645
->willReturn(null);
636-
$this->productFactory->expects($this->exactly(2))
646+
$this->productFactory->expects($this->exactly(1))
637647
->method('create')
638648
->will($this->returnValue($this->product));
639649
$this->initializationHelperMock->expects($this->never())->method('initialize');
@@ -662,7 +672,7 @@ public function testSaveInvalidProductException()
662672
{
663673
$this->storeManagerMock->expects($this->any())->method('getWebsites')->willReturn([1 => 'default']);
664674
$this->resourceModelMock->expects($this->exactly(1))->method('getIdBySku')->will($this->returnValue(null));
665-
$this->productFactory->expects($this->exactly(2))
675+
$this->productFactory->expects($this->exactly(1))
666676
->method('create')
667677
->will($this->returnValue($this->product));
668678
$this->initializationHelperMock->expects($this->never())->method('initialize');
@@ -744,6 +754,7 @@ public function testDeleteById()
744754
->will($this->returnValue('42'));
745755
$this->product->expects($this->once())->method('load')->with('42');
746756
$this->product->expects($this->atLeastOnce())->method('getSku')->willReturn($sku);
757+
$this->product->expects($this->atLeastOnce())->method('getId')->willReturn(42);
747758
$this->assertTrue($this->model->deleteById($sku));
748759
}
749760

@@ -842,8 +853,9 @@ public function cacheKeyDataProvider()
842853
*/
843854
public function testSaveExistingWithOptions(array $newOptions, array $existingOptions, array $expectedData)
844855
{
856+
$id = 100;
845857
$this->storeManagerMock->expects($this->any())->method('getWebsites')->willReturn([1 => 'default']);
846-
$this->resourceModelMock->expects($this->any())->method('getIdBySku')->will($this->returnValue(100));
858+
$this->resourceModelMock->expects($this->any())->method('getIdBySku')->will($this->returnValue($id));
847859
$this->productFactory->expects($this->any())
848860
->method('create')
849861
->will($this->returnValue($this->initializedProductMock));
@@ -863,6 +875,8 @@ public function testSaveExistingWithOptions(array $newOptions, array $existingOp
863875
$this->initializedProductMock->expects($this->atLeastOnce())
864876
->method('getSku')->willReturn($this->productData['sku']);
865877
$this->product->expects($this->atLeastOnce())->method('getSku')->willReturn($this->productData['sku']);
878+
$this->initializedProductMock->expects($this->at(0))->method('getId')->willReturn(null);
879+
$this->initializedProductMock->expects($this->any())->method('getId')->willReturn($id);
866880

867881
$this->assertEquals($this->initializedProductMock, $this->model->save($this->product));
868882
}
@@ -1019,6 +1033,7 @@ public function testSaveWithLinks(array $newLinks, array $existingLinks, array $
10191033
$this->productFactory->expects($this->any())
10201034
->method('create')
10211035
->will($this->returnValue($this->initializedProductMock));
1036+
$this->initializedProductMock->method('getId')->willReturn(100);
10221037
$this->initializationHelperMock->expects($this->never())->method('initialize');
10231038
$this->resourceModelMock->expects($this->once())->method('validate')->with($this->initializedProductMock)
10241039
->willReturn(true);
@@ -1260,6 +1275,8 @@ public function testSaveExistingWithNewMediaGalleryEntries()
12601275
$this->initializedProductMock->expects($this->atLeastOnce())
12611276
->method('getSku')->willReturn($this->productData['sku']);
12621277
$this->product->expects($this->atLeastOnce())->method('getSku')->willReturn($this->productData['sku']);
1278+
$this->initializedProductMock->expects($this->at(0))->method('getId')->willReturn(null);
1279+
$this->initializedProductMock->expects($this->any())->method('getId')->willReturn(100);
12631280

12641281
$this->model->save($this->product);
12651282
}
@@ -1301,6 +1318,8 @@ public function testSaveWithDifferentWebsites()
13011318
$this->product->method('setWebsiteIds')->willReturn([2,3]);
13021319
$this->product->method('getSku')
13031320
->willReturn('simple');
1321+
$this->product->expects($this->at(0))->method('getId')->willReturn(null);
1322+
$this->product->expects($this->any())->method('getId')->willReturn(100);
13041323

13051324
$this->assertEquals($this->product, $this->model->save($this->product));
13061325
}
@@ -1353,6 +1372,7 @@ public function testSaveExistingWithMediaGalleryEntries()
13531372
->method('getSku')->willReturn($this->productData['sku']);
13541373
$this->product->expects($this->atLeastOnce())->method('getSku')->willReturn($this->productData['sku']);
13551374
$this->product->expects($this->any())->method('getMediaGalleryEntries')->willReturn(null);
1375+
$this->initializedProductMock->expects($this->any())->method('getId')->willReturn(100);
13561376
$this->model->save($this->product);
13571377
}
13581378
}

0 commit comments

Comments
 (0)