Skip to content

Error in vendor/magento/module-shipping/Model/Config/Source/Allmethod… #25315

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

Merged
merged 3 commits into from
Dec 29, 2019
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
9 changes: 9 additions & 0 deletions app/code/Magento/Shipping/Model/Config/Source/Allmethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\Shipping\Model\Config\Source;

/**
* @inheritdoc
*/
class Allmethods implements \Magento\Framework\Option\ArrayInterface
{
/**
Expand Down Expand Up @@ -33,6 +36,7 @@ public function __construct(

/**
* Return array of carriers.
*
* If $isActiveOnlyFlag is set to true, will return only active carriers
*
* @param bool $isActiveOnlyFlag
Expand All @@ -56,6 +60,11 @@ public function toOptionArray($isActiveOnlyFlag = false)
);
$methods[$carrierCode] = ['label' => $carrierTitle, 'value' => []];
foreach ($carrierMethods as $methodCode => $methodTitle) {

/** Check it $carrierMethods array was well formed */
if (!$methodCode) {
continue;
}
$methods[$carrierCode]['value'][] = [
'value' => $carrierCode . '_' . $methodCode,
'label' => '[' . $carrierCode . '] ' . $methodTitle,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Shipping\Test\Unit\Model\Config\Source;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Shipping\Model\Carrier\AbstractCarrierInterface;
use Magento\Shipping\Model\Config;
use Magento\Shipping\Model\Config\Source\Allmethods;
use PHPUnit\Framework\MockObject\MockObject;

/**
* Tests for Allmethods Class
*/
class AllmethodsTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ScopeConfigInterface|MockObject $scopeConfig
*/
private $scopeConfig;

/**
* @var Config|MockObject $shippingConfig
*/
private $shippingConfig;

/**
* @var Allmethods $allmethods
*/
private $allmethods;

/**
* @var MockObject
*/
private $carriersMock;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->scopeConfig = $this->createMock(ScopeConfigInterface::class);
$this->shippingConfig = $this->createMock(Config::class);
$this->carriersMock = $this->getMockBuilder(AbstractCarrierInterface::class)
->setMethods(
[
'isActive',
'getAllowedMethods'
]
)->getMockForAbstractClass();

$this->allmethods = new Allmethods(
$this->scopeConfig,
$this->shippingConfig
);
}

/**
* Ensure that options converted correctly
*
* @dataProvider getCarriersMethodsProvider
* @param array $expectedArray
* @return void
*/
public function testToOptionArray(array $expectedArray): void
{
$expectedArray['getAllCarriers'] = [$this->carriersMock];

$this->shippingConfig->expects($this->once())
->method('getAllCarriers')
->willReturn($expectedArray['getAllCarriers']);
$this->carriersMock->expects($this->once())
->method('isActive')
->willReturn(true);
$this->carriersMock->expects($this->once())
->method('getAllowedMethods')
->willReturn($expectedArray['allowedMethods']);
$this->assertEquals([$expectedArray['expected_result']], $this->allmethods->toOptionArray());
}

/**
* Returns providers data for test
*
* @return array
*/
public function getCarriersMethodsProvider(): array
{
return [
[
[
'allowedMethods' => [null => 'method_title'],
'expected_result' => [ 'value' => [], 'label' => null],
'getAllCarriers' => []
],
[
'allowedMethods' => ['method_code' => 'method_title'],
'expected_result' => [ 'value' => [], 'label' => 'method_code'],
'getAllCarriers' => []
]

]
];
}
}