Skip to content

Commit 129771e

Browse files
committed
Unit Test to cover class \Magento\Captcha\CustomerData\Captcha
1 parent 36ea828 commit 129771e

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
18+
/**
19+
* Test class to cover \Magento\Captcha\CustomerData\Captcha
20+
*
21+
* Class \Magento\Captcha\Test\Unit\CustomerData\CaptchaTest
22+
*/
23+
class CaptchaTest extends \PHPUnit\Framework\TestCase
24+
{
25+
/**
26+
* @var CaptchaHelper | \PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
private $helper;
29+
30+
/**
31+
* @var CustomerSession | \PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
private $customerSession;
34+
35+
/**
36+
* @var CustomerData | \PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
private $customerData;
39+
40+
/**
41+
* @var Captcha
42+
*/
43+
private $model;
44+
45+
/**
46+
* @var array
47+
*/
48+
private $formIds;
49+
50+
/**
51+
* @var ObjectManagerHelper
52+
*/
53+
protected $objectManagerHelper;
54+
55+
/**
56+
* Create mocks and model
57+
*/
58+
protected function setUp()
59+
{
60+
$this->helper = $this->createMock(CaptchaHelper::class);
61+
$this->customerSession = $this->createMock(CustomerSession::class);
62+
$this->formIds = [
63+
'user_login'
64+
];
65+
$this->objectManagerHelper = new ObjectManagerHelper($this);
66+
$this->model = $this->objectManagerHelper->getObject(
67+
Captcha::class,
68+
[
69+
'helper' => $this->helper,
70+
'formIds' => $this->formIds,
71+
'customerSession' => $this->customerSession
72+
]
73+
);
74+
}
75+
76+
/**
77+
* Test getSectionData() when user is login and require captcha
78+
*/
79+
public function testGetSectionData()
80+
{
81+
$emailLogin = 'test@localhost.com';
82+
83+
$userLoginModel = $this->createMock(DefaultModel::class);
84+
$userLoginModel->expects($this->any())->method('isRequired')->with($emailLogin)
85+
->willReturn(true);
86+
$this->helper->expects($this->any())->method('getCaptcha')->with('user_login')->willReturn($userLoginModel);
87+
88+
$this->customerSession->expects($this->any())->method('isLoggedIn')
89+
->willReturn(true);
90+
91+
$this->customerData = $this->createMock(CustomerData::class);
92+
$this->customerData->expects($this->any())->method('getEmail')->willReturn($emailLogin);
93+
$this->customerSession->expects($this->any())->method('getCustomerData')
94+
->willReturn($this->customerData);
95+
96+
/* Assert to test */
97+
$this->assertEquals(
98+
[
99+
"user_login" => [
100+
"isRequired" => true,
101+
"timestamp" => time()
102+
]
103+
],
104+
$this->model->getSectionData()
105+
);
106+
}
107+
}

0 commit comments

Comments
 (0)