|
| 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\Model\Locator; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Framework\ObjectManagerInterface; |
| 13 | +use Magento\Framework\Registry; |
| 14 | +use Magento\Store\Api\Data\StoreInterface; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Magento\Store\Model\StoreManagerInterface; |
| 18 | + |
| 19 | +/** |
| 20 | + * Test registry locator |
| 21 | + * |
| 22 | + * @see \Magento\Catalog\Model\Locator\RegistryLocator |
| 23 | + * @magentoAppArea frontend |
| 24 | + */ |
| 25 | +class RegistryLocatorTest extends TestCase |
| 26 | +{ |
| 27 | + /** @var ObjectManagerInterface */ |
| 28 | + private $objectManager; |
| 29 | + |
| 30 | + /** @var StoreManagerInterface */ |
| 31 | + private $storeManager; |
| 32 | + |
| 33 | + /** @var RegistryLocator */ |
| 34 | + private $registryLocator; |
| 35 | + |
| 36 | + /** @var Registry */ |
| 37 | + private $registry; |
| 38 | + |
| 39 | + /** @var ProductRepositoryInterface */ |
| 40 | + private $productRepository; |
| 41 | + |
| 42 | + |
| 43 | + /** |
| 44 | + * @inheritdoc |
| 45 | + */ |
| 46 | + protected function setUp(): void |
| 47 | + { |
| 48 | + parent::setUp(); |
| 49 | + |
| 50 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 51 | + $this->storeManager = $this->objectManager->get(StoreManagerInterface::class); |
| 52 | + $this->registryLocator = $this->objectManager->get(RegistryLocator::class); |
| 53 | + $this->registry = $this->objectManager->get(Registry::class); |
| 54 | + $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class); |
| 55 | + $this->productRepository->cleanCache(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @inheritdoc |
| 60 | + */ |
| 61 | + protected function tearDown(): void |
| 62 | + { |
| 63 | + $this->registry->unregister('current_product'); |
| 64 | + $this->registry->unregister('current_store'); |
| 65 | + |
| 66 | + parent::tearDown(); |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + /** |
| 71 | + * @magentoDbIsolation disabled |
| 72 | + * @magentoDataFixture Magento/Catalog/_files/product_two_websites.php |
| 73 | + * |
| 74 | + * @return void |
| 75 | + */ |
| 76 | + public function testGetWebsiteIds(): void |
| 77 | + { |
| 78 | + $product = $this->productRepository->get('simple-on-two-websites'); |
| 79 | + $this->registerProduct($product); |
| 80 | + $this->assertEquals($product->getExtensionAttributes()->getWebsiteIds(), $this->registryLocator->getWebsiteIds()); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @return void |
| 85 | + */ |
| 86 | + public function testGetBaseCurrencyCode(): void |
| 87 | + { |
| 88 | + $store = $this->storeManager->getStore(); |
| 89 | + $this->registerStore($store); |
| 90 | + $this->assertEquals($store->getBaseCurrencyCode(), $this->registryLocator->getBaseCurrencyCode()); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Register the product |
| 95 | + * |
| 96 | + * @param ProductInterface $product |
| 97 | + * @return void |
| 98 | + */ |
| 99 | + private function registerProduct(ProductInterface $product): void |
| 100 | + { |
| 101 | + $this->registry->unregister('current_product'); |
| 102 | + $this->registry->register('current_product', $product); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Register the store |
| 107 | + * |
| 108 | + * @param StoreInterface $store |
| 109 | + * @return void |
| 110 | + */ |
| 111 | + private function registerStore(StoreInterface $store): void |
| 112 | + { |
| 113 | + $this->registry->unregister('current_store'); |
| 114 | + $this->registry->register('current_store', $store); |
| 115 | + } |
| 116 | +} |
0 commit comments