Skip to content

Commit

Permalink
[17] Read API :: Integration tests :: Customer* magento#55
Browse files Browse the repository at this point in the history
  • Loading branch information
skovalenk committed Jun 10, 2020
1 parent 513f692 commit 19be0cd
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use Magento\Catalog\Api\Data\ProductTierPriceExtensionFactory;
use Magento\Catalog\Api\Data\ProductExtensionInterfaceFactory;

\Magento\TestFramework\Helper\Bootstrap::getInstance()->reinitialize();

/** @var \Magento\TestFramework\ObjectManager $objectManager */
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

/** @var \Magento\Catalog\Api\CategoryLinkManagementInterface $categoryLinkManagement */
$categoryLinkManagement = $objectManager->get(\Magento\Catalog\Api\CategoryLinkManagementInterface::class);

$tierPrices = [];
/** @var \Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $tierPriceFactory */
$tierPriceFactory = $objectManager->get(\Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory::class);
/** @var $tpExtensionAttributes */
$tpExtensionAttributesFactory = $objectManager->get(ProductTierPriceExtensionFactory::class);
/** @var $productExtensionAttributes */
$productExtensionAttributesFactory = $objectManager->get(ProductExtensionInterfaceFactory::class);

$adminWebsite = $objectManager->get(\Magento\Store\Api\WebsiteRepositoryInterface::class)->get('admin');
$tierPriceExtensionAttributes1 = $tpExtensionAttributesFactory->create()
->setWebsiteId($adminWebsite->getId());
$productExtensionAttributesWebsiteIds = $productExtensionAttributesFactory->create(
['website_ids' => $adminWebsite->getId()]
);

$tierPrices[] = $tierPriceFactory->create(
[
'data' => [
'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL,
'qty' => 2,
'value' => 8
]
]
)->setExtensionAttributes($tierPriceExtensionAttributes1);

$tierPrices[] = $tierPriceFactory->create(
[
'data' => [
'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL,
'qty' => 5,
'value' => 5
]
]
)->setExtensionAttributes($tierPriceExtensionAttributes1);

$tierPrices[] = $tierPriceFactory->create(
[
'data' => [
'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID,
'qty' => 3,
'value' => 5
]
]
)->setExtensionAttributes($tierPriceExtensionAttributes1);

$tierPrices[] = $tierPriceFactory->create(
[
'data' => [
'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID,
'qty' => 3.2,
'value' => 6,
]
]
)->setExtensionAttributes($tierPriceExtensionAttributes1);

$tierPriceExtensionAttributes2 = $tpExtensionAttributesFactory->create()
->setWebsiteId($adminWebsite->getId())
->setPercentageValue(50);

$tierPrices[] = $tierPriceFactory->create(
[
'data' => [
'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID,
'qty' => 10
]
]
)->setExtensionAttributes($tierPriceExtensionAttributes2);

/** @var $product \Magento\Catalog\Model\Product */
$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
$product->isObjectNew(true);
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
->setId(1)
->setAttributeSetId(4)
->setWebsiteIds([1])
->setName('Simple Product')
->setSku('simple')
->setPrice(10)
->setWeight(1)
->setShortDescription("Short description")
->setTaxClassId(0)
->setTierPrices($tierPrices)
->setDescription('Description with <b>html tag</b>')
->setExtensionAttributes($productExtensionAttributesWebsiteIds)
->setMetaTitle('meta title')
->setMetaKeyword('meta keyword')
->setMetaDescription('meta description')
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED)
->setStockData(
[
'use_config_manage_stock' => 1,
'qty' => 100,
'is_qty_decimal' => 0,
'is_in_stock' => 1,
]
)->setCanSaveCustomOptions(true)
->setHasOptions(true);

$oldOptions = [
[
'previous_group' => 'text',
'title' => 'Test Field',
'type' => 'field',
'is_require' => 1,
'sort_order' => 0,
'price' => 1,
'price_type' => 'fixed',
'sku' => '1-text',
'max_characters' => 100,
],
[
'previous_group' => 'date',
'title' => 'Test Date and Time',
'type' => 'date_time',
'is_require' => 1,
'sort_order' => 0,
'price' => 2,
'price_type' => 'fixed',
'sku' => '2-date',
],
[
'previous_group' => 'select',
'title' => 'Test Select',
'type' => 'drop_down',
'is_require' => 1,
'sort_order' => 0,
'values' => [
[
'option_type_id' => null,
'title' => 'Option 1',
'price' => 3,
'price_type' => 'fixed',
'sku' => '3-1-select',
],
[
'option_type_id' => null,
'title' => 'Option 2',
'price' => 3,
'price_type' => 'fixed',
'sku' => '3-2-select',
],
]
],
[
'previous_group' => 'select',
'title' => 'Test Radio',
'type' => 'radio',
'is_require' => 1,
'sort_order' => 0,
'values' => [
[
'option_type_id' => null,
'title' => 'Option 1',
'price' => 3,
'price_type' => 'fixed',
'sku' => '4-1-radio',
],
[
'option_type_id' => null,
'title' => 'Option 2',
'price' => 3,
'price_type' => 'fixed',
'sku' => '4-2-radio',
],
]
]
];

$options = [];

/** @var \Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory $customOptionFactory */
$customOptionFactory = $objectManager->create(\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory::class);

