|
| 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\Test\Unit\Ui\DataProvider\Product\Form\Modifier; |
| 9 | + |
| 10 | +use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\CurrencySymbolProvider; |
| 11 | +use Magento\Catalog\Model\Locator\LocatorInterface; |
| 12 | +use Magento\Catalog\Model\Product; |
| 13 | +use Magento\Directory\Model\Currency as CurrencyModel; |
| 14 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 15 | +use Magento\Framework\Locale\CurrencyInterface; |
| 16 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 17 | +use Magento\Store\Api\Data\StoreInterface; |
| 18 | +use Magento\Store\Api\Data\WebsiteInterface; |
| 19 | +use Magento\Store\Model\StoreManagerInterface; |
| 20 | +use PHPUnit\Framework\MockObject\MockObject; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | +use Zend_Currency; |
| 23 | + |
| 24 | +/** |
| 25 | + * Test class for Website Currency Symbol provider |
| 26 | + */ |
| 27 | +class CurrencySymbolProviderTest extends TestCase |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @var CurrencySymbolProvider|MockObject |
| 31 | + */ |
| 32 | + private $currencySymbolProvider; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var ScopeConfigInterface|MockObject |
| 36 | + */ |
| 37 | + private $scopeConfigMock; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var StoreManagerInterface|MockObject |
| 41 | + */ |
| 42 | + private $storeManagerMock; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var LocatorInterface|MockObject |
| 46 | + */ |
| 47 | + private $locatorMock; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var CurrencyInterface|MockObject |
| 51 | + */ |
| 52 | + private $localeCurrencyMock; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var StoreInterface|MockObject |
| 56 | + */ |
| 57 | + private $currentStoreMock; |
| 58 | + |
| 59 | + /** |
| 60 | + * @var CurrencyModel|MockObject |
| 61 | + */ |
| 62 | + private $currencyMock; |
| 63 | + |
| 64 | + /** |
| 65 | + * @var Zend_Currency|MockObject |
| 66 | + */ |
| 67 | + private $websiteCurrencyMock; |
| 68 | + |
| 69 | + /** |
| 70 | + * @var Product|MockObject |
| 71 | + */ |
| 72 | + private $productMock; |
| 73 | + |
| 74 | + protected function setUp(): void |
| 75 | + { |
| 76 | + $objectManager = new ObjectManager($this); |
| 77 | + |
| 78 | + $this->scopeConfigMock = $this->getMockForAbstractClass( |
| 79 | + ScopeConfigInterface::class, |
| 80 | + [], |
| 81 | + '', |
| 82 | + true, |
| 83 | + true, |
| 84 | + true, |
| 85 | + ['getValue'] |
| 86 | + ); |
| 87 | + $this->storeManagerMock = $this->getMockForAbstractClass( |
| 88 | + StoreManagerInterface::class, |
| 89 | + [], |
| 90 | + '', |
| 91 | + true, |
| 92 | + true, |
| 93 | + true, |
| 94 | + ['getWebsites'] |
| 95 | + ); |
| 96 | + $this->currentStoreMock = $this->getMockForAbstractClass( |
| 97 | + StoreInterface::class, |
| 98 | + [], |
| 99 | + '', |
| 100 | + true, |
| 101 | + true, |
| 102 | + true, |
| 103 | + ['getBaseCurrency'] |
| 104 | + ); |
| 105 | + $this->currencyMock = $this->createMock(CurrencyModel::class); |
| 106 | + $this->websiteCurrencyMock = $this->createMock(Zend_Currency::class); |
| 107 | + $this->productMock = $this->createMock(Product::class); |
| 108 | + $this->locatorMock = $this->getMockForAbstractClass( |
| 109 | + LocatorInterface::class, |
| 110 | + [], |
| 111 | + '', |
| 112 | + true, |
| 113 | + true, |
| 114 | + true, |
| 115 | + ['getStore', 'getProduct'] |
| 116 | + ); |
| 117 | + $this->localeCurrencyMock = $this->getMockForAbstractClass( |
| 118 | + CurrencyInterface::class, |
| 119 | + [], |
| 120 | + '', |
| 121 | + true, |
| 122 | + true, |
| 123 | + true, |
| 124 | + ['getWebsites', 'getCurrency'] |
| 125 | + ); |
| 126 | + $this->currencySymbolProvider = $objectManager->getObject( |
| 127 | + CurrencySymbolProvider::class, |
| 128 | + [ |
| 129 | + 'scopeConfig' => $this->scopeConfigMock, |
| 130 | + 'storeManager' => $this->storeManagerMock, |
| 131 | + 'locator' => $this->locatorMock, |
| 132 | + 'localeCurrency' => $this->localeCurrencyMock |
| 133 | + ] |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Test for Get option array of currency symbol prefixes. |
| 139 | + * |
| 140 | + * @param int $catalogPriceScope |
| 141 | + * @param string $defaultStoreCurrencySymbol |
| 142 | + * @param array $listOfWebsites |
| 143 | + * @param array $productWebsiteIds |
| 144 | + * @param array $currencySymbols |
| 145 | + * @param array $actualResult |
| 146 | + * @dataProvider getWebsiteCurrencySymbolDataProvider |
| 147 | + */ |
| 148 | + public function testGetCurrenciesPerWebsite( |
| 149 | + int $catalogPriceScope, |
| 150 | + string $defaultStoreCurrencySymbol, |
| 151 | + array $listOfWebsites, |
| 152 | + array $productWebsiteIds, |
| 153 | + array $currencySymbols, |
| 154 | + array $actualResult |
| 155 | + ): void { |
| 156 | + $this->locatorMock->expects($this->any()) |
| 157 | + ->method('getStore') |
| 158 | + ->willReturn($this->currentStoreMock); |
| 159 | + $this->currentStoreMock->expects($this->any()) |
| 160 | + ->method('getBaseCurrency') |
| 161 | + ->willReturn($this->currencyMock); |
| 162 | + $this->currencyMock->expects($this->any()) |
| 163 | + ->method('getCurrencySymbol') |
| 164 | + ->willReturn($defaultStoreCurrencySymbol); |
| 165 | + $this->scopeConfigMock |
| 166 | + ->expects($this->any()) |
| 167 | + ->method('getValue') |
| 168 | + ->willReturn($catalogPriceScope); |
| 169 | + $this->locatorMock->expects($this->any()) |
| 170 | + ->method('getProduct') |
| 171 | + ->willReturn($this->productMock); |
| 172 | + $this->storeManagerMock->expects($this->any()) |
| 173 | + ->method('getWebsites') |
| 174 | + ->willReturn($listOfWebsites); |
| 175 | + $this->productMock->expects($this->any()) |
| 176 | + ->method('getWebsiteIds') |
| 177 | + ->willReturn($productWebsiteIds); |
| 178 | + $this->localeCurrencyMock->expects($this->any()) |
| 179 | + ->method('getCurrency') |
| 180 | + ->willReturn($this->websiteCurrencyMock); |
| 181 | + foreach ($currencySymbols as $currencySymbol) { |
| 182 | + $this->websiteCurrencyMock->expects($this->any()) |
| 183 | + ->method('getSymbol') |
| 184 | + ->willReturn($currencySymbol); |
| 185 | + } |
| 186 | + $expectedResult = $this->currencySymbolProvider |
| 187 | + ->getCurrenciesPerWebsite(); |
| 188 | + $this->assertEquals($expectedResult, $actualResult); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * DataProvider for getCurrenciesPerWebsite. |
| 193 | + * |
| 194 | + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
| 195 | + * @return array |
| 196 | + */ |
| 197 | + public function getWebsiteCurrencySymbolDataProvider(): array |
| 198 | + { |
| 199 | + return [ |
| 200 | + 'verify website currency with default website and global price scope' => [ |
| 201 | + 'catalogPriceScope' => 0, |
| 202 | + 'defaultStoreCurrencySymbol' => '$', |
| 203 | + 'listOfWebsites' => $this->getWebsitesMock( |
| 204 | + [ |
| 205 | + [ |
| 206 | + 'id' => '1', |
| 207 | + 'name' => 'Main Website', |
| 208 | + 'code' => 'main_website', |
| 209 | + 'base_currency_code' => 'USD', |
| 210 | + 'currency_symbol' => '$' |
| 211 | + ] |
| 212 | + ] |
| 213 | + ), |
| 214 | + 'productWebsiteIds' => ['1'], |
| 215 | + 'currencySymbols' => ['$'], |
| 216 | + 'actualResult' => ['$'] |
| 217 | + ], |
| 218 | + 'verify website currency with default website and website price scope' => [ |
| 219 | + 'catalogPriceScope' => 1, |
| 220 | + 'defaultStoreCurrencySymbol' => '$', |
| 221 | + 'listOfWebsites' => $this->getWebsitesMock( |
| 222 | + [ |
| 223 | + [ |
| 224 | + 'id' => '1', |
| 225 | + 'name' => 'Main Website', |
| 226 | + 'code' => 'main_website', |
| 227 | + 'base_currency_code' => 'USD', |
| 228 | + 'currency_symbol' => '$' |
| 229 | + ] |
| 230 | + ] |
| 231 | + ), |
| 232 | + 'productWebsiteIds' => ['1'], |
| 233 | + 'currencySymbols' => ['$'], |
| 234 | + 'actualResult' => ['$', '$'] |
| 235 | + ], |
| 236 | + 'verify website currency with two website and website price scope' => [ |
| 237 | + 'catalogPriceScope' => 1, |
| 238 | + 'defaultStoreCurrencySymbol' => '$', |
| 239 | + 'listOfWebsites' => $this->getWebsitesMock( |
| 240 | + [ |
| 241 | + [ |
| 242 | + 'id' => '1', |
| 243 | + 'name' => 'Main Website', |
| 244 | + 'code' => 'main_website', |
| 245 | + 'base_currency_code' => 'USD', |
| 246 | + 'currency_symbol' => '$' |
| 247 | + ], |
| 248 | + [ |
| 249 | + 'id' => '2', |
| 250 | + 'name' => 'Indian Website', |
| 251 | + 'code' => 'indian_website', |
| 252 | + 'base_currency_code' => 'INR', |
| 253 | + 'currency_symbol' => '₹' |
| 254 | + ] |
| 255 | + ] |
| 256 | + ), |
| 257 | + 'productWebsiteIds' => ['1', '2'], |
| 258 | + 'currencySymbols' => ['$', '₹'], |
| 259 | + 'actualResult' => ['$', '$', '$'] |
| 260 | + ] |
| 261 | + ]; |
| 262 | + } |
| 263 | + |
| 264 | + /** |
| 265 | + * Get list of websites mock |
| 266 | + * |
| 267 | + * @param array $websites |
| 268 | + * @return array |
| 269 | + */ |
| 270 | + private function getWebsitesMock(array $websites): array |
| 271 | + { |
| 272 | + $websitesMock = []; |
| 273 | + foreach ($websites as $key => $website) { |
| 274 | + $websitesMock[$key] = $this->getMockForAbstractClass( |
| 275 | + WebsiteInterface::class, |
| 276 | + [], |
| 277 | + '', |
| 278 | + true, |
| 279 | + true, |
| 280 | + true, |
| 281 | + ['getId', 'getBaseCurrencyCode'] |
| 282 | + ); |
| 283 | + $websitesMock[$key]->expects($this->any()) |
| 284 | + ->method('getId') |
| 285 | + ->willReturn($website['id']); |
| 286 | + $websitesMock[$key]->expects($this->any()) |
| 287 | + ->method('getBaseCurrencyCode') |
| 288 | + ->willReturn($website['base_currency_code']); |
| 289 | + } |
| 290 | + return $websitesMock; |
| 291 | + } |
| 292 | + |
| 293 | + protected function tearDown(): void |
| 294 | + { |
| 295 | + unset($this->scopeConfigMock); |
| 296 | + unset($this->storeManagerMock); |
| 297 | + unset($this->currentStoreMock); |
| 298 | + unset($this->currencyMock); |
| 299 | + unset($this->websiteCurrencyMock); |
| 300 | + unset($this->productMock); |
| 301 | + unset($this->locatorMock); |
| 302 | + unset($this->localeCurrencyMock); |
| 303 | + } |
| 304 | +} |
0 commit comments