|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Quote\Model\Quote\Item; |
| 9 | + |
| 10 | +use Magento\Framework\Exception\LocalizedException; |
| 11 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 12 | +use Magento\Framework\ObjectManagerInterface; |
| 13 | +use Magento\Framework\DataObject; |
| 14 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 15 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 16 | +use Magento\Catalog\Api\Data\ProductCustomOptionInterface; |
| 17 | +use Magento\Catalog\Model\Product\Option; |
| 18 | +use Magento\Quote\Model\Quote; |
| 19 | +use Magento\TestFramework\Helper\Bootstrap; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | + |
| 22 | +class CompareTest extends TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var Compare |
| 26 | + */ |
| 27 | + private $compare; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var ObjectManagerInterface |
| 31 | + */ |
| 32 | + private $objectManager; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var ProductRepositoryInterface |
| 36 | + */ |
| 37 | + private $productRepository; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var Quote |
| 41 | + */ |
| 42 | + private $quote1; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var Quote |
| 46 | + */ |
| 47 | + private $quote2; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var Quote |
| 51 | + */ |
| 52 | + private $quote3; |
| 53 | + |
| 54 | + /** |
| 55 | + * @inheritdoc |
| 56 | + */ |
| 57 | + protected function setUp(): void |
| 58 | + { |
| 59 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 60 | + $this->compare = $this->objectManager->create(Compare::class); |
| 61 | + $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class); |
| 62 | + $this->quote1 = $this->objectManager->create(Quote::class); |
| 63 | + $this->quote2 = $this->objectManager->create(Quote::class); |
| 64 | + $this->quote3 = $this->objectManager->create(Quote::class); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @magentoDataFixture Magento/Catalog/_files/product_simple_with_custom_options.php |
| 69 | + * @return void |
| 70 | + * @throws NoSuchEntityException |
| 71 | + * @throws LocalizedException |
| 72 | + */ |
| 73 | + public function testCompare(): void |
| 74 | + { |
| 75 | + $this->quote1->setReservedOrderId('test_order_item_with_custom_options1'); |
| 76 | + $this->quote2->setReservedOrderId('test_order_item_with_custom_options2'); |
| 77 | + $this->quote2->setReservedOrderId('test_order_item_with_custom_options3'); |
| 78 | + |
| 79 | + $product = $this->productRepository->get('simple_with_custom_options', true, null, true); |
| 80 | + |
| 81 | + $options = $this->selectOptions($product, 'test1'); |
| 82 | + $requestInfo = new DataObject(['qty' => 1, 'options' => $options]); |
| 83 | + $this->quote1->addProduct($product, $requestInfo); |
| 84 | + $items1 = $this->quote1->getAllItems(); |
| 85 | + |
| 86 | + $options = $this->selectOptions($product, 'test2'); |
| 87 | + $requestInfo = new DataObject(['qty' => 1, 'options' => $options]); |
| 88 | + $this->quote2->addProduct($product, $requestInfo); |
| 89 | + $items2 = $this->quote2->getAllItems(); |
| 90 | + |
| 91 | + $options = $this->selectOptions($product, 'test1'); |
| 92 | + $requestInfo = new DataObject(['qty' => 1, 'options' => $options]); |
| 93 | + $this->quote3->addProduct($product, $requestInfo); |
| 94 | + $items3 = $this->quote3->getAllItems(); |
| 95 | + |
| 96 | + $this->assertTrue($this->compare->compare($items1[0], $items1[0])); |
| 97 | + $this->assertTrue($this->compare->compare($items2[0], $items2[0])); |
| 98 | + $this->assertFalse($this->compare->compare($items1[0], $items2[0])); |
| 99 | + $this->assertTrue($this->compare->compare($items1[0], $items3[0])); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Make selection of custom options |
| 104 | + * |
| 105 | + * @param ProductInterface $product |
| 106 | + * @param string $textValue |
| 107 | + * @return array |
| 108 | + */ |
| 109 | + private function selectOptions(ProductInterface $product, string $textValue): array |
| 110 | + { |
| 111 | + $options = []; |
| 112 | + /** @var $option Option */ |
| 113 | + foreach ($product->getOptions() as $option) { |
| 114 | + $value = match ($option->getGroupByType()) { |
| 115 | + ProductCustomOptionInterface::OPTION_GROUP_SELECT => key($option->getValues()), |
| 116 | + default => $textValue, |
| 117 | + }; |
| 118 | + $options[$option->getId()] = $value; |
| 119 | + } |
| 120 | + return $options; |
| 121 | + } |
| 122 | +} |
0 commit comments