Skip to content

Commit 0710311

Browse files
committed
MAGETWO-93807: [Forwardport] Some improvements on product create|edit page in admin area
1 parent 2546e77 commit 0710311

File tree

1 file changed

+15
-41
lines changed

1 file changed

+15
-41
lines changed

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

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,15 @@
55
* See COPYING.txt for license details.
66
*/
77

8-
declare(strict_types=1);
9-
108
namespace Magento\Catalog\Test\Unit\Model;
119

1210
use Magento\Catalog\Api\Data\ProductAttributeInterface;
1311
use Magento\Framework\Api\Data\ImageContentInterface;
14-
use Magento\Framework\Api\Data\ImageContentInterfaceFactory;
15-
use Magento\Framework\Api\ImageContentValidatorInterface;
1612
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
17-
use Magento\Framework\Api\SearchCriteriaBuilder;
1813
use Magento\Framework\DB\Adapter\ConnectionException;
1914
use Magento\Framework\Serialize\Serializer\Json;
2015
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
2116
use Magento\Store\Api\Data\StoreInterface;
22-
use PHPUnit_Framework_MockObject_MockObject as MockObject;
2317

2418
/**
2519
* Class ProductRepositoryTest
@@ -197,7 +191,6 @@ protected function setUp()
197191
'load',
198192
'getOptions',
199193
'getSku',
200-
'getId',
201194
'hasGalleryAttribute',
202195
'getMediaConfig',
203196
'getMediaAttributes',
@@ -312,7 +305,7 @@ function ($value) {
312305
*/
313306
public function testGetAbsentProduct()
314307
{
315-
$this->productFactoryMock->expects($this->never())->method('create')
308+
$this->productFactoryMock->expects($this->once())->method('create')
316309
->will($this->returnValue($this->productMock));
317310
$this->resourceModelMock->expects($this->once())->method('getIdBySku')->with('test_sku')
318311
->will($this->returnValue(null));
@@ -328,8 +321,7 @@ public function testCreateCreatesProduct()
328321
$this->resourceModelMock->expects($this->once())->method('getIdBySku')->with($sku)
329322
->will($this->returnValue('test_id'));
330323
$this->productMock->expects($this->once())->method('load')->with('test_id');
331-
$this->productMock->expects($this->any())->method('getId')->willReturn('test_id');
332-
$this->productMock->expects($this->any())->method('getSku')->willReturn($sku);
324+
$this->productMock->expects($this->once())->method('getSku')->willReturn($sku);
333325
$this->assertEquals($this->productMock, $this->model->get($sku));
334326
}
335327

@@ -342,7 +334,6 @@ public function testGetProductInEditMode()
342334
->will($this->returnValue('test_id'));
343335
$this->productMock->expects($this->once())->method('setData')->with('_edit_mode', true);
344336
$this->productMock->expects($this->once())->method('load')->with('test_id');
345-
$this->productMock->expects($this->any())->method('getId')->willReturn('test_id');
346337
$this->productMock->expects($this->once())->method('getSku')->willReturn($sku);
347338
$this->assertEquals($this->productMock, $this->model->get($sku, true));
348339
}
@@ -356,8 +347,7 @@ public function testGetBySkuWithSpace()
356347
$this->resourceModelMock->expects($this->once())->method('getIdBySku')->with($sku)
357348
->will($this->returnValue('test_id'));
358349
$this->productMock->expects($this->once())->method('load')->with('test_id');
359-
$this->productMock->expects($this->any())->method('getId')->willReturn('test_id');
360-
$this->productMock->expects($this->any())->method('getSku')->willReturn($trimmedSku);
350+
$this->productMock->expects($this->once())->method('getSku')->willReturn($trimmedSku);
361351
$this->assertEquals($this->productMock, $this->model->get($sku));
362352
}
363353

@@ -370,8 +360,8 @@ public function testGetWithSetStoreId()
370360
$this->resourceModelMock->expects($this->once())->method('getIdBySku')->with($sku)->willReturn($productId);
371361
$this->productMock->expects($this->once())->method('setData')->with('store_id', $storeId);
372362
$this->productMock->expects($this->once())->method('load')->with($productId);
373-
$this->productMock->expects($this->any())->method('getId')->willReturn($productId);
374-
$this->productMock->expects($this->any())->method('getSku')->willReturn($sku);
363+
$this->productMock->expects($this->once())->method('getId')->willReturn($productId);
364+
$this->productMock->expects($this->once())->method('getSku')->willReturn($sku);
375365
$this->assertSame($this->productMock, $this->model->get($sku, false, $storeId));
376366
}
377367

