Skip to content

Commit 279967c

Browse files
ENGCOM-6695: [Fedex] covered Model/Source/Generic.php by unit test #26549
- Merge Pull Request #26549 from srsathish92/magento2:2.4-unit-test/fedex-model-source-generic - Merged commits: 1. 5b9b163 2. 680021b 3. 0340713 4. eb97904 5. 74b1d8b
2 parents 332e765 + 74b1d8b commit 279967c

File tree

2 files changed

+111
-3
lines changed

2 files changed

+111
-3
lines changed

app/code/Magento/Fedex/Model/Source/Generic.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Fedex\Model\Source;
78

8-
class Generic implements \Magento\Framework\Option\ArrayInterface
9+
/**
10+
* Fedex generic source implementation
11+
*/
12+
class Generic implements \Magento\Framework\Data\OptionSourceInterface
913
{
1014
/**
1115
* @var \Magento\Fedex\Model\Carrier
@@ -36,9 +40,19 @@ public function toOptionArray()
3640
{
3741
$configData = $this->_shippingFedex->getCode($this->_code);
3842
$arr = [];
39-
foreach ($configData as $code => $title) {
40-
$arr[] = ['value' => $code, 'label' => $title];
43+
if ($configData) {
44+
$arr = array_map(
45+
function ($code, $title) {
46+
return [
47+
'value' => $code,
48+
'label' => $title
49+
];
50+
},
51+
array_keys($configData),
52+
$configData
53+
);
4154
}
55+
4256
return $arr;
4357
}
4458
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Magento\Fedex\Test\Unit\Model\Source;
11+
12+
use Magento\Fedex\Model\Source\Generic;
13+
use PHPUnit\Framework\TestCase;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use Magento\Fedex\Model\Carrier;
16+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
17+
18+
/**
19+
* Unit test for Magento\Fedex\Test\Unit\Model\Source\Generic
20+
*/
21+
class GenericTest extends TestCase
22+
{
23+
/**
24+
* @var Generic
25+
*/
26+
private $model;
27+
28+
/**
29+
* @var Carrier|MockObject
30+
*/
31+
private $shippingFedexMock;
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
protected function setUp(): void
37+
{
38+
$this->shippingFedexMock = $this->createMock(Carrier::class);
39+
40+
$objectManager = new ObjectManagerHelper($this);
41+
$this->model = $objectManager->getObject(
42+
Generic::class,
43+
[
44+
'shippingFedex' => $this->shippingFedexMock
45+
]
46+
);
47+
}
48+
49+
/**
50+
* Test toOptionArray
51+
*
52+
* @param string $code
53+
* @param array|false $methods
54+
* @param array $result
55+
* @return void
56+
* @dataProvider toOptionArrayDataProvider
57+
*/
58+
public function testToOptionArray($code, $methods, $result): void
59+
{
60+
$this->model->code = $code;
61+
$this->shippingFedexMock->expects($this->once())
62+
->method('getCode')
63+
->willReturn($methods);
64+
65+
$this->assertEquals($result, $this->model->toOptionArray());
66+
}
67+
68+
/**
69+
* Data provider for testToOptionArray()
70+
*
71+
* @return array
72+
*/
73+
public function toOptionArrayDataProvider(): array
74+
{
75+
return [
76+
[
77+
'method',
78+
[
79+
'FEDEX_GROUND' => __('Ground'),
80+
'FIRST_OVERNIGHT' => __('First Overnight')
81+
],
82+
[
83+
['value' => 'FEDEX_GROUND', 'label' => __('Ground')],
84+
['value' => 'FIRST_OVERNIGHT', 'label' => __('First Overnight')]
85+
]
86+
],
87+
[
88+
'',
89+
false,
90+
[]
91+
]
92+
];
93+
}
94+
}

0 commit comments

Comments
 (0)