Skip to content

Commit a5d7355

Browse files
Merge remote-tracking branch 'engcom-Charlie/7720' into 7720
# Conflicts: # dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php
2 parents 042df41 + 13af48a commit a5d7355

File tree

3 files changed

+131
-18
lines changed

3 files changed

+131
-18
lines changed

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
use Magento\Framework\Exception\ValidatorException;
3131

3232
/**
33-
* Product Repository.
33+
* @inheritdoc
34+
*
3435
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3536
* @SuppressWarnings(PHPMD.TooManyFields)
3637
*/
@@ -737,6 +738,7 @@ private function getCollectionProcessor()
737738
{
738739
if (!$this->collectionProcessor) {
739740
$this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
741+
// phpstan:ignore "Class Magento\Catalog\Model\Api\SearchCriteria\ProductCollectionProcessor not found."
740742
\Magento\Catalog\Model\Api\SearchCriteria\ProductCollectionProcessor::class
741743
);
742744
}

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

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,24 @@
4646
use PHPUnit_Framework_MockObject_MockObject as MockObject;
4747

4848
/**
49-
* Class ProductRepositoryTest
50-
* @package Magento\Catalog\Test\Unit\Model
49+
* Test for \Magento\Catalog\Model\ProductRepository.
50+
*
5151
* @SuppressWarnings(PHPMD.TooManyFields)
5252
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
5353
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
5454
*/
5555
class ProductRepositoryTest extends TestCase
5656
{
57+
private const STUB_STORE_ID = 1;
58+
59+
private const STUB_STORE_ID_GLOBAL = 0;
60+
61+
private const STUB_PRODUCT_ID = 100;
62+
63+
private const STUB_PRODUCT_NAME = 'name';
64+
65+
private const STUB_PRODUCT_SKU = 'sku';
66+
5767
/**
5868
* @var Product|MockObject
5969
*/
@@ -291,6 +301,7 @@ protected function setUp()
291301
->disableOriginalConstructor()
292302
->setMethods([])
293303
->getMockForAbstractClass();
304+
$storeMock->method('getId')->willReturn(self::STUB_STORE_ID);
294305
$storeMock->expects($this->any())->method('getWebsiteId')->willReturn('1');
295306
$storeMock->expects($this->any())->method('getCode')->willReturn(Store::ADMIN_CODE);
296307
$this->storeManager->expects($this->any())->method('getStore')->willReturn($storeMock);
@@ -345,6 +356,68 @@ function ($value) {
345356
$this->objectManager->setBackwardCompatibleProperty($this->model, 'mediaProcessor', $mediaProcessor);
346357
}
347358

359+
/**
360+
* Test save product with global store id
361+
*
362+
* @param array $productData
363+
* @return void
364+
* @dataProvider getProductData
365+
*/
366+
public function testSaveForAllStoreViewScope(array $productData): void
367+
{
368+
$this->productFactory->method('create')->willReturn($this->product);
369+
$this->product->method('getSku')->willReturn($productData['sku']);
370+
$this->extensibleDataObjectConverter
371+
->expects($this->once())
372+
->method('toNestedArray')
373+
->willReturn($productData);
374+
$this->resourceModel->method('getIdBySku')->willReturn(self::STUB_PRODUCT_ID);
375+
$this->resourceModel->expects($this->once())->method('validate')->willReturn(true);
376+
$this->product->expects($this->at(14))->method('setData')
377+
->with('store_id', $productData['store_id']);
378+
379+
$this->model->save($this->product);
380+
}
381+
382+
/**
383+
* Product data provider
384+
*
385+
* @return array
386+
*/
387+
public function getProductData(): array
388+
{
389+
return [
390+
[
391+
[
392+
'sku' => self::STUB_PRODUCT_SKU,
393+
'name' => self::STUB_PRODUCT_NAME,
394+
'store_id' => self::STUB_STORE_ID_GLOBAL,
395+
],
396+
],
397+
];
398+
}
399+
400+
/**
401+
* Test save product without store
402+
*
403+
* @return void
404+
*/
405+
public function testSaveWithoutStoreId(): void
406+
{
407+
$this->productFactory->method('create')->willReturn($this->product);
408+
$this->product->method('getSku')->willReturn($this->productData['sku']);
409+
$this->extensibleDataObjectConverter
410+
->expects($this->once())
411+
->method('toNestedArray')
412+
->willReturn($this->productData);
413+
$this->resourceModel->method('getIdBySku')->willReturn(self::STUB_PRODUCT_ID);
414+
$this->resourceModel->expects($this->once())->method('validate')->willReturn(true);
415+
$this->product->expects($this->at(15))->method('setData')
416+
->with('store_id', self::STUB_STORE_ID);
417+
418+
$this->model->save($this->product);
419+
}
420+
348421
/**
349422
* @expectedException \Magento\Framework\Exception\NoSuchEntityException
350423
* @expectedExceptionMessage The product that was requested doesn't exist. Verify the product and try again.

dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77

88
namespace Magento\Catalog\Model;
99

10-
use Magento\Backend\Model\Auth;
1110
use Magento\Catalog\Api\ProductRepositoryInterface;
1211
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
1312
use Magento\Framework\Api\SearchCriteriaBuilder;
1413
use Magento\Framework\Exception\LocalizedException;
1514
use Magento\TestFramework\Catalog\Model\ProductLayoutUpdateManager;
1615
use Magento\TestFramework\Helper\Bootstrap;
17-
use Magento\TestFramework\Bootstrap as TestBootstrap;
18-
use Magento\Framework\Acl\Builder;
1916

2017
/**
2118
* Provide tests for ProductRepository model.
@@ -26,6 +23,16 @@
2623
*/
2724
class ProductRepositoryTest extends \PHPUnit\Framework\TestCase
2825
{
26+
private const STUB_STORE_ID = 1;
27+
28+
private const STUB_STORE_ID_GLOBAL = 0;
29+
30+
private const STUB_PRODUCT_NAME = 'Simple Product';
31+
32+
private const STUB_UPDATED_PRODUCT_NAME = 'updated';
33+
34+
private const STUB_PRODUCT_SKU = 'simple';
35+
2936
/**
3037
* Test subject.
3138
*
@@ -235,22 +242,53 @@ public function testCustomLayout(): void
235242
}
236243

237244
/**
238-
* Tests product repository update should use provided store code.
245+
* Tests product repository update
239246
*
247+
* @dataProvider productUpdateDataProvider
240248
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
249+
* @param int $storeId
250+
* @param int $checkStoreId
251+
* @param string $expectedNameStore
252+
* @param string $expectedNameCheckedStore
241253
*/
242-
public function testProductUpdate(): void
243-
{
244-
$sku = 'simple';
245-
$nameUpdated = 'updated';
254+
public function testProductUpdate(
255+
int $storeId,
256+
int $checkStoreId,
257+
string $expectedNameStore,
258+
string $expectedNameCheckedStore
259+
): void {
260+
$sku = self::STUB_PRODUCT_SKU;
246261

247-
$product = $this->productRepository->get($sku, false, 0);
248-
$product->setName($nameUpdated);
262+
$product = $this->productRepository->get($sku, false, $storeId);
263+
$product->setName(self::STUB_UPDATED_PRODUCT_NAME);
249264
$this->productRepository->save($product);
250-
$product = $this->productRepository->get($sku, false, 0);
251-
self::assertEquals(
252-
$nameUpdated,
253-
$product->getName()
254-
);
265+
$productNameStoreId = $this->productRepository->get($sku, false, $storeId)->getName();
266+
$productNameCheckedStoreId = $this->productRepository->get($sku, false, $checkStoreId)->getName();
267+
268+
$this->assertEquals($expectedNameStore, $productNameStoreId);
269+
$this->assertEquals($expectedNameCheckedStore, $productNameCheckedStoreId);
270+
}
271+
272+
/**
273+
* Product update data provider
274+
*
275+
* @return array
276+
*/
277+
public function productUpdateDataProvider(): array
278+
{
279+
return [
280+
'Updating for global store' => [
281+
self::STUB_STORE_ID_GLOBAL,
282+
self::STUB_STORE_ID,
283+
self::STUB_UPDATED_PRODUCT_NAME,
284+
self::STUB_UPDATED_PRODUCT_NAME,
285+
],
286+
'Updating for store' => [
287+
self::STUB_STORE_ID,
288+
self::STUB_STORE_ID_GLOBAL,
289+
self::STUB_UPDATED_PRODUCT_NAME,
290+
self::STUB_PRODUCT_NAME,
291+
],
292+
];
255293
}
256294
}

0 commit comments

Comments
 (0)