@@ -519,13 +509,13 @@ public function testGetForcedReload()
519509
$editMode = false;
520510
$storeId = 0;
521511

522-
$this->resourceModelMock->expects($this->exactly(2))->method('getIdBySku')
523-
->with($sku)->willReturn($id);
524512
$this->productFactoryMock->expects($this->exactly(2))->method('create')
525513
->will($this->returnValue($this->productMock));
526514
$this->productMock->expects($this->exactly(2))->method('load');
527-
$this->productMock->expects($this->any())->method('getId')->willReturn($id);
528-
$this->productMock->expects($this->any())->method('getSku')->willReturn($sku);
515+
$this->productMock->expects($this->exactly(2))->method('getId')->willReturn($sku);
516+
$this->resourceModelMock->expects($this->exactly(2))->method('getIdBySku')
517+
->with($sku)->willReturn($id);
518+
$this->productMock->expects($this->exactly(2))->method('getSku')->willReturn($sku);
529519
$this->serializerMock->expects($this->exactly(3))->method('serialize');
530520

531521
$this->assertEquals($this->productMock, $this->model->get($sku, $editMode, $storeId));
@@ -563,8 +553,7 @@ public function testGetBySkuFromCacheInitializedInGetById()
563553

564554
public function testSaveExisting()
565555
{
566-
$id = 100;
567-
$this->resourceModelMock->expects($this->any())->method('getIdBySku')->will($this->returnValue($id));
556+
$this->resourceModelMock->expects($this->any())->method('getIdBySku')->will($this->returnValue(100));
568557
$this->productFactoryMock->expects($this->any())
569558
->method('create')
570559
->will($this->returnValue($this->productMock));
@@ -577,20 +566,15 @@ public function testSaveExisting()
577566
->method('toNestedArray')
578567
->will($this->returnValue($this->productData));
579568
$this->productMock->expects($this->atLeastOnce())->method('getSku')->willReturn($this->productData['sku']);
580-
$this->productMock->expects($this->at(0))->method('getId')->willReturn(null);
581-
$this->productMock->expects($this->any())->method('getId')->willReturn($id);
582569

583570
$this->assertEquals($this->productMock, $this->model->save($this->productMock));
584571
}
585572

