Skip to content

Commit 124f8cd

Browse files
MC-30646: Refactor CatalogRule integration test
1 parent 27076ca commit 124f8cd

File tree

7 files changed

+180
-149
lines changed

7 files changed

+180
-149
lines changed

dev/tests/integration/testsuite/Magento/CatalogRule/Model/Indexer/Product/PriceTest.php

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,49 @@
88
use Magento\Catalog\Api\ProductRepositoryInterface;
99
use Magento\Catalog\Model\ProductRepository;
1010
use Magento\Catalog\Model\ResourceModel\Product\Collection;
11+
use Magento\CatalogRule\Model\Indexer\IndexBuilder;
1112
use Magento\CatalogRule\Model\ResourceModel\Rule;
1213
use Magento\Framework\Api\SearchCriteriaInterface;
1314
use Magento\Framework\Api\SortOrder;
15+
use Magento\Store\Api\WebsiteRepositoryInterface;
1416
use Magento\TestFramework\Helper\Bootstrap;
1517

1618
class PriceTest extends \PHPUnit\Framework\TestCase
1719
{
20+
/**
21+
* @var \Magento\Framework\ObjectManagerInterface
22+
*/
23+
private $objectManager;
1824
/**
1925
* @var Rule
2026
*/
2127
private $resourceRule;
2228

29+
/**
30+
* @var WebsiteRepositoryInterface
31+
*/
32+
private $websiteRepository;
33+
34+
/**
35+
* @var ProductRepository
36+
*/
37+
private $productRepository;
38+
39+
/**
40+
* @var IndexBuilder
41+
*/
42+
private $indexerBuilder;
43+
44+
/**
45+
* @inheritdoc
46+
*/
2347
protected function setUp()
2448
{
25-
$this->resourceRule = Bootstrap::getObjectManager()->get(Rule::class);
49+
$this->objectManager = Bootstrap::getObjectManager();
50+
$this->resourceRule = $this->objectManager->get(Rule::class);
51+
$this->websiteRepository = $this->objectManager->get(WebsiteRepositoryInterface::class);
52+
$this->productRepository = $this->objectManager->create(ProductRepository::class);
53+
$this->indexerBuilder = $this->objectManager->get(IndexBuilder::class);
2654
}
2755

2856
/**
@@ -58,41 +86,23 @@ public function testPriceApplying()
5886
}
5987

6088
/**
61-
* @magentoDataFixtureBeforeTransaction Magento/CatalogRule/_files/simple_products.php
62-
* @magentoDataFixtureBeforeTransaction Magento/CatalogRule/_files/catalog_rule_50_percent_off.php
89+
* @magentoDataFixtureBeforeTransaction Magento/CatalogRule/_files/simple_product_with_catalog_rule_50_percent_off.php
6390
* @magentoDbIsolation enabled
6491
* @magentoAppIsolation enabled
92+
* @return void
6593
*/
66-
public function testPriceForSecondStore()
94+
public function testPriceForSecondStore():void
6795
{
68-
$customerGroupId = 1;
69-
$websiteId = 2;
70-
/** @var ProductRepository $productRepository */
71-
$productRepository = Bootstrap::getObjectManager()->create(
72-
ProductRepository::class
73-
);
74-
$simpleProduct = $productRepository->get('simple3');
96+
$websiteId = $this->websiteRepository->get('test')->getId();
97+
$simpleProduct = $this->productRepository->get('simple');
7598
$simpleProduct->setPriceCalculation(true);
76-
$this->assertEquals('simple3', $simpleProduct->getSku());
99+
$this->assertEquals('simple', $simpleProduct->getSku());
77100
$this->assertFalse(
78-
$this->resourceRule->getRulePrice(
79-
new \DateTime(),
80-
$websiteId,
81-
$customerGroupId,
82-
$simpleProduct->getId()
83-
)
84-
);
85-
$indexerBuilder = Bootstrap::getObjectManager()->get(
86-
\Magento\CatalogRule\Model\Indexer\IndexBuilder::class
101+
$this->resourceRule->getRulePrice(new \DateTime(), $websiteId, 1, $simpleProduct->getId())
87102
);
88-
$indexerBuilder->reindexById($simpleProduct->getId());
103+
$this->indexerBuilder->reindexById($simpleProduct->getId());
89104
$this->assertEquals(
90-
$this->resourceRule->getRulePrice(
91-
new \DateTime(),
92-
$websiteId,
93-
$customerGroupId,
94-
$simpleProduct->getId()
95-
),
105+
$this->resourceRule->getRulePrice(new \DateTime(), $websiteId, 1, $simpleProduct->getId()),
96106
25
97107
);
98108
}

dev/tests/integration/testsuite/Magento/CatalogRule/_files/catalog_rule_50_percent_off.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

dev/tests/integration/testsuite/Magento/CatalogRule/_files/catalog_rule_50_percent_off_rollback.php

Lines changed: 0 additions & 28 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Catalog\Api\Data\ProductInterfaceFactory;
9+
use Magento\Catalog\Api\ProductRepositoryInterface;
10+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
11+
use Magento\Catalog\Model\Product\Visibility;
12+
use Magento\CatalogRule\Api\CatalogRuleRepositoryInterface;
13+
use Magento\CatalogRule\Model\Indexer\IndexBuilder;
14+
use Magento\CatalogRule\Model\Rule;
15+
use Magento\CatalogRule\Model\RuleFactory;
16+
use Magento\Customer\Model\Group;
17+
use Magento\Store\Api\WebsiteRepositoryInterface;
18+
use Magento\TestFramework\Helper\Bootstrap;
19+
20+
require __DIR__ . '/../../Store/_files/second_website_with_two_stores.php';
21+
22+
$objectManager = Bootstrap::getObjectManager();
23+
/** @var WebsiteRepositoryInterface $websiteRepository */
24+
$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class);
25+
/** @var IndexBuilder $indexBuilder */
26+
$indexBuilder = $objectManager->get(IndexBuilder::class);
27+
/** @var CatalogRuleRepositoryInterface $catalogRuleRepository */
28+
$catalogRuleRepository = $objectManager->get(CatalogRuleRepositoryInterface::class);
29+
/** @var ProductInterfaceFactory $productFactory */
30+
$productFactory = $objectManager->get(ProductInterfaceFactory::class);
31+
/** @var ProductRepositoryInterface $productRepository */
32+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
33+
/** @var RuleFactory $ruleFactory */
34+
$ruleFactory = $objectManager->get(RuleFactory::class);
35+
36+
$secondWebsite = $websiteRepository->get('test');
37+
$product = $productFactory->create();
38+
$product->setTypeId('simple')
39+
->setAttributeSetId($product->getDefaultAttributeSetId())
40+
->setWebsiteIds([$secondWebsite->getId()])
41+
->setName('Simple Product')
42+
->setSku('simple')
43+
->setPrice(50)
44+
->setVisibility(Visibility::VISIBILITY_BOTH)
45+
->setStatus(Status::STATUS_ENABLED)
46+
->setStockData(
47+
[
48+
'use_config_manage_stock' => 1,
49+
'qty' => 100,
50+
'is_qty_decimal' => 0,
51+
'is_in_stock' => 1,
52+
]
53+
);
54+
$productRepository->save($product);
55+
/** @var Rule $rule */
56+
$catalogRule = $ruleFactory->create();
57+
$catalogRule->loadPost(
58+
[
59+
'name' => 'Test Catalog Rule 50% off',
60+
'is_active' => '1',
61+
'stop_rules_processing' => 0,
62+
'website_ids' => [$secondWebsite->getId()],
63+
'customer_group_ids' => [Group::NOT_LOGGED_IN_ID, 1],
64+
'discount_amount' => 50,
65+
'simple_action' => 'by_percent',
66+
'from_date' => '',
67+
'to_date' => '',
68+
'sort_order' => 0,
69+
'sub_is_enable' => 0,
70+
'sub_discount_amount' => 0,
71+
'conditions' => [],
72+
]
73+
);
74+
$catalogRuleRepository->save($catalogRule);
75+
$indexBuilder->reindexFull();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\CatalogRule\Api\CatalogRuleRepositoryInterface;
10+
use Magento\CatalogRule\Model\Indexer\IndexBuilder;
11+
use Magento\CatalogRule\Model\ResourceModel\Rule;
12+
use Magento\Framework\Exception\CouldNotDeleteException;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Framework\Registry;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
17+
$objectManager = Bootstrap::getObjectManager();
18+
/** @var Registry $registry */
19+
$registry = $objectManager->get(Registry::class);
20+
/** @var ProductRepositoryInterface $productRepository */
21+
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
22+
/** @var CatalogRuleRepositoryInterface $ruleRepository */
23+
$ruleRepository = $objectManager->get(CatalogRuleRepositoryInterface::class);
24+
/** @var IndexBuilder $indexBuilder */
25+
$indexBuilder = $objectManager->get(IndexBuilder::class);
26+
27+
$registry->unregister('isSecureArea');
28+
$registry->register('isSecureArea', true);
29+
try {
30+
$productRepository->deleteById('simple');
31+
} catch (NoSuchEntityException $e) {
32+
//already removed
33+
}
34+
$registry->unregister('isSecureArea');
35+
$registry->register('isSecureArea', false);
36+
37+
/** @var Rule $catalogRuleResource */
38+
$catalogRuleResource = $objectManager->create(Rule::class);
39+
//Retrieve rule by name
40+
$select = $catalogRuleResource->getConnection()
41+
->select()
42+
->from($catalogRuleResource->getMainTable(), 'rule_id')
43+
->where('name = ?', 'Test Catalog Rule 50% off');
44+
$ruleId = $catalogRuleResource->getConnection()->fetchOne($select);
45+
46+
try {
47+
$ruleRepository->deleteById($ruleId);
48+
} catch (CouldNotDeleteException $ex) {
49+
//Nothing to remove
50+
}
51+
52+
$indexBuilder->reindexFull();
53+
54+
require __DIR__ . '/../../Store/_files/second_website_with_two_stores_rollback.php';

dev/tests/integration/testsuite/Magento/CatalogRule/_files/simple_products.php

Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@
2626
->setPrice(10)
2727
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
2828
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
29-
->setStockData(
30-
[
31-
'use_config_manage_stock' => 1,
32-
'qty' => 100,
33-
'is_qty_decimal' => 0,
34-
'is_in_stock' => 1,
35-
]
36-
);
29+
->setStockData([
30+
'use_config_manage_stock' => 1,
31+
'qty' => 100,
32+
'is_qty_decimal' => 0,
33+
'is_in_stock' => 1,
34+
]);
3735
$productRepository->save($product);
3836
$productAction = $objectManager->get(\Magento\Catalog\Model\Product\Action::class);
3937
$productAction->updateAttributes([$product->getId()], ['test_attribute' => 'test_attribute_value'], $store->getId());
@@ -48,52 +46,10 @@
4846
->setPrice(9.9)
4947
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
5048
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
51-
->setStockData(
52-
[
53-
'use_config_manage_stock' => 1,
54-
'qty' => 100,
55-
'is_qty_decimal' => 0,
56-
'is_in_stock' => 1,
57-
]
58-
);
49+
->setStockData([
50+
'use_config_manage_stock' => 1,
51+
'qty' => 100,
52+
'is_qty_decimal' => 0,
53+
'is_in_stock' => 1,
54+
]);
5955
$productRepository->save($product);
60-
$store = $objectManager->create(\Magento\Store\Model\Store::class);
61-
$store->load('second_store_view', 'code');
62-
/**
63-
* @var Website $website
64-
*/
65-
$website2 = $objectManager->get(\Magento\Store\Model\Website::class);
66-
$website2->load('second_website', 'code');
67-
if (!$website2->getId()) {
68-
/** @var \Magento\Store\Model\Website $website */
69-
$website2->setData(
70-
[
71-
'code' => 'second_website',
72-
'name' => 'Second Website',
73-
74-
]
75-
);
76-
77-
$website2->save();
78-
}
79-
$product = $objectManager->create(\Magento\Catalog\Model\Product::class)
80-
->setTypeId('simple')
81-
->setId(3)
82-
->setAttributeSetId($attributeSetId)
83-
->setWebsiteIds([$website2->getId()])
84-
->setName('Simple Product 3')
85-
->setSku('simple3')
86-
->setPrice(50)
87-
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
88-
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
89-
->setStockData(
90-
[
91-
'use_config_manage_stock' => 1,
92-
'qty' => 100,
93-
'is_qty_decimal' => 0,
94-
'is_in_stock' => 1,
95-
]
96-
);
97-
$productRepository->save($product);
98-
$productAction = $objectManager->get(\Magento\Catalog\Model\Product\Action::class);
99-
$productAction->updateAttributes([$product->getId()], ['test_attribute' => 'test_attribute_value'], $store->getId());

dev/tests/integration/testsuite/Magento/CatalogRule/_files/simple_products_rollback.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
2020
$productRepository = $objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
21-
foreach (['simple1', 'simple2','simple3'] as $sku) {
21+
foreach (['simple1', 'simple2'] as $sku) {
2222
try {
2323
$product = $productRepository->get($sku, false, null, true);
2424
$productRepository->delete($product);

0 commit comments

Comments
 (0)