Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit Test to cover class \Magento\Captcha\CustomerData\Captcha #25686

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions app/code/Magento/Captcha/Test/Unit/CustomerData/CaptchaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Captcha\Test\Unit\CustomerData;

use Magento\Captcha\Helper\Data as CaptchaHelper;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Captcha\CustomerData\Captcha;
use Magento\Captcha\Model\DefaultModel;
use Magento\Customer\Api\Data\CustomerInterface as CustomerData;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use PHPUnit\Framework\TestCase;

class CaptchaTest extends TestCase
{
/**
* @var CaptchaHelper|\PHPUnit_Framework_MockObject_MockObject
*/
private $helperMock;

/**
* @var CustomerSession|\PHPUnit_Framework_MockObject_MockObject
*/
private $customerSessionMock;

/**
* @var Captcha
*/
private $model;

/**
* @var array
*/
private $formIds;

/**
* @var ObjectManagerHelper
*/
protected $objectManagerHelper;

/**
* Create mocks and model
*/
protected function setUp()
{
$this->helperMock = $this->createMock(CaptchaHelper::class);
$this->customerSessionMock = $this->createMock(CustomerSession::class);
$this->formIds = [
'user_login'
];
$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->model = $this->objectManagerHelper->getObject(
Captcha::class,
[
'helper' => $this->helperMock,
'formIds' => $this->formIds,
'customerSession' => $this->customerSessionMock
]
);
}

/**
* Test getSectionData() when user is login and require captcha
*/
public function testGetSectionDataWhenLoginAndRequireCaptcha()
{
$emailLogin = 'test@localhost.com';

$userLoginModel = $this->createMock(DefaultModel::class);
$userLoginModel->expects($this->any())->method('isRequired')->with($emailLogin)
->willReturn(true);
$this->helperMock->expects($this->any())->method('getCaptcha')->with('user_login')->willReturn($userLoginModel);

$this->customerSessionMock->expects($this->any())->method('isLoggedIn')
->willReturn(true);

$customerDataMock = $this->createMock(CustomerData::class);
$customerDataMock->expects($this->any())->method('getEmail')->willReturn($emailLogin);
$this->customerSessionMock->expects($this->any())->method('getCustomerData')
->willReturn($customerDataMock);

/* Assert to test */
$this->assertEquals(
[
"user_login" => [
"isRequired" => true,
"timestamp" => time()
]
],
$this->model->getSectionData()
);
}
}