|
| 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\ProductAlert\Test\Unit\Helper; |
| 9 | + |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Magento\ProductAlert\Helper\Data as HelperData; |
| 12 | +use Magento\Framework\App\Helper\Context; |
| 13 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 14 | +use Magento\Framework\UrlInterface; |
| 15 | +use Magento\Framework\Url\EncoderInterface; |
| 16 | +use Magento\Catalog\Model\Product; |
| 17 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 18 | +use Magento\ProductAlert\Model\Observer; |
| 19 | +use Magento\Store\Model\ScopeInterface; |
| 20 | +use Magento\Framework\View\LayoutInterface; |
| 21 | +use Magento\ProductAlert\Block\Email\Price; |
| 22 | +use Magento\Framework\Exception\LocalizedException; |
| 23 | + |
| 24 | +class DataTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var HelperData |
| 28 | + */ |
| 29 | + private $helper; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var ObjectManagerHelper |
| 33 | + */ |
| 34 | + private $objectManagerHelper; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var Context|\PHPUnit_Framework_MockObject_MockObject |
| 38 | + */ |
| 39 | + private $contextMock; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var UrlInterface|\PHPUnit_Framework_MockObject_MockObject |
| 43 | + */ |
| 44 | + private $urlBuilderMock; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var EncoderInterface|\PHPUnit_Framework_MockObject_MockObject |
| 48 | + */ |
| 49 | + private $encoderMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject |
| 53 | + */ |
| 54 | + private $scopeConfigMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var LayoutInterface|\PHPUnit_Framework_MockObject_MockObject |
| 58 | + */ |
| 59 | + private $layoutMock; |
| 60 | + |
| 61 | + /** |
| 62 | + * Setup environment for testing |
| 63 | + */ |
| 64 | + protected function setUp() |
| 65 | + { |
| 66 | + $this->contextMock = $this->createMock(Context::class); |
| 67 | + $this->urlBuilderMock = $this->createMock(UrlInterface::class); |
| 68 | + $this->encoderMock = $this->createMock(EncoderInterface::class); |
| 69 | + $this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class); |
| 70 | + $this->layoutMock = $this->createMock(LayoutInterface::class); |
| 71 | + $this->contextMock->expects($this->once())->method('getUrlBuilder')->willReturn($this->urlBuilderMock); |
| 72 | + $this->contextMock->expects($this->once())->method('getUrlEncoder')->willReturn($this->encoderMock); |
| 73 | + $this->contextMock->expects($this->once())->method('getScopeConfig')->willReturn($this->scopeConfigMock); |
| 74 | + |
| 75 | + $this->objectManagerHelper = new ObjectManagerHelper($this); |
| 76 | + |
| 77 | + $productMock = $this->createMock(Product::class); |
| 78 | + $productMock->expects($this->any())->method('getId')->willReturn(1); |
| 79 | + |
| 80 | + $this->helper = $this->objectManagerHelper->getObject( |
| 81 | + HelperData::class, |
| 82 | + [ |
| 83 | + 'context' => $this->contextMock, |
| 84 | + 'layout' => $this->layoutMock |
| 85 | + ] |
| 86 | + ); |
| 87 | + $this->helper->setProduct($productMock); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Test getSaveUrl() function |
| 92 | + */ |
| 93 | + public function testGetSaveUrl() |
| 94 | + { |
| 95 | + $currentUrl = 'http://www.example.com/'; |
| 96 | + $type = 'stock'; |
| 97 | + $uenc = strtr(base64_encode($currentUrl), '+/=', '-_,'); |
| 98 | + $expected = 'http://www.example.com/roductalert/add/stock/product_id/1/uenc/' . $uenc; |
| 99 | + |
| 100 | + $this->urlBuilderMock->expects($this->any())->method('getCurrentUrl')->willReturn($currentUrl); |
| 101 | + $this->encoderMock->expects($this->any())->method('encode') |
| 102 | + ->with($currentUrl) |
| 103 | + ->willReturn($uenc); |
| 104 | + $this->urlBuilderMock->expects($this->any())->method('getUrl') |
| 105 | + ->with( |
| 106 | + 'productalert/add/' . $type, |
| 107 | + [ |
| 108 | + 'product_id' => 1, |
| 109 | + 'uenc' => $uenc |
| 110 | + ] |
| 111 | + ) |
| 112 | + ->willReturn($expected); |
| 113 | + |
| 114 | + $this->assertEquals($expected, $this->helper->getSaveUrl($type)); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Test createBlock() with no exception |
| 119 | + */ |
| 120 | + public function testCreateBlockWithNoException() |
| 121 | + { |
| 122 | + $priceBlockMock = $this->createMock(Price::class); |
| 123 | + $this->layoutMock->expects($this->once())->method('createBlock')->willReturn($priceBlockMock); |
| 124 | + |
| 125 | + $this->assertEquals($priceBlockMock, $this->helper->createBlock(Price::class)); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Test createBlock() with exception |
| 130 | + */ |
| 131 | + public function testCreateBlockWithException() |
| 132 | + { |
| 133 | + $invalidBlock = $this->createMock(Product::class); |
| 134 | + $this->expectException(LocalizedException::class); |
| 135 | + $this->expectExceptionMessage((string)__('Invalid block type: %1')); |
| 136 | + |
| 137 | + $this->helper->createBlock($invalidBlock); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Test isStockAlertAllowed() function with Yes settings |
| 142 | + */ |
| 143 | + public function testIsStockAlertAllowedWithYesSettings() |
| 144 | + { |
| 145 | + $this->scopeConfigMock->expects($this->any())->method('isSetFlag') |
| 146 | + ->with(Observer::XML_PATH_STOCK_ALLOW, ScopeInterface::SCOPE_STORE) |
| 147 | + ->willReturn('1'); |
| 148 | + |
| 149 | + $this->assertEquals('1', $this->helper->isStockAlertAllowed()); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Test isPriceAlertAllowed() function with Yes settings |
| 154 | + */ |
| 155 | + public function testIsPriceAlertAllowedWithYesSetting() |
| 156 | + { |
| 157 | + $this->scopeConfigMock->expects($this->any())->method('isSetFlag') |
| 158 | + ->with(Observer::XML_PATH_PRICE_ALLOW, ScopeInterface::SCOPE_STORE) |
| 159 | + ->willReturn('1'); |
| 160 | + |
| 161 | + $this->assertEquals('1', $this->helper->isPriceAlertAllowed()); |
| 162 | + } |
| 163 | +} |
0 commit comments