Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix iterate on null #30822

Merged
merged 5 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 33 additions & 15 deletions app/code/Magento/Shipping/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Shipping\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\DataObject;
use Magento\Shipping\Model\Carrier\AbstractCarrierInterface;
use Magento\Store\Model\ScopeInterface;

/**
* Class Config
* Config model for shipping
* @api
* @since 100.0.2
*/
class Config extends \Magento\Framework\DataObject
class Config extends DataObject
{
/**
* Shipping origin settings
Expand All @@ -29,25 +34,25 @@ class Config extends \Magento\Framework\DataObject
/**
* Core store config
*
* @var \Magento\Framework\App\Config\ScopeConfigInterface
* @var ScopeConfigInterface
*/
protected $_scopeConfig;

/**
* @var \Magento\Shipping\Model\CarrierFactory
* @var CarrierFactory
*/
protected $_carrierFactory;

/**
* Constructor
*
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Shipping\Model\CarrierFactory $carrierFactory
* @param ScopeConfigInterface $scopeConfig
* @param CarrierFactory $carrierFactory
* @param array $data
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Shipping\Model\CarrierFactory $carrierFactory,
ScopeConfigInterface $scopeConfig,
CarrierFactory $carrierFactory,
array $data = []
) {
$this->_scopeConfig = $scopeConfig;
Expand All @@ -58,17 +63,17 @@ public function __construct(
/**
* Retrieve active system carriers
*
* @param mixed $store
* @return AbstractCarrierInterface[]
* @param mixed $store
* @return AbstractCarrierInterface[]
*/
public function getActiveCarriers($store = null)
{
$carriers = [];
$config = $this->_scopeConfig->getValue('carriers', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
$config = $this->getCarriersConfig($store);
foreach (array_keys($config) as $carrierCode) {
if ($this->_scopeConfig->isSetFlag(
'carriers/' . $carrierCode . '/active',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
ScopeInterface::SCOPE_STORE,
$store
)) {
$carrierModel = $this->_carrierFactory->create($carrierCode, $store);
Expand All @@ -77,25 +82,38 @@ public function getActiveCarriers($store = null)
}
}
}

return $carriers;
}

/**
* Retrieve all system carriers
*
* @param mixed $store
* @return AbstractCarrierInterface[]
* @param mixed $store
* @return AbstractCarrierInterface[]
*/
public function getAllCarriers($store = null)
{
$carriers = [];
$config = $this->_scopeConfig->getValue('carriers', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
$config = $this->getCarriersConfig($store);
foreach (array_keys($config) as $carrierCode) {
$model = $this->_carrierFactory->create($carrierCode, $store);
if ($model) {
$carriers[$carrierCode] = $model;
}
}

return $carriers;
}

/**
* Returns carriers config by store
*
* @param mixed $store
* @return array
*/
private function getCarriersConfig($store = null): array
{
return $this->_scopeConfig->getValue('carriers', ScopeInterface::SCOPE_STORE, $store) ?: [];
}
}
152 changes: 152 additions & 0 deletions app/code/Magento/Shipping/Test/Unit/Model/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Shipping\Test\Unit\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Shipping\Model\CarrierFactory;
use Magento\Store\Model\ScopeInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Magento\Shipping\Model\Config;

/**
* Test for \Magento\Shipping\Model\Config.
*/
class ConfigTest extends TestCase
{
private const STUB_STORE_CODE = 'default';

/**
* @var array
*/
private $shippingCarriersData = [
'flatrate' => [
'active' => '1',
'name' => 'Fixed',
'title' => 'Flat Rate',
],
'tablerate' => [
'active' => '0',
'name' => 'Table Rate',
'title' => 'Best Way',
]
];

/**
* @var Config
*/
private $model;

/**
* @var ScopeConfigInterface|MockObject
*/
private $scopeConfigMock;

/**
* @var CarrierFactory|MockObject
*/
private $carrierFactoryMock;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
$this->carrierFactoryMock = $this->createMock(CarrierFactory::class);

$this->model = new Config($this->scopeConfigMock, $this->carrierFactoryMock, []);
}

/**
* Get active carriers when there is no active on the store
*
* @return void
*/
public function testGetActiveCarriersWhenThereIsNoAvailable(): void
{
$this->scopeConfigMock->expects($this->once())
->method('getValue')
->with('carriers', ScopeInterface::SCOPE_STORE, null)
->willReturn(null);

$this->assertEquals([], $this->model->getActiveCarriers());
}

/**
* Test for getActiveCarriers
*
* @return void
*/
public function testGetActiveCarriers(): void
{
$this->scopeConfigMock->expects($this->once())
->method('getValue')
->with('carriers', ScopeInterface::SCOPE_STORE, self::STUB_STORE_CODE)
->willReturn($this->shippingCarriersData);

$this->scopeConfigMock->expects($this->exactly(2))
->method('isSetFlag')
->withConsecutive(
['carriers/flatrate/active', ScopeInterface::SCOPE_STORE, self::STUB_STORE_CODE],
['carriers/tablerate/active', ScopeInterface::SCOPE_STORE, self::STUB_STORE_CODE],
)
->willReturnOnConsecutiveCalls(
true,
false,
);

$this->carrierFactoryMock->expects($this->once())
->method('create')
->with('flatrate', self::STUB_STORE_CODE)
->willReturn(true);

$this->assertEquals(['flatrate' => true], $this->model->getActiveCarriers(self::STUB_STORE_CODE));
}

/**
* Get all carriers when there is no carriers available on the store
*
* @return void
*/
public function testGetAllCarriersWhenThereIsNoAvailable(): void
{
$this->scopeConfigMock->expects($this->once())
->method('getValue')
->with('carriers', ScopeInterface::SCOPE_STORE, null)
->willReturn(null);

$this->assertEquals([], $this->model->getAllCarriers());
}

/**
* Test for getAllCarriers
*
* @return void
*/
public function testGetAllCarriers(): void
{
$this->scopeConfigMock->expects($this->once())
->method('getValue')
->with('carriers', ScopeInterface::SCOPE_STORE, self::STUB_STORE_CODE)
->willReturn($this->shippingCarriersData);

$this->carrierFactoryMock->expects($this->exactly(2))
->method('create')
->withConsecutive(
['flatrate', self::STUB_STORE_CODE],
['tablerate', self::STUB_STORE_CODE],
)
->willReturnOnConsecutiveCalls(
true,
false,
);

$this->assertEquals(['flatrate' => true], $this->model->getAllCarriers(self::STUB_STORE_CODE));
}
}