Skip to content

Commit 0c62b9b

Browse files
committed
[CardinalCommerce Module] Unit Test to cover JwtParserTest class
1 parent abbfa03 commit 0c62b9b

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CardinalCommerce\Test\Unit\Model\Response;
9+
10+
use Magento\CardinalCommerce\Model\Response\JwtParser;
11+
use Magento\CardinalCommerce\Model\Config;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
use Magento\CardinalCommerce\Model\JwtManagement;
14+
use Magento\CardinalCommerce\Model\Response\JwtPayloadValidatorInterface;
15+
use Magento\Framework\Exception\LocalizedException;
16+
17+
/**
18+
* Class \Magento\CardinalCommerce\Test\Unit\Model\Response\JwtParserTest
19+
*/
20+
class JwtParserTest extends \PHPUnit\Framework\TestCase
21+
{
22+
/**
23+
* @var ObjectManager
24+
*/
25+
private $objectManager;
26+
27+
/**
28+
* @var JwtParser
29+
*/
30+
private $model;
31+
32+
/**
33+
* @var \PHPUnit_Framework_MockObject_MockObject | Config
34+
*/
35+
private $configMock;
36+
37+
/**
38+
* @var \PHPUnit_Framework_MockObject_MockObject | JwtManagement
39+
*/
40+
private $jwtManagementMock;
41+
42+
/**
43+
* @var \PHPUnit_Framework_MockObject_MockObject | JwtPayloadValidatorInterface
44+
*/
45+
private $jwtPayloadValidatorMock;
46+
47+
/**
48+
* @inheritdoc
49+
*/
50+
protected function setUp()
51+
{
52+
$this->objectManager = new ObjectManager($this);
53+
54+
$this->configMock = $this->getMockBuilder(Config::class)
55+
->setMethods(['getApiKey', 'isDebugModeEnabled'])
56+
->disableOriginalConstructor()
57+
->getMock();
58+
59+
$this->jwtManagementMock = $this->getMockBuilder(JwtManagement::class)
60+
->setMethods(['decode'])
61+
->disableOriginalConstructor()
62+
->getMock();
63+
64+
$this->jwtPayloadValidatorMock = $this->getMockBuilder(JwtPayloadValidatorInterface::class)
65+
->setMethods(['validate'])
66+
->disableOriginalConstructor()
67+
->getMock();
68+
69+
$this->model = $this->objectManager->getObject(
70+
JwtParser::class,
71+
[
72+
'jwtManagement' => $this->jwtManagementMock,
73+
'config' => $this->configMock,
74+
'tokenValidator' => $this->jwtPayloadValidatorMock
75+
]
76+
);
77+
78+
$this->configMock->expects($this->any())
79+
->method('getApiKey')
80+
->willReturn('API Key');
81+
82+
$this->configMock->expects($this->any())
83+
->method('isDebugModeEnabled')
84+
->willReturn(false);
85+
86+
$this->jwtManagementMock->expects($this->any())
87+
->method('decode')
88+
->with('string_to_test', 'API Key')
89+
->willReturn(['mockResult' => 'jwtPayload']);
90+
}
91+
92+
/**
93+
* Tests Jwt Parser execute with the result and no exception.
94+
*/
95+
public function testExecuteWithNoException()
96+
{
97+
/* Validate Success */
98+
$this->jwtPayloadValidatorMock->expects($this->any())
99+
->method('validate')
100+
->with(['mockResult' => 'jwtPayload'])
101+
->willReturn(true);
102+
103+
/* Assert the result of function */
104+
$jwtPayload = $this->model->execute('string_to_test');
105+
$this->assertEquals(
106+
['mockResult' => 'jwtPayload'],
107+
$jwtPayload
108+
);
109+
}
110+
111+
/**
112+
* Tests Jwt Parser execute with exception and no result.
113+
*/
114+
public function testExecuteWithException()
115+
{
116+
/* Validate Fail */
117+
$this->jwtPayloadValidatorMock->expects($this->any())
118+
->method('validate')
119+
->with(['mockResult' => 'jwtPayload'])
120+
->willReturn(false);
121+
122+
$this->expectException(LocalizedException::class);
123+
$this->expectExceptionMessage(
124+
'Authentication Failed. Your card issuer cannot authenticate this card. ' .
125+
'Please select another card or form of payment to complete your purchase.'
126+
);
127+
128+
/* Execute function */
129+
$this->model->execute('string_to_test');
130+
}
131+
}

0 commit comments

Comments
 (0)