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