|
| 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\Shipping\Test\Unit\Model; |
| 9 | + |
| 10 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 11 | +use Magento\Shipping\Model\CarrierFactory; |
| 12 | +use Magento\Store\Model\ScopeInterface; |
| 13 | +use PHPUnit\Framework\MockObject\MockObject; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Magento\Shipping\Model\Config; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test for \Magento\Shipping\Model\Config. |
| 19 | + */ |
| 20 | +class ConfigTest extends TestCase |
| 21 | +{ |
| 22 | + private const STUB_STORE_CODE = 'default'; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var array |
| 26 | + */ |
| 27 | + private $shippingCarriersData = [ |
| 28 | + 'flatrate' => [ |
| 29 | + 'active' => '1', |
| 30 | + 'name' => 'Fixed', |
| 31 | + 'title' => 'Flat Rate', |
| 32 | + ], |
| 33 | + 'tablerate' => [ |
| 34 | + 'active' => '0', |
| 35 | + 'name' => 'Table Rate', |
| 36 | + 'title' => 'Best Way', |
| 37 | + ] |
| 38 | + ]; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var Config |
| 42 | + */ |
| 43 | + private $model; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var ScopeConfigInterface|MockObject |
| 47 | + */ |
| 48 | + private $scopeConfigMock; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var CarrierFactory|MockObject |
| 52 | + */ |
| 53 | + private $carrierFactoryMock; |
| 54 | + |
| 55 | + /** |
| 56 | + * @inheritdoc |
| 57 | + */ |
| 58 | + protected function setUp(): void |
| 59 | + { |
| 60 | + $this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class); |
| 61 | + $this->carrierFactoryMock = $this->createMock(CarrierFactory::class); |
| 62 | + |
| 63 | + $this->model = new Config($this->scopeConfigMock, $this->carrierFactoryMock, []); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Get active carriers when there is no active on the store |
| 68 | + * |
| 69 | + * @return void |
| 70 | + */ |
| 71 | + public function testGetActiveCarriersWhenThereIsNoAvailable(): void |
| 72 | + { |
| 73 | + $this->scopeConfigMock->expects($this->once()) |
| 74 | + ->method('getValue') |
| 75 | + ->with('carriers', ScopeInterface::SCOPE_STORE, null) |
| 76 | + ->willReturn(null); |
| 77 | + |
| 78 | + $this->assertEquals([], $this->model->getActiveCarriers()); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Test for getActiveCarriers |
| 83 | + * |
| 84 | + * @return void |
| 85 | + */ |
| 86 | + public function testGetActiveCarriers(): void |
| 87 | + { |
| 88 | + $this->scopeConfigMock->expects($this->once()) |
| 89 | + ->method('getValue') |
| 90 | + ->with('carriers', ScopeInterface::SCOPE_STORE, self::STUB_STORE_CODE) |
| 91 | + ->willReturn($this->shippingCarriersData); |
| 92 | + |
| 93 | + $this->scopeConfigMock->expects($this->exactly(2)) |
| 94 | + ->method('isSetFlag') |
| 95 | + ->withConsecutive( |
| 96 | + ['carriers/flatrate/active', ScopeInterface::SCOPE_STORE, self::STUB_STORE_CODE], |
| 97 | + ['carriers/tablerate/active', ScopeInterface::SCOPE_STORE, self::STUB_STORE_CODE], |
| 98 | + ) |
| 99 | + ->willReturnOnConsecutiveCalls( |
| 100 | + true, |
| 101 | + false, |
| 102 | + ); |
| 103 | + |
| 104 | + $this->carrierFactoryMock->expects($this->once()) |
| 105 | + ->method('create') |
| 106 | + ->with('flatrate', self::STUB_STORE_CODE) |
| 107 | + ->willReturn(true); |
| 108 | + |
| 109 | + $this->assertEquals(['flatrate' => true], $this->model->getActiveCarriers(self::STUB_STORE_CODE)); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Get all carriers when there is no carriers available on the store |
| 114 | + * |
| 115 | + * @return void |
| 116 | + */ |
| 117 | + public function testGetAllCarriersWhenThereIsNoAvailable(): void |
| 118 | + { |
| 119 | + $this->scopeConfigMock->expects($this->once()) |
| 120 | + ->method('getValue') |
| 121 | + ->with('carriers', ScopeInterface::SCOPE_STORE, null) |
| 122 | + ->willReturn(null); |
| 123 | + |
| 124 | + $this->assertEquals([], $this->model->getAllCarriers()); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Test for getAllCarriers |
| 129 | + * |
| 130 | + * @return void |
| 131 | + */ |
| 132 | + public function testGetAllCarriers(): void |
| 133 | + { |
| 134 | + $this->scopeConfigMock->expects($this->once()) |
| 135 | + ->method('getValue') |
| 136 | + ->with('carriers', ScopeInterface::SCOPE_STORE, self::STUB_STORE_CODE) |
| 137 | + ->willReturn($this->shippingCarriersData); |
| 138 | + |
| 139 | + $this->carrierFactoryMock->expects($this->exactly(2)) |
| 140 | + ->method('create') |
| 141 | + ->withConsecutive( |
| 142 | + ['flatrate', self::STUB_STORE_CODE], |
| 143 | + ['tablerate', self::STUB_STORE_CODE], |
| 144 | + ) |
| 145 | + ->willReturnOnConsecutiveCalls( |
| 146 | + true, |
| 147 | + false, |
| 148 | + ); |
| 149 | + |
| 150 | + $this->assertEquals(['flatrate' => true], $this->model->getAllCarriers(self::STUB_STORE_CODE)); |
| 151 | + } |
| 152 | +} |
0 commit comments