Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions app/code/Magento/Fedex/Model/Source/Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Fedex\Model\Source;

class Generic implements \Magento\Framework\Option\ArrayInterface
/**
* Fedex generic source implementation
*/
class Generic implements \Magento\Framework\Data\OptionSourceInterface
{
/**
* @var \Magento\Fedex\Model\Carrier
Expand Down Expand Up @@ -36,9 +40,19 @@ public function toOptionArray()
{
$configData = $this->_shippingFedex->getCode($this->_code);
$arr = [];
foreach ($configData as $code => $title) {
$arr[] = ['value' => $code, 'label' => $title];
if ($configData) {
$arr = array_map(
function ($code, $title) {
return [
'value' => $code,
'label' => $title
];
},
array_keys($configData),
$configData
);
}

return $arr;
}
}
94 changes: 94 additions & 0 deletions app/code/Magento/Fedex/Test/Unit/Model/Source/GenericTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Fedex\Test\Unit\Model\Source;

use Magento\Fedex\Model\Source\Generic;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
use Magento\Fedex\Model\Carrier;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;

/**
* Unit test for Magento\Fedex\Test\Unit\Model\Source\Generic
*/
class GenericTest extends TestCase
{
/**
* @var Generic
*/
private $model;

/**
* @var Carrier|MockObject
*/
private $shippingFedexMock;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->shippingFedexMock = $this->createMock(Carrier::class);

$objectManager = new ObjectManagerHelper($this);
$this->model = $objectManager->getObject(
Generic::class,
[
'shippingFedex' => $this->shippingFedexMock
]
);
}

/**
* Test toOptionArray
*
* @param string $code
* @param array|false $methods
* @param array $result
* @return void
* @dataProvider toOptionArrayDataProvider
*/
public function testToOptionArray($code, $methods, $result): void
{
$this->model->code = $code;
$this->shippingFedexMock->expects($this->once())
->method('getCode')
->willReturn($methods);

$this->assertEquals($result, $this->model->toOptionArray());
}

/**
* Data provider for testToOptionArray()
*
* @return array
*/
public function toOptionArrayDataProvider(): array
{
return [
[
'method',
[
'FEDEX_GROUND' => __('Ground'),
'FIRST_OVERNIGHT' => __('First Overnight')
],
[
['value' => 'FEDEX_GROUND', 'label' => __('Ground')],
['value' => 'FIRST_OVERNIGHT', 'label' => __('First Overnight')]
]
],
[
'',
false,
[]
]
];
}
}