586573
public function testSaveNew()
587574
{
588-
$id = 100;
589575
$this->storeManagerMock->expects($this->any())->method('getWebsites')->willReturn([1 => 'default']);
590576
$this->resourceModelMock->expects($this->at(0))->method('getIdBySku')->will($this->returnValue(null));
591-
$this->resourceModelMock->expects($this->at(3))->method('getIdBySku')->will($this->returnValue($id));
592-
$this->productMock->expects($this->at(0))->method('getId')->willReturn(null);
593-
$this->productMock->expects($this->any())->method('getId')->willReturn($id);
577+
$this->resourceModelMock->expects($this->at(3))->method('getIdBySku')->will($this->returnValue(100));
594578
$this->productFactoryMock->expects($this->any())
595579
->method('create')
596580
->will($this->returnValue($this->productMock));
@@ -616,7 +600,7 @@ public function testSaveUnableToSaveException()
616600
$this->storeManagerMock->expects($this->any())->method('getWebsites')->willReturn([1 => 'default']);
617601
$this->resourceModelMock->expects($this->exactly(1))
618602
->method('getIdBySku')->willReturn(null);
619-
$this->productFactoryMock->expects($this->exactly(1))
603+
$this->productFactoryMock->expects($this->exactly(2))
620604
->method('create')
621605
->will($this->returnValue($this->productMock));
622606
$this->initializationHelperMock->expects($this->never())->method('initialize');
@@ -641,7 +625,7 @@ public function testSaveException()
641625
{
642626
$this->storeManagerMock->expects($this->any())->method('getWebsites')->willReturn([1 => 'default']);
643627
$this->resourceModelMock->expects($this->exactly(1))->method('getIdBySku')->will($this->returnValue(null));
644-
$this->productFactoryMock->expects($this->exactly(1))
628+
$this->productFactoryMock->expects($this->exactly(2))
645629
->method('create')
646630
->will($this->returnValue($this->productMock));
647631
$this->initializationHelperMock->expects($this->never())->method('initialize');
@@ -667,7 +651,7 @@ public function testSaveInvalidProductException()
667651
{
668652
$this->storeManagerMock->expects($this->any())->method('getWebsites')->willReturn([1 => 'default']);
669653
$this->resourceModelMock->expects($this->exactly(1))->method('getIdBySku')->will($this->returnValue(null));
670-
$this->productFactoryMock->expects($this->exactly(1))
654+
$this->productFactoryMock->expects($this->exactly(2))
671655
->method('create')
672656
->will($this->returnValue($this->productMock));
673657
$this->initializationHelperMock->expects($this->never())->method('initialize');
@@ -743,7 +727,6 @@ public function testDeleteById()
743727
->will($this->returnValue('42'));
744728
$this->productMock->expects($this->once())->method('load')->with('42');
745729
$this->productMock->expects($this->atLeastOnce())->method('getSku')->willReturn($sku);
746-
$this->productMock->expects($this->atLeastOnce())->method('getId')->willReturn(42);
747730
$this->assertTrue($this->model->deleteById($sku));
748731
}
749732

@@ -838,9 +821,8 @@ public function cacheKeyDataProvider()
838821
*/
839822
public function testSaveExistingWithOptions(array $newOptions, array $existingOptions, array $expectedData)
840823
{
841-
$id = 100;
842824
$this->storeManagerMock->expects($this->any())->method('getWebsites')->willReturn([1 => 'default']);
843-
$this->resourceModelMock->expects($this->any())->method('getIdBySku')->will($this->returnValue($id));
825+
$this->resourceModelMock->expects($this->any())->method('getIdBySku')->will($this->returnValue(100));
844826
$this->productFactoryMock->expects($this->any())
845827
->method('create')
846828
->will($this->returnValue($this->initializedProductMock));
@@ -859,8 +841,6 @@ public function testSaveExistingWithOptions(array $newOptions, array $existingOp
859841
$this->initializedProductMock->expects($this->atLeastOnce())
860842
->method('getSku')->willReturn($this->productData['sku']);
861843
$this->productMock->expects($this->atLeastOnce())->method('getSku')->willReturn($this->productData['sku']);
862-
$this->initializedProductMock->expects($this->at(0))->method('getId')->willReturn(null);
863-
$this->initializedProductMock->expects($this->any())->method('getId')->willReturn($id);
864844

865845
$this->assertEquals($this->initializedProductMock, $this->model->save($this->productMock));
866846
}
@@ -1017,7 +997,6 @@ public function testSaveWithLinks(array $newLinks, array $existingLinks, array $
1017997
$this->productFactoryMock->expects($this->any())
1018998
->method('create')
1019999
->will($this->returnValue($this->initializedProductMock));
1020-
$this->initializedProductMock->method('getId')->willReturn(100);
10211000
$this->initializationHelperMock->expects($this->never())->method('initialize');
10221001
$this->resourceModelMock->expects($this->once())->method('validate')->with($this->initializedProductMock)
10231002
->willReturn(true);
@@ -1280,8 +1259,6 @@ public function testSaveExistingWithNewMediaGalleryEntries()
12801259
$this->initializedProductMock->expects($this->atLeastOnce())
12811260
->method('getSku')->willReturn($this->productData['sku']);
12821261
$this->productMock->expects($this->atLeastOnce())->method('getSku')->willReturn($this->productData['sku']);
1283-
$this->initializedProductMock->expects($this->at(0))->method('getId')->willReturn(null);
1284-
$this->initializedProductMock->expects($this->any())->method('getId')->willReturn(100);
12851262

12861263
$this->model->save($this->productMock);
12871264
}
@@ -1324,8 +1301,6 @@ public function testSaveWithDifferentWebsites()
13241301
]);
13251302
$this->productMock->expects($this->once())->method('setWebsiteIds')->willReturn([2,3]);
13261303
$this->productMock->method('getSku')->willReturn('simple');
1327-
$this->productMock->expects($this->at(0))->method('getId')->willReturn(null);
1328-
$this->productMock->expects($this->any())->method('getId')->willReturn(100);
13291304

13301305
$this->assertEquals($this->productMock, $this->model->save($this->productMock));
13311306
}
@@ -1399,7 +1374,6 @@ public function testSaveExistingWithMediaGalleryEntries()
13991374
->method('getSku')->willReturn($this->productData['sku']);
14001375
$this->productMock->expects($this->atLeastOnce())->method('getSku')->willReturn($this->productData['sku']);
14011376
$this->productMock->expects($this->any())->method('getMediaGalleryEntries')->willReturn(null);
1402-
$this->initializedProductMock->expects($this->any())->method('getId')->willReturn(100);
14031377
$this->model->save($this->productMock);
14041378
$this->assertEquals($expectedResult, $this->initializedProductMock->getMediaGallery('images'));
14051379
}

0 commit comments

Comments
 (0)