foreach ($oldOptions as $option) {
/** @var \Magento\Catalog\Api\Data\ProductCustomOptionInterface $option */
$option = $customOptionFactory->create(['data' => $option]);
$option->setProductSku($product->getSku());

$options[] = $option;
}

$product->setOptions($options);

/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
$productRepository->save($product);

$categoryLinkManagement->assignProductToCategories(
$product->getSku(),
[2]
);

/**
* We need to remember that automatic reindexation is not working properly in integration tests
* Reindexation is sitting on top of afterCommit callbacks:
* \Magento\Catalog\Model\Product::priceReindexCallback
*
* However, callbacks are applied only when transaction_level = 0 (when transaction is commited), however
* integration tests are not committing transactions, so we need to reindex data manually in order to reuse it in tests
*/
/** @var \Magento\Indexer\Model\Indexer $indexer */
$indexer = $objectManager->create(\Magento\Indexer\Model\Indexer::class);
$indexer->load('catalog_product_price');
$indexer->reindexRow($product->getId());
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,17 @@
$product->getSku(),
[2]
);


/**
* We need to remember that automatic reindexation is not working properly in integration tests
* Reindexation is sitting on top of afterCommit callbacks:
* \Magento\Catalog\Model\Product::priceReindexCallback
*
* However, callbacks are applied only when transaction_level = 0 (when transaction is commited), however
* integration tests are not committing transactions, so we need to reindex data manually in order to reuse it in tests
*/
/** @var \Magento\Indexer\Model\Indexer $indexer */
$indexer = $objectManager->create(\Magento\Indexer\Model\Indexer::class);
$indexer->load('catalog_product_price');
$indexer->reindexRow($product->getId());
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
$attributeValues = [];
$attributeSetId = $installer->getAttributeSetId(Product::ENTITY, 'Default');
$associatedProductIds = [];
$productIds = [10, 20];
$idsToReindex = $productIds = [10, 20];
array_shift($options); //remove the first option which is empty

foreach ($options as $option) {
Expand Down Expand Up @@ -142,3 +142,17 @@
$product->getSku(),
[2]
);

/**
* We need to remember that automatic reindexation is not working properly in integration tests
* Reindexation is sitting on top of afterCommit callbacks:
* \Magento\Catalog\Model\Product::priceReindexCallback
*
* However, callbacks are applied only when transaction_level = 0 (when transaction is commited), however
* integration tests are not committing transactions, so we need to reindex data manually in order to reuse it in tests
*/
/** @var \Magento\Indexer\Model\Indexer $indexer */
$indexer = Bootstrap::getObjectManager()->create(\Magento\Indexer\Model\Indexer::class);
$indexer->load('catalog_product_price');
$indexer->reindexRow($product->getId());
$indexer->reindexList($idsToReindex);
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
$attributeValues = [];
$attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
$associatedProductIds = [];
$productIds = [10, 20];
$idsToReindex = $productIds = [10, 20];
array_shift($options); //remove the first option which is empty

foreach ($options as $option) {
Expand Down Expand Up @@ -142,3 +142,16 @@
$product->getSku(),
[2]
);

/**
* We need to remember that automatic reindexation is not working properly in integration tests
* Reindexation is sitting on top of afterCommit callbacks:
* \Magento\Catalog\Model\Product::priceReindexCallback
*
* However, callbacks are applied only when transaction_level = 0 (when transaction is commited), however
* integration tests are not committing transactions, so we need to reindex data manually in order to reuse it in tests
*/
/** @var \Magento\Indexer\Model\Indexer $indexer */
$indexer = Bootstrap::getObjectManager()->create(\Magento\Indexer\Model\Indexer::class);
$indexer->load('catalog_product_price');
$indexer->reindexList($idsToReindex);
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,13 @@ public function testGetItemCollection(): void
}

/**
* @magentoDataFixture Magento/Wishlist/_files/wishlist.php
* @magentoDataFixture Magento/Wishlist/_files/wishlist_with_disabled_simple_product.php
*
* @return void
*/
public function testGetItemCollectionWithDisabledProduct(): void
{
$productSku = 'simple';
$customerId = 1;
$product = $this->productRepository->get($productSku);
$product->setStatus(ProductStatus::STATUS_DISABLED);
$this->productRepository->save($product);
$this->assertEmpty($this->getWishlistByCustomerId->execute($customerId)->getItemCollection()->getItems());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Customer\Model\CustomerRegistry;
use Magento\Framework\DataObject;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
use Magento\Wishlist\Model\Wishlist;

Resolver::getInstance()->requireDataFixture('Magento/Customer/_files/customer.php');
Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/product_simple_disabled.php');

$objectManager = Bootstrap::getObjectManager();
/** @var CustomerRegistry $customerRegistry */
$customerRegistry = Bootstrap::getObjectManager()->create(CustomerRegistry::class);
$customer = $customerRegistry->retrieve(1);
/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
$product = $productRepository->get('simple');
$wishlist = Bootstrap::getObjectManager()->create(Wishlist::class);
$wishlist->loadByCustomerId($customer->getId(), true);
$item = $wishlist->addNewItem($product, new DataObject([]));
$wishlist->setSharingCode('fixture_unique_code')->save();

0 comments on commit 19be0cd

Please sign in to comment.