|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\InstantPurchase\Test\Unit\Block; |
| 10 | + |
| 11 | +use Magento\InstantPurchase\Block\Button; |
| 12 | +use Magento\InstantPurchase\Model\Config; |
| 13 | +use Magento\Framework\View\Element\Template\Context; |
| 14 | +use Magento\Store\Model\StoreManagerInterface; |
| 15 | +use Magento\Store\Api\Data\StoreInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test class for button block |
| 19 | + * |
| 20 | + * Class \Magento\InstantPurchase\Test\Unit\Block\ButtonTest |
| 21 | + */ |
| 22 | +class ButtonTest extends \PHPUnit\Framework\TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var Button | \PHPUnit_Framework_MockObject_MockObject |
| 26 | + */ |
| 27 | + private $block; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var Config | \PHPUnit_Framework_MockObject_MockObject |
| 31 | + */ |
| 32 | + private $config; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var StoreManagerInterface | \PHPUnit_Framework_MockObject_MockObject |
| 36 | + */ |
| 37 | + private $storeManager; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var StoreInterface | \PHPUnit_Framework_MockObject_MockObject |
| 41 | + */ |
| 42 | + private $store; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var Context | \PHPUnit_Framework_MockObject_MockObject |
| 46 | + */ |
| 47 | + private $context; |
| 48 | + |
| 49 | + /** |
| 50 | + * Setup environment for testing |
| 51 | + */ |
| 52 | + protected function setUp() |
| 53 | + { |
| 54 | + $this->context = $this->createMock(Context::class); |
| 55 | + $this->storeManager = $this->createMock(StoreManagerInterface::class); |
| 56 | + $this->store = $this->createMock(StoreInterface::class); |
| 57 | + |
| 58 | + $this->storeManager->expects($this->any())->method('getStore') |
| 59 | + ->willReturn($this->store); |
| 60 | + |
| 61 | + $this->config = $this->createMock(Config::class); |
| 62 | + |
| 63 | + $this->context->expects($this->any())->method('getStoreManager') |
| 64 | + ->willReturn($this->storeManager); |
| 65 | + |
| 66 | + $this->block = $this->getMockBuilder(Button::class) |
| 67 | + ->setConstructorArgs( |
| 68 | + [ |
| 69 | + 'context' => $this->context, |
| 70 | + 'instantPurchaseConfig' => $this->config |
| 71 | + ] |
| 72 | + ) |
| 73 | + ->setMethods(['getUrl']) |
| 74 | + ->getMock(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Test isEnabled() function |
| 79 | + * |
| 80 | + * @param $currentStoreId |
| 81 | + * @param $isModuleEnabled |
| 82 | + * @param $expected |
| 83 | + * @dataProvider isEnabledDataProvider |
| 84 | + */ |
| 85 | + public function testIsEnabled($currentStoreId, $isModuleEnabled, $expected) |
| 86 | + { |
| 87 | + $this->store->expects($this->any())->method('getId') |
| 88 | + ->willReturn($currentStoreId); |
| 89 | + |
| 90 | + $this->config->expects($this->any())->method('isModuleEnabled') |
| 91 | + ->willReturn($isModuleEnabled); |
| 92 | + |
| 93 | + $this->assertEquals($expected, $this->block->isEnabled()); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Data Provider for test isEnabled() |
| 98 | + * |
| 99 | + * @return array |
| 100 | + */ |
| 101 | + public function isEnabledDataProvider() |
| 102 | + { |
| 103 | + return [ |
| 104 | + 'Store With ID = 1 and enable module' => [ |
| 105 | + 1, |
| 106 | + true, |
| 107 | + true |
| 108 | + ], |
| 109 | + 'Store With ID = 1 and disable module' => [ |
| 110 | + 1, |
| 111 | + false, |
| 112 | + false |
| 113 | + ] |
| 114 | + ]; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Test getJsLayout() function |
| 119 | + */ |
| 120 | + public function testGetJsLayout() |
| 121 | + { |
| 122 | + $currentStoreId = 1; |
| 123 | + $buttonText = 'Instant Purchased'; |
| 124 | + $url = 'https://magento2.com/instantpurchase/button/placeOrder'; |
| 125 | + $expected = '{"components":{"instant-purchase":{"config":{"buttonText":"Instant Purchased",' . |
| 126 | + '"purchaseUrl":"https:\/\/magento2.com\/instantpurchase\/button\/placeOrder"}}}}'; |
| 127 | + |
| 128 | + $this->store->expects($this->any())->method('getId') |
| 129 | + ->willReturn($currentStoreId); |
| 130 | + $this->config->expects($this->any())->method('getButtonText') |
| 131 | + ->willReturn($buttonText); |
| 132 | + $this->block->expects($this->any())->method('getUrl') |
| 133 | + ->with('instantpurchase/button/placeOrder', ['_secure' => true]) |
| 134 | + ->willReturn($url); |
| 135 | + |
| 136 | + $this->assertEquals($expected, $this->block->getJsLayout()); |
| 137 | + } |
| 138 | +} |
0 commit comments