Skip to content

Commit d8afb0e

Browse files
cover by unit test, fix failed unit tests
1 parent 2368e87 commit d8afb0e

File tree

2 files changed

+156
-67
lines changed

2 files changed

+156
-67
lines changed

app/code/Magento/Sales/Test/Unit/Model/Order/Payment/InfoTest.php

Lines changed: 140 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
declare(strict_types=1);
78

89
namespace Magento\Sales\Test\Unit\Model\Order\Payment;
@@ -12,72 +13,90 @@
1213
use Magento\Framework\Registry;
1314
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1415
use Magento\Payment\Helper\Data;
15-
use Magento\Payment\Model\Method;
1616
use Magento\Payment\Model\Method\Substitution;
1717
use Magento\Payment\Model\MethodInterface;
18+
use Magento\Sales\Api\Data\OrderInterface;
1819
use Magento\Sales\Model\Order\Payment\Info;
1920
use PHPUnit\Framework\MockObject\MockObject;
2021
use PHPUnit\Framework\TestCase;
22+
use Magento\Framework\Exception\LocalizedException;
2123

24+
/**
25+
* Test for \Magento\Sales\Model\Order\Payment\Info.
26+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27+
*/
2228
class InfoTest extends TestCase
2329
{
24-
/** @var \Magento\Sales\Model\Order\Payment\Info */
25-
protected $info;
26-
27-
/** @var ObjectManagerHelper */
28-
protected $objectManagerHelper;
29-
30-
/** @var Context|MockObject */
31-
protected $contextMock;
30+
/**
31+
* @var Info
32+
*/
33+
private $info;
3234

33-
/** @var Registry|MockObject */
34-
protected $registryMock;
35+
/**
36+
* @var Data|MockObject
37+
*/
38+
private $paymentHelperMock;
3539

36-
/** @var Data|MockObject */
37-
protected $paymentHelperMock;
40+
/**
41+
* @var EncryptorInterface|MockObject
42+
*/
43+
private $encryptorInterfaceMock;
3844

39-
/** @var EncryptorInterface|MockObject */
40-
protected $encryptorInterfaceMock;
45+
/**
46+
* @var Data|MockObject
47+
*/
48+
private $methodInstanceMock;
4149

42-
/** @var Data|MockObject */
43-
protected $methodInstanceMock;
50+
/**
51+
* @var OrderInterface|MockObject
52+
*/
53+
private $orderMock;
4454

55+
/**
56+
* @inheritdoc
57+
*/
4558
protected function setUp(): void
4659
{
47-
$this->contextMock = $this->createMock(Context::class);
48-
$this->registryMock = $this->createMock(Registry::class);
60+
$contextMock = $this->createMock(Context::class);
61+
$registryMock = $this->createMock(Registry::class);
4962
$this->paymentHelperMock = $this->createPartialMock(Data::class, ['getMethodInstance']);
5063
$this->encryptorInterfaceMock = $this->getMockForAbstractClass(EncryptorInterface::class);
51-
$this->methodInstanceMock = $this->getMockBuilder(MethodInterface::class)
52-
->getMockForAbstractClass();
64+
$this->methodInstanceMock = $this->getMockForAbstractClass(MethodInterface::class);
65+
$this->orderMock = $this->createMock(OrderInterface::class);
5366

54-
$this->objectManagerHelper = new ObjectManagerHelper($this);
55-
$this->info = $this->objectManagerHelper->getObject(
67+
$objectManagerHelper = new ObjectManagerHelper($this);
68+
$this->info = $objectManagerHelper->getObject(
5669
Info::class,
5770
[
58-
'context' => $this->contextMock,
59-
'registry' => $this->registryMock,
71+
'context' => $contextMock,
72+
'registry' => $registryMock,
6073
'paymentData' => $this->paymentHelperMock,
6174
'encryptor' => $this->encryptorInterfaceMock
6275
]
6376
);
77+
$this->info->setData('order', $this->orderMock);
6478
}
6579

6680
/**
81+
* Get data cc number
82+
*
6783
* @dataProvider ccKeysDataProvider
6884
* @param string $keyCc
6985
* @param string $keyCcEnc
86+
* @return void
7087
*/
71-
public function testGetDataCcNumber($keyCc, $keyCcEnc)
88+
public function testGetDataCcNumber($keyCc, $keyCcEnc): void
7289
{
7390
// no data was set
7491
$this->assertNull($this->info->getData($keyCc));
7592

7693
// we set encrypted data
7794
$this->info->setData($keyCcEnc, $keyCcEnc);
78-
$this->encryptorInterfaceMock->expects($this->once())->method('decrypt')->with($keyCcEnc)->willReturn(
79-
$keyCc
80-
);
95+
$this->encryptorInterfaceMock->expects($this->once())
96+
->method('decrypt')
97+
->with($keyCcEnc)
98+
->willReturn($keyCc);
99+
81100
$this->assertEquals($keyCc, $this->info->getData($keyCc));
82101
}
83102

@@ -86,22 +105,34 @@ public function testGetDataCcNumber($keyCc, $keyCcEnc)
86105
*
87106
* @return array
88107
*/
89-
public function ccKeysDataProvider()
108+
public function ccKeysDataProvider(): array
90109
{
91110
return [
92111
['cc_number', 'cc_number_enc'],
93112
['cc_cid', 'cc_cid_enc']
94113
];
95114
}
96115

97-
public function testGetMethodInstanceWithRealMethod()
116+
/**
117+
* Get method instance with real method
118+
*
119+
* @return void
120+
*/
121+
public function testGetMethodInstanceWithRealMethod(): void
98122
{
123+
$storeId = 2;
99124
$method = 'real_method';
100125
$this->info->setData('method', $method);
101126

127+
$this->orderMock->expects($this->once())
128+
->method('getStoreId')
129+
->willReturn($storeId);
102130
$this->methodInstanceMock->expects($this->once())
103131
->method('setInfoInstance')
104132
->with($this->info);
133+
$this->methodInstanceMock->expects($this->once())
134+
->method('setStore')
135+
->with($storeId);
105136

106137
$this->paymentHelperMock->expects($this->once())
107138
->method('getMethodInstance')
@@ -111,7 +142,12 @@ public function testGetMethodInstanceWithRealMethod()
111142
$this->info->getMethodInstance();
112143
}
113144

114-
public function testGetMethodInstanceWithUnrealMethod()
145+
/**
146+
* Get method instance with unreal method
147+
*
148+
* @return void
149+
*/
150+
public function testGetMethodInstanceWithUnrealMethod(): void
115151
{
116152
$method = 'unreal_method';
117153
$this->info->setData('method', $method);
@@ -133,15 +169,26 @@ public function testGetMethodInstanceWithUnrealMethod()
133169
$this->info->getMethodInstance();
134170
}
135171

136-
public function testGetMethodInstanceWithNoMethod()
172+
/**
173+
* Get method instance withot method
174+
*
175+
* @return void
176+
*/
177+
public function testGetMethodInstanceWithNoMethod(): void
137178
{
138-
$this->expectException('Magento\Framework\Exception\LocalizedException');
179+
$this->expectException(LocalizedException::class);
139180
$this->expectExceptionMessage('The payment method you requested is not available.');
181+
140182
$this->info->setData('method', false);
141183
$this->info->getMethodInstance();
142184
}
143185

144-
public function testGetMethodInstanceRequestedMethod()
186+
/**
187+
* Get method instance requested method
188+
*
189+
* @return void
190+
*/
191+
public function testGetMethodInstanceRequestedMethod(): void
145192
{
146193
$code = 'real_method';
147194
$this->info->setData('method', $code);
@@ -160,40 +207,62 @@ public function testGetMethodInstanceRequestedMethod()
160207
$this->assertSame($this->methodInstanceMock, $this->info->getMethodInstance());
161208
}
162209

163-
public function testEncrypt()
210+
/**
211+
* Encrypt test
212+
*
213+
* @return void
214+
*/
215+
public function testEncrypt(): void
164216
{
165217
$data = 'data';
166218
$encryptedData = 'd1a2t3a4';
167219

168-
$this->encryptorInterfaceMock->expects($this->once())->method('encrypt')->with($data)->willReturn(
169-
$encryptedData
170-
);
220+
$this->encryptorInterfaceMock->expects($this->once())
221+
->method('encrypt')
222+
->with($data)
223+
->willReturn($encryptedData);
224+
171225
$this->assertEquals($encryptedData, $this->info->encrypt($data));
172226
}
173227

174-
public function testDecrypt()
228+
/**
229+
* Decrypt test
230+
*
231+
* @return void
232+
*/
233+
public function testDecrypt(): void
175234
{
176235
$data = 'data';
177236
$encryptedData = 'd1a2t3a4';
178237

179-
$this->encryptorInterfaceMock->expects($this->once())->method('decrypt')->with($encryptedData)->willReturn(
180-
$data
181-
);
238+
$this->encryptorInterfaceMock->expects($this->once())
239+
->method('decrypt')
240+
->with($encryptedData)
241+
->willReturn($data);
242+
182243
$this->assertEquals($data, $this->info->decrypt($encryptedData));
183244
}
184245

185-
public function testSetAdditionalInformationException()
246+
/**
247+
* Set additional information exception
248+
*
249+
* @return void
250+
*/
251+
public function testSetAdditionalInformationException(): void
186252
{
187-
$this->expectException('Magento\Framework\Exception\LocalizedException');
253+
$this->expectException(LocalizedException::class);
188254
$this->info->setAdditionalInformation('object', new \stdClass());
189255
}
190256

191257
/**
258+
* Set additional info multiple types
259+
*
192260
* @dataProvider additionalInformationDataProvider
193261
* @param mixed $key
194262
* @param mixed $value
263+
* @return void
195264
*/
196-
public function testSetAdditionalInformationMultipleTypes($key, $value = null)
265+
public function testSetAdditionalInformationMultipleTypes($key, $value = null): void
197266
{
198267
$this->info->setAdditionalInformation($key, $value);
199268
$this->assertEquals($value ? [$key => $value] : $key, $this->info->getAdditionalInformation());
@@ -204,23 +273,33 @@ public function testSetAdditionalInformationMultipleTypes($key, $value = null)
204273
*
205274
* @return array
206275
*/
207-
public function additionalInformationDataProvider()
276+
public function additionalInformationDataProvider(): array
208277
{
209278
return [
210279
[['key1' => 'data1', 'key2' => 'data2'], null],
211280
['key', 'data']
212281
];
213282
}
214283

215-
public function testGetAdditionalInformationByKey()
284+
/**
285+
* Get additional info by key
286+
*
287+
* @return void
288+
*/
289+
public function testGetAdditionalInformationByKey(): void
216290
{
217291
$key = 'key';
218292
$value = 'value';
219293
$this->info->setAdditionalInformation($key, $value);
220294
$this->assertEquals($value, $this->info->getAdditionalInformation($key));
221295
}
222296

223-
public function testUnsAdditionalInformation()
297+
/**
298+
* Unsetter additional info
299+
*
300+
* @return void
301+
*/
302+
public function testUnsAdditionalInformation(): void
224303
{
225304
// set array to additional
226305
$data = ['key1' => 'data1', 'key2' => 'data2'];
@@ -236,7 +315,12 @@ public function testUnsAdditionalInformation()
236315
$this->assertEmpty($this->info->unsAdditionalInformation()->getAdditionalInformation());
237316
}
238317

239-
public function testHasAdditionalInformation()
318+
/**
319+
* Has additional info
320+
*
321+
* @return void
322+
*/
323+
public function testHasAdditionalInformation(): void
240324
{
241325
$this->assertFalse($this->info->hasAdditionalInformation());
242326

@@ -248,7 +332,12 @@ public function testHasAdditionalInformation()
248332
$this->assertTrue($this->info->hasAdditionalInformation());
249333
}
250334

251-
public function testInitAdditionalInformationWithUnserialize()
335+
/**
336+
* Init additional info with unserialize
337+
*
338+
* @return void
339+
*/
340+
public function testInitAdditionalInformationWithUnserialize(): void
252341
{
253342
$data = ['key1' => 'data1', 'key2' => 'data2'];
254343
$this->info->setData('additional_information', $data);

0 commit comments

Comments
 (0)