|
| 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\Catalog\Ui\DataProvider\Product\Related; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Framework\App\RequestInterface; |
| 12 | +use Magento\Framework\ObjectManagerInterface; |
| 13 | +use Magento\Framework\View\Element\UiComponentFactory; |
| 14 | +use Magento\Framework\View\Element\UiComponentInterface; |
| 15 | +use Magento\Store\Model\StoreManagerInterface; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Magento\TestFramework\Helper\Bootstrap; |
| 18 | + |
| 19 | +/** |
| 20 | + * Checks up-sell products data provider |
| 21 | + * |
| 22 | + * @see \Magento\Catalog\Ui\DataProvider\Product\Related\UpSellDataProvider |
| 23 | + * |
| 24 | + * @magentoAppArea adminhtml |
| 25 | + * @magentoDbIsolation disabled |
| 26 | + */ |
| 27 | +class UpSellDataProviderTest extends TestCase |
| 28 | +{ |
| 29 | + /** @var ObjectManagerInterface */ |
| 30 | + private $objectManager; |
| 31 | + |
| 32 | + /** @var UiComponentFactory */ |
| 33 | + private $componentFactory; |
| 34 | + |
| 35 | + /** @var RequestInterface */ |
| 36 | + private $request; |
| 37 | + |
| 38 | + /** @var ProductRepositoryInterface */ |
| 39 | + private $productRepository; |
| 40 | + |
| 41 | + /** @var StoreManagerInterface */ |
| 42 | + private $storeManager; |
| 43 | + |
| 44 | + /** |
| 45 | + * @inheritdoc |
| 46 | + */ |
| 47 | + protected function setUp(): void |
| 48 | + { |
| 49 | + parent::setUp(); |
| 50 | + |
| 51 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 52 | + $this->request = $this->objectManager->get(RequestInterface::class); |
| 53 | + $this->componentFactory = $this->objectManager->get(UiComponentFactory::class); |
| 54 | + $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class); |
| 55 | + $this->storeManager = $this->objectManager->get(StoreManagerInterface::class); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @dataProvider productDataProvider |
| 60 | + * |
| 61 | + * @magentoDataFixture Magento/Catalog/_files/second_product_simple.php |
| 62 | + * @magentoDataFixture Magento/Catalog/_files/product_with_price_on_second_website.php |
| 63 | + * |
| 64 | + * @param string $storeCode |
| 65 | + * @param float $price |
| 66 | + * @return void |
| 67 | + */ |
| 68 | + public function testGetData(string $storeCode, float $price): void |
| 69 | + { |
| 70 | + $this->prepareRequest('simple2', $storeCode); |
| 71 | + $result = $this->getComponentProvidedData('upsell_product_listing')['items']; |
| 72 | + $this->assertCount(1, $result); |
| 73 | + $item = reset($result); |
| 74 | + $this->assertEquals($item['sku'], 'second-website-price-product'); |
| 75 | + $this->assertEquals($price, (float)$item['price']); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @return array |
| 80 | + */ |
| 81 | + public function productDataProvider(): array |
| 82 | + { |
| 83 | + return [ |
| 84 | + 'without_store_code' => [ |
| 85 | + 'store_code' => 'default', |
| 86 | + 'product_price' => 20, |
| 87 | + ], |
| 88 | + 'with_store_code' => [ |
| 89 | + 'store_code' => 'fixture_second_store', |
| 90 | + 'product_price' => 10, |
| 91 | + ], |
| 92 | + ]; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Get component provided data |
| 97 | + * |
| 98 | + * @param string $namespace |
| 99 | + * @return array |
| 100 | + */ |
| 101 | + private function getComponentProvidedData(string $namespace): array |
| 102 | + { |
| 103 | + $component = $this->componentFactory->create($namespace); |
| 104 | + $this->prepareChildComponents($component); |
| 105 | + |
| 106 | + return $component->getContext()->getDataProvider()->getData(); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Call prepare method in the child components |
| 111 | + * |
| 112 | + * @param UiComponentInterface $component |
| 113 | + * @return void |
| 114 | + */ |
| 115 | + private function prepareChildComponents(UiComponentInterface $component): void |
| 116 | + { |
| 117 | + foreach ($component->getChildComponents() as $child) { |
| 118 | + $this->prepareChildComponents($child); |
| 119 | + } |
| 120 | + |
| 121 | + $component->prepare(); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Prepare request |
| 126 | + * |
| 127 | + * @param string $productSku |
| 128 | + * @param string $storeCode |
| 129 | + * @return void |
| 130 | + */ |
| 131 | + private function prepareRequest(string $productSku, string $storeCode): void |
| 132 | + { |
| 133 | + $storeId = (int)$this->storeManager->getStore($storeCode)->getId(); |
| 134 | + $productId = (int)$this->productRepository->get($productSku)->getId(); |
| 135 | + $this->request->setParams(['current_product_id' => $productId, 'current_store_id' => $storeId]); |
| 136 | + } |
| 137 | +} |
0 commit comments