Skip to content

[Captcha] Cover Model Config by Unit Test #25880

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

Merged
Show file tree
Hide file tree
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
101 changes: 101 additions & 0 deletions app/code/Magento/Captcha/Test/Unit/Model/Config/FontTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Captcha\Test\Unit\Model\Config;

use PHPUnit\Framework\TestCase;
use Magento\Captcha\Helper\Data as HelperData;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\Captcha\Model\Config\Font;

class FontTest extends TestCase
{
/**
* @var ObjectManagerHelper
*/
private $objectManagerHelper;

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

/**
* @var HelperData|\PHPUnit_Framework_MockObject_MockObject
*/
private $helperDataMock;

/**
* Setup Environment For Testing
*/
protected function setUp()
{
$this->helperDataMock = $this->createMock(HelperData::class);

$this->objectManagerHelper = new ObjectManagerHelper($this);

$this->model = $this->objectManagerHelper->getObject(
Font::class,
[
'captchaData' => $this->helperDataMock
]
);
}

/**
* Test toOptionArray() with data provider below
*
* @param array $fonts
* @param array $expectedResult
* @dataProvider toOptionArrayDataProvider
*/
public function testToOptionArray($fonts, $expectedResult)
{
$this->helperDataMock->expects($this->any())->method('getFonts')
->willReturn($fonts);

$this->assertEquals($expectedResult, $this->model->toOptionArray());
}

/**
* Data Provider for testing toOptionArray()
*
* @return array
*/
public function toOptionArrayDataProvider()
{
return [
'Empty get font' => [
[],
[]
],
'Get font result' => [
[
'arial' => [
'label' => 'Arial',
'path' => '/www/magento/fonts/arial.ttf'
],
'verdana' => [
'label' => 'Verdana',
'path' => '/www/magento/fonts/verdana.ttf'
]
],
[
[
'label' => 'Arial',
'value' => 'arial'
],
[
'label' => 'Verdana',
'value' => 'verdana'
]
]
]
];
}
}
100 changes: 100 additions & 0 deletions app/code/Magento/Captcha/Test/Unit/Model/Config/Form/BackendTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Captcha\Test\Unit\Model\Config\Form;

use Magento\Captcha\Model\Config\Form\Backend;
use PHPUnit\Framework\TestCase;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;

class BackendTest extends TestCase
{
/**
* @var ObjectManagerHelper
*/
private $objectManagerHelper;

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

/**
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $configMock;

/**
* Setup Environment For Testing
*/
protected function setUp()
{
$this->configMock = $this->createMock(ScopeConfigInterface::class);

$this->objectManagerHelper = new ObjectManagerHelper($this);

$this->model = $this->objectManagerHelper->getObject(
Backend::class,
[
'config' => $this->configMock
]
);
}

/**
* Test toOptionArray() with data provider below
*
* @param string|array $config
* @param array $expectedResult
* @dataProvider toOptionArrayDataProvider
*/
public function testToOptionArray($config, $expectedResult)
{
$this->configMock->expects($this->any())->method('getValue')
->with('captcha/backend/areas', 'default')
->willReturn($config);

$this->assertEquals($expectedResult, $this->model->toOptionArray());
}

/**
* Data Provider for testing toOptionArray()
*
* @return array
*/
public function toOptionArrayDataProvider()
{
return [
'Empty captcha backend areas' => [
'',
[]
],
'With two captcha backend area' => [
[
'backend_login' => [
'label' => 'Admin Login'
],
'backend_forgotpassword' => [
'label' => 'Admin Forgot Password'
]
],
[
[
'label' => 'Admin Login',
'value' => 'backend_login'
],
[
'label' => 'Admin Forgot Password',
'value' => 'backend_forgotpassword'
]
]
]
];
}
}
100 changes: 100 additions & 0 deletions app/code/Magento/Captcha/Test/Unit/Model/Config/Form/FrontendTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Captcha\Test\Unit\Model\Config\Form;

use Magento\Captcha\Model\Config\Form\Frontend;
use PHPUnit\Framework\TestCase;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;

class FrontendTest extends TestCase
{
/**
* @var ObjectManagerHelper
*/
private $objectManagerHelper;

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

/**
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $configMock;

/**
* Setup Environment For Testing
*/
protected function setUp()
{
$this->configMock = $this->createMock(ScopeConfigInterface::class);

$this->objectManagerHelper = new ObjectManagerHelper($this);

$this->model = $this->objectManagerHelper->getObject(
Frontend::class,
[
'config' => $this->configMock
]
);
}

/**
* Test toOptionArray() with data provider below
*
* @param string|array $config
* @param array $expectedResult
* @dataProvider toOptionArrayDataProvider
*/
public function testToOptionArray($config, $expectedResult)
{
$this->configMock->expects($this->any())->method('getValue')
->with('captcha/frontend/areas', 'default')
->willReturn($config);

$this->assertEquals($expectedResult, $this->model->toOptionArray());
}

/**
* Data Provider for testing toOptionArray()
*
* @return array
*/
public function toOptionArrayDataProvider()
{
return [
'Empty captcha frontend areas' => [
'',
[]
],
'With two captcha frontend area' => [
[
'product_sendtofriend_form' => [
'label' => 'Send To Friend Form'
],
'sales_rule_coupon_request' => [
'label' => 'Applying coupon code'
]
],
[
[
'label' => 'Send To Friend Form',
'value' => 'product_sendtofriend_form'
],
[
'label' => 'Applying coupon code',
'value' => 'sales_rule_coupon_request'
]
]
]
];
}
}