|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\SalesRule\Model\Rule\Condition\Product; |
| 8 | + |
| 9 | +use Magento\Framework\ObjectManagerInterface; |
| 10 | +use Magento\Quote\Model\Quote; |
| 11 | +use Magento\SalesRule\Model\Rule\Condition\Product as SalesRuleProduct; |
| 12 | +use Magento\TestFramework\Helper\Bootstrap; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +/** |
| 16 | + * Integration test for Subselect::validate() method returning true. |
| 17 | + * |
| 18 | + * @magentoAppArea frontend |
| 19 | + * @magentoDbIsolation enabled |
| 20 | + */ |
| 21 | +class SubselectTest extends TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var ObjectManagerInterface |
| 25 | + */ |
| 26 | + private $objectManager; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var Subselect |
| 30 | + */ |
| 31 | + private $subselectCondition; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var SalesRuleProduct |
| 35 | + */ |
| 36 | + private $productCondition; |
| 37 | + |
| 38 | + protected function setUp(): void |
| 39 | + { |
| 40 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 41 | + $this->productCondition = $this->objectManager->create(SalesRuleProduct::class); |
| 42 | + $this->subselectCondition = $this->objectManager->create( |
| 43 | + Subselect::class, |
| 44 | + [ |
| 45 | + 'context' => $this->objectManager->get(\Magento\Rule\Model\Condition\Context::class), |
| 46 | + 'ruleConditionProduct' => $this->productCondition |
| 47 | + ] |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Test validate() method returning true for total quantity >= 2 with single shipping. |
| 53 | + * |
| 54 | + * @magentoDataFixture Magento/Catalog/_files/products.php |
| 55 | + */ |
| 56 | + public function testValidateReturnsTrueForSufficientQuantity() |
| 57 | + { |
| 58 | + $this->subselectCondition->setData([ |
| 59 | + 'attribute' => 'qty', |
| 60 | + 'operator' => '>=', |
| 61 | + 'value' => 2, |
| 62 | + 'aggregator' => 'all', |
| 63 | + ]); |
| 64 | + $productCondition = $this->objectManager->create(SalesRuleProduct::class); |
| 65 | + $productCondition->setData([ |
| 66 | + 'attribute' => 'sku', |
| 67 | + 'operator' => '==', |
| 68 | + 'value' => 'simple', |
| 69 | + ]); |
| 70 | + $this->subselectCondition->setConditions([$productCondition]); |
| 71 | + $productRepository = $this->objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); |
| 72 | + $product = $productRepository->get('simple'); |
| 73 | + $product->setPrice(100.50); |
| 74 | + $this->assertNotNull($product->getId()); |
| 75 | + $quote = $this->objectManager->create(Quote::class); |
| 76 | + $quote->setStoreId(1) |
| 77 | + ->setIsActive(true) |
| 78 | + ->setIsMultiShipping(false) |
| 79 | + ->setReservedOrderId('test_quote') |
| 80 | + ->addProduct($product, 2); |
| 81 | + $quote->collectTotals()->save(); |
| 82 | + $this->assertNotNull($quote->getId()); |
| 83 | + $this->assertNotEmpty($quote->getAllVisibleItems()); |
| 84 | + $quoteItem = $quote->getAllVisibleItems()[0]; |
| 85 | + $this->assertNotNull($quoteItem); |
| 86 | + $this->assertNotNull($quoteItem->getProduct()); |
| 87 | + $this->assertEquals(2, $quoteItem->getQty()); |
| 88 | + $this->assertNotNull($quoteItem->getQuote()); |
| 89 | + $result = $this->subselectCondition->validate($quoteItem); |
| 90 | + $this->assertTrue($result); |
| 91 | + } |
| 92 | +} |
0 commit comments