|
| 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 | +namespace Magento\Bundle\Block\Catalog\Product\View\Type; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Framework\ObjectManagerInterface; |
| 12 | +use Magento\Framework\Registry; |
| 13 | +use Magento\Framework\Serialize\SerializerInterface; |
| 14 | +use Magento\Framework\View\LayoutInterface; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +/** |
| 19 | + * Check bundle product prices. |
| 20 | + * |
| 21 | + * @magentoDbIsolation enabled |
| 22 | + * @magentoAppIsolation disabled |
| 23 | + * @magentoAppArea frontend |
| 24 | + */ |
| 25 | +class BundleProductPriceTest extends TestCase |
| 26 | +{ |
| 27 | + /** @var ObjectManagerInterface */ |
| 28 | + private $objectManager; |
| 29 | + |
| 30 | + /** @var Registry */ |
| 31 | + private $registry; |
| 32 | + |
| 33 | + /** @var ProductRepositoryInterface */ |
| 34 | + private $productRepository; |
| 35 | + |
| 36 | + /** @var SerializerInterface */ |
| 37 | + private $json; |
| 38 | + |
| 39 | + /** @var Bundle */ |
| 40 | + private $block; |
| 41 | + |
| 42 | + /** |
| 43 | + * @inheritdoc |
| 44 | + */ |
| 45 | + protected function setUp() |
| 46 | + { |
| 47 | + parent::setUp(); |
| 48 | + |
| 49 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 50 | + $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class); |
| 51 | + $this->productRepository->cleanCache(); |
| 52 | + $this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Bundle::class); |
| 53 | + $this->registry = $this->objectManager->get(Registry::class); |
| 54 | + $this->json = $this->objectManager->get(SerializerInterface::class); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @inheritdoc |
| 59 | + */ |
| 60 | + protected function tearDown() |
| 61 | + { |
| 62 | + $this->registry->unregister('product'); |
| 63 | + |
| 64 | + parent::tearDown(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @magentoDataFixture Magento/Bundle/_files/dynamic_bundle_product_multiselect_option.php |
| 69 | + * |
| 70 | + * @return void |
| 71 | + */ |
| 72 | + public function testDynamicBundleOptionPrices(): void |
| 73 | + { |
| 74 | + $expectedData = [ |
| 75 | + 'options_prices' => [ |
| 76 | + [ |
| 77 | + 'oldPrice' => ['amount' => 10], |
| 78 | + 'basePrice' => ['amount' => 10], |
| 79 | + 'finalPrice' => ['amount' => 10], |
| 80 | + ], |
| 81 | + [ |
| 82 | + 'oldPrice' => ['amount' => 20], |
| 83 | + 'basePrice' => ['amount' => 20], |
| 84 | + 'finalPrice' => ['amount' => 20], |
| 85 | + ], |
| 86 | + [ |
| 87 | + 'oldPrice' => ['amount' => 30], |
| 88 | + 'basePrice' => ['amount' => 30], |
| 89 | + 'finalPrice' => ['amount' => 30], |
| 90 | + ], |
| 91 | + ], |
| 92 | + 'bundle_prices' => [ |
| 93 | + 'oldPrice' => ['amount' => 0], |
| 94 | + 'basePrice' => ['amount' => 0], |
| 95 | + 'finalPrice' => ['amount' => 0], |
| 96 | + ] |
| 97 | + ]; |
| 98 | + $this->processBundlePriceView('bundle_product', $expectedData); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @magentoDataFixture Magento/Bundle/_files/product_with_multiple_options_1.php |
| 103 | + * |
| 104 | + * @return void |
| 105 | + */ |
| 106 | + public function testFixedBundleOptionPrices(): void |
| 107 | + { |
| 108 | + $expectedData = [ |
| 109 | + 'options_prices' => [ |
| 110 | + [ |
| 111 | + 'oldPrice' => ['amount' => 2.75], |
| 112 | + 'basePrice' => ['amount' => 2.75], |
| 113 | + 'finalPrice' => ['amount' => 2.75], |
| 114 | + ], |
| 115 | + [ |
| 116 | + 'oldPrice' => ['amount' => 6.75], |
| 117 | + 'basePrice' => ['amount' => 6.75], |
| 118 | + 'finalPrice' => ['amount' => 6.75], |
| 119 | + ], |
| 120 | + ], |
| 121 | + 'bundle_prices' => [ |
| 122 | + 'oldPrice' => ['amount' => 12.75], |
| 123 | + 'basePrice' => ['amount' => 10], |
| 124 | + 'finalPrice' => ['amount' => 10], |
| 125 | + ] |
| 126 | + ]; |
| 127 | + $this->processBundlePriceView('bundle-product', $expectedData); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @param string $productSku |
| 132 | + * @param array $expectedData |
| 133 | + * @return void |
| 134 | + */ |
| 135 | + private function processBundlePriceView(string $productSku, array $expectedData): void |
| 136 | + { |
| 137 | + $this->registerProduct($productSku); |
| 138 | + $jsonConfig = $this->json->unserialize($this->block->getJsonConfig()); |
| 139 | + $this->assertEquals($expectedData['bundle_prices'], $jsonConfig['prices']); |
| 140 | + $this->assertOptionsConfig($expectedData['options_prices'], $jsonConfig); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Assert options prices. |
| 145 | + * |
| 146 | + * @param array $expectedData |
| 147 | + * @param array $actualData |
| 148 | + * @return void |
| 149 | + */ |
| 150 | + private function assertOptionsConfig(array $expectedData, array $actualData): void |
| 151 | + { |
| 152 | + $optionConfig = $actualData['options'] ?? null; |
| 153 | + $this->assertNotNull($optionConfig); |
| 154 | + $optionConfig = reset($optionConfig); |
| 155 | + foreach (array_values($optionConfig['selections']) as $key => $selection) { |
| 156 | + $this->assertEquals($expectedData[$key], $selection['prices']); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Register the product. |
| 162 | + * |
| 163 | + * @param string $productSku |
| 164 | + * @return void |
| 165 | + */ |
| 166 | + private function registerProduct(string $productSku): void |
| 167 | + { |
| 168 | + $product = $this->productRepository->get($productSku); |
| 169 | + $this->registry->unregister('product'); |
| 170 | + $this->registry->register('product', $product); |
| 171 | + } |
| 172 | +} |
0 commit comments