Skip to content

Commit 489029c

Browse files
ENGCOM-6322: Unit Test to cover class \Magento\Captcha\CustomerData\Captcha #25686
- Merge Pull Request #25686 from edenduong/magento2:2.3-testcover/customerdata_captcha - Merged commits: 1. 129771e 2. 7534f33 3. 9992c0e
2 parents 8ebf827 + 9992c0e commit 489029c

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Captcha\Test\Unit\CustomerData;
10+
11+
use Magento\Captcha\Helper\Data as CaptchaHelper;
12+
use Magento\Customer\Model\Session as CustomerSession;
13+
use Magento\Captcha\CustomerData\Captcha;
14+
use Magento\Captcha\Model\DefaultModel;
15+
use Magento\Customer\Api\Data\CustomerInterface as CustomerData;
16+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class CaptchaTest extends TestCase
20+
{
21+
/**
22+
* @var CaptchaHelper|\PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
private $helperMock;
25+
26+
/**
27+
* @var CustomerSession|\PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
private $customerSessionMock;
30+
31+
/**
32+
* @var Captcha
33+
*/
34+
private $model;
35+
36+
/**
37+
* @var array
38+
*/
39+
private $formIds;
40+
41+
/**
42+
* @var ObjectManagerHelper
43+
*/
44+
protected $objectManagerHelper;
45+
46+
/**
47+
* Create mocks and model
48+
*/
49+
protected function setUp()
50+
{
51+
$this->helperMock = $this->createMock(CaptchaHelper::class);
52+
$this->customerSessionMock = $this->createMock(CustomerSession::class);
53+
$this->formIds = [
54+
'user_login'
55+
];
56+
$this->objectManagerHelper = new ObjectManagerHelper($this);
57+
$this->model = $this->objectManagerHelper->getObject(
58+
Captcha::class,
59+
[
60+
'helper' => $this->helperMock,
61+
'formIds' => $this->formIds,
62+
'customerSession' => $this->customerSessionMock
63+
]
64+
);
65+
}
66+
67+
/**
68+
* Test getSectionData() when user is login and require captcha
69+
*/
70+
public function testGetSectionDataWhenLoginAndRequireCaptcha()
71+
{
72+
$emailLogin = 'test@localhost.com';
73+
74+
$userLoginModel = $this->createMock(DefaultModel::class);
75+
$userLoginModel->expects($this->any())->method('isRequired')->with($emailLogin)
76+
->willReturn(true);
77+
$this->helperMock->expects($this->any())->method('getCaptcha')->with('user_login')->willReturn($userLoginModel);
78+
79+
$this->customerSessionMock->expects($this->any())->method('isLoggedIn')
80+
->willReturn(true);
81+
82+
$customerDataMock = $this->createMock(CustomerData::class);
83+
$customerDataMock->expects($this->any())->method('getEmail')->willReturn($emailLogin);
84+
$this->customerSessionMock->expects($this->any())->method('getCustomerData')
85+
->willReturn($customerDataMock);
86+
87+
/* Assert to test */
88+
$this->assertEquals(
89+
[
90+
"user_login" => [
91+
"isRequired" => true,
92+
"timestamp" => time()
93+
]
94+
],
95+
$this->model->getSectionData()
96+
);
97+
}
98+
}

0 commit comments

Comments
 (0)