Skip to content

Commit

Permalink
Merge remote-tracking branch 'engcom-Charlie/7720' into 7720
Browse files Browse the repository at this point in the history
# Conflicts:
#	dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php
  • Loading branch information
engcom-Charlie committed Jan 27, 2020
2 parents 042df41 + 13af48a commit a5d7355
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 18 deletions.
4 changes: 3 additions & 1 deletion app/code/Magento/Catalog/Model/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
use Magento\Framework\Exception\ValidatorException;

/**
* Product Repository.
* @inheritdoc
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyFields)
*/
Expand Down Expand Up @@ -737,6 +738,7 @@ private function getCollectionProcessor()
{
if (!$this->collectionProcessor) {
$this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
// phpstan:ignore "Class Magento\Catalog\Model\Api\SearchCriteria\ProductCollectionProcessor not found."
\Magento\Catalog\Model\Api\SearchCriteria\ProductCollectionProcessor::class
);
}
Expand Down
77 changes: 75 additions & 2 deletions app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,24 @@
use PHPUnit_Framework_MockObject_MockObject as MockObject;

/**
* Class ProductRepositoryTest
* @package Magento\Catalog\Test\Unit\Model
* Test for \Magento\Catalog\Model\ProductRepository.
*
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
class ProductRepositoryTest extends TestCase
{
private const STUB_STORE_ID = 1;

private const STUB_STORE_ID_GLOBAL = 0;

private const STUB_PRODUCT_ID = 100;

private const STUB_PRODUCT_NAME = 'name';

private const STUB_PRODUCT_SKU = 'sku';

/**
* @var Product|MockObject
*/
Expand Down Expand Up @@ -291,6 +301,7 @@ protected function setUp()
->disableOriginalConstructor()
->setMethods([])
->getMockForAbstractClass();
$storeMock->method('getId')->willReturn(self::STUB_STORE_ID);
$storeMock->expects($this->any())->method('getWebsiteId')->willReturn('1');
$storeMock->expects($this->any())->method('getCode')->willReturn(Store::ADMIN_CODE);
$this->storeManager->expects($this->any())->method('getStore')->willReturn($storeMock);
Expand Down Expand Up @@ -345,6 +356,68 @@ function ($value) {
$this->objectManager->setBackwardCompatibleProperty($this->model, 'mediaProcessor', $mediaProcessor);
}

/**
* Test save product with global store id
*
* @param array $productData
* @return void
* @dataProvider getProductData
*/
public function testSaveForAllStoreViewScope(array $productData): void
{
$this->productFactory->method('create')->willReturn($this->product);
$this->product->method('getSku')->willReturn($productData['sku']);
$this->extensibleDataObjectConverter
->expects($this->once())
->method('toNestedArray')
->willReturn($productData);
$this->resourceModel->method('getIdBySku')->willReturn(self::STUB_PRODUCT_ID);
$this->resourceModel->expects($this->once())->method('validate')->willReturn(true);
$this->product->expects($this->at(14))->method('setData')
->with('store_id', $productData['store_id']);

$this->model->save($this->product);
}

/**
* Product data provider
*
* @return array
*/
public function getProductData(): array
{
return [
[
[
'sku' => self::STUB_PRODUCT_SKU,
'name' => self::STUB_PRODUCT_NAME,
'store_id' => self::STUB_STORE_ID_GLOBAL,
],
],
];
}

/**
* Test save product without store
*
* @return void
*/
public function testSaveWithoutStoreId(): void
{
$this->productFactory->method('create')->willReturn($this->product);
$this->product->method('getSku')->willReturn($this->productData['sku']);
$this->extensibleDataObjectConverter
->expects($this->once())
->method('toNestedArray')
->willReturn($this->productData);
$this->resourceModel->method('getIdBySku')->willReturn(self::STUB_PRODUCT_ID);
$this->resourceModel->expects($this->once())->method('validate')->willReturn(true);
$this->product->expects($this->at(15))->method('setData')
->with('store_id', self::STUB_STORE_ID);

$this->model->save($this->product);
}

/**
* @expectedException \Magento\Framework\Exception\NoSuchEntityException
* @expectedExceptionMessage The product that was requested doesn't exist. Verify the product and try again.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@

namespace Magento\Catalog\Model;

use Magento\Backend\Model\Auth;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Exception\LocalizedException;
use Magento\TestFramework\Catalog\Model\ProductLayoutUpdateManager;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Bootstrap as TestBootstrap;
use Magento\Framework\Acl\Builder;

/**
* Provide tests for ProductRepository model.
Expand All @@ -26,6 +23,16 @@
*/
class ProductRepositoryTest extends \PHPUnit\Framework\TestCase
{
private const STUB_STORE_ID = 1;

private const STUB_STORE_ID_GLOBAL = 0;

private const STUB_PRODUCT_NAME = 'Simple Product';

private const STUB_UPDATED_PRODUCT_NAME = 'updated';

private const STUB_PRODUCT_SKU = 'simple';

/**
* Test subject.
*
Expand Down Expand Up @@ -235,22 +242,53 @@ public function testCustomLayout(): void
}

/**
* Tests product repository update should use provided store code.
* Tests product repository update
*
* @dataProvider productUpdateDataProvider
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @param int $storeId
* @param int $checkStoreId
* @param string $expectedNameStore
* @param string $expectedNameCheckedStore
*/
public function testProductUpdate(): void
{
$sku = 'simple';
$nameUpdated = 'updated';
public function testProductUpdate(
int $storeId,
int $checkStoreId,
string $expectedNameStore,
string $expectedNameCheckedStore
): void {
$sku = self::STUB_PRODUCT_SKU;

$product = $this->productRepository->get($sku, false, 0);
$product->setName($nameUpdated);
$product = $this->productRepository->get($sku, false, $storeId);
$product->setName(self::STUB_UPDATED_PRODUCT_NAME);
$this->productRepository->save($product);
$product = $this->productRepository->get($sku, false, 0);
self::assertEquals(
$nameUpdated,
$product->getName()
);
$productNameStoreId = $this->productRepository->get($sku, false, $storeId)->getName();
$productNameCheckedStoreId = $this->productRepository->get($sku, false, $checkStoreId)->getName();

$this->assertEquals($expectedNameStore, $productNameStoreId);
$this->assertEquals($expectedNameCheckedStore, $productNameCheckedStoreId);
}

/**
* Product update data provider
*
* @return array
*/
public function productUpdateDataProvider(): array
{
return [
'Updating for global store' => [
self::STUB_STORE_ID_GLOBAL,
self::STUB_STORE_ID,
self::STUB_UPDATED_PRODUCT_NAME,
self::STUB_UPDATED_PRODUCT_NAME,
],
'Updating for store' => [
self::STUB_STORE_ID,
self::STUB_STORE_ID_GLOBAL,
self::STUB_UPDATED_PRODUCT_NAME,
self::STUB_PRODUCT_NAME,
],
];
}
}

0 comments on commit a5d7355

Please sign in to comment.