Skip to content

Commit 4468cdf

Browse files
ENGCOM-6201: [Swatches] Unit Test to cover Magento\Swatches\Block\Adminhtml\Attribute\Edit\Options\Text and Magento\Swatches\Block\Adminhtml\Attribute\Edit\Options\Visual #25340
2 parents 496a855 + 2c57ace commit 4468cdf

File tree

2 files changed

+189
-0
lines changed

2 files changed

+189
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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\Swatches\Test\Unit\Block\Adminhtml\Attribute\Edit\Options;
10+
11+
use Magento\Swatches\Block\Adminhtml\Attribute\Edit\Options\Text;
12+
13+
/**
14+
* Class \Magento\Swatches\Test\Unit\Block\Adminhtml\Attribute\Edit\Options\TextTest
15+
*/
16+
class TextTest extends \PHPUnit\Framework\TestCase
17+
{
18+
/**
19+
* @var \PHPUnit_Framework_MockObject_MockObject|Text
20+
*/
21+
private $model;
22+
23+
/**
24+
* Setup environment for test
25+
*/
26+
protected function setUp()
27+
{
28+
$this->model = $this->getMockBuilder(Text::class)
29+
->disableOriginalConstructor()
30+
->setMethods(['getReadOnly', 'canManageOptionDefaultOnly', 'getOptionValues'])
31+
->getMock();
32+
}
33+
34+
/**
35+
* Test getJsonConfig with getReadOnly() is true and canManageOptionDefaultOnly() is false
36+
*/
37+
public function testGetJsonConfigDataSet1()
38+
{
39+
$testCase1 = [
40+
'dataSet' => [
41+
'read_only' => true,
42+
'can_manage_option_default_only' => false,
43+
'option_values' => [
44+
new \Magento\Framework\DataObject(['value' => 6, 'label' => 'red']),
45+
new \Magento\Framework\DataObject(['value' => 6, 'label' => 'blue']),
46+
]
47+
],
48+
'expectedResult' => '{"attributesData":[{"value":6,"label":"red"},{"value":6,"label":"blue"}],' .
49+
'"isSortable":0,"isReadOnly":1}'
50+
51+
];
52+
53+
$this->executeTest($testCase1);
54+
}
55+
56+
/**
57+
* Test getJsonConfig with getReadOnly() is false and canManageOptionDefaultOnly() is false
58+
*/
59+
public function testGetJsonConfigDataSet2()
60+
{
61+
$testCase2 = [
62+
'dataSet' => [
63+
'read_only' => false,
64+
'can_manage_option_default_only' => false,
65+
'option_values' => [
66+
new \Magento\Framework\DataObject(['value' => 6, 'label' => 'red']),
67+
new \Magento\Framework\DataObject(['value' => 6, 'label' => 'blue']),
68+
]
69+
],
70+
'expectedResult' => '{"attributesData":[{"value":6,"label":"red"},{"value":6,"label":"blue"}],' .
71+
'"isSortable":1,"isReadOnly":0}'
72+
73+
];
74+
75+
$this->executeTest($testCase2);
76+
}
77+
78+
/**
79+
* Execute test for getJsonConfig() function
80+
*/
81+
public function executeTest($testCase)
82+
{
83+
$this->model->expects($this->any())->method('getReadOnly')
84+
->willReturn($testCase['dataSet']['read_only']);
85+
$this->model->expects($this->any())->method('canManageOptionDefaultOnly')
86+
->willReturn($testCase['dataSet']['can_manage_option_default_only']);
87+
$this->model->expects($this->any())->method('getOptionValues')->willReturn(
88+
$testCase['dataSet']['option_values']
89+
);
90+
91+
$this->assertEquals($testCase['expectedResult'], $this->model->getJsonConfig());
92+
}
93+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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\Swatches\Test\Unit\Block\Adminhtml\Attribute\Edit\Options;
10+
11+
use Magento\Swatches\Block\Adminhtml\Attribute\Edit\Options\Visual;
12+
13+
/**
14+
* Class \Magento\Swatches\Test\Unit\Block\Adminhtml\Attribute\Edit\Options\VisualTest
15+
*/
16+
class VisualTest extends \PHPUnit\Framework\TestCase
17+
{
18+
/**
19+
* @var \PHPUnit_Framework_MockObject_MockObject|Visual
20+
*/
21+
private $model;
22+
23+
/**
24+
* Setup environment for test
25+
*/
26+
protected function setUp()
27+
{
28+
$this->model = $this->getMockBuilder(Visual::class)
29+
->disableOriginalConstructor()
30+
->setMethods(['getReadOnly', 'canManageOptionDefaultOnly', 'getOptionValues', 'getUrl'])
31+
->getMock();
32+
}
33+
34+
/**
35+
* Test getJsonConfig with getReadOnly() is true and canManageOptionDefaultOnly() is false
36+
*/
37+
public function testGetJsonConfigDataSet1()
38+
{
39+
$testCase1 = [
40+
'dataSet' => [
41+
'read_only' => true,
42+
'can_manage_option_default_only' => false,
43+
'upload_action_url' => 'http://magento.com/admin/swatches/iframe/show',
44+
'option_values' => [
45+
new \Magento\Framework\DataObject(['value' => 6, 'label' => 'red']),
46+
new \Magento\Framework\DataObject(['value' => 6, 'label' => 'blue']),
47+
]
48+
],
49+
'expectedResult' => '{"attributesData":[{"value":6,"label":"red"},{"value":6,"label":"blue"}],' .
50+
'"uploadActionUrl":"http:\/\/magento.com\/admin\/swatches\/iframe\/show","isSortable":0,"isReadOnly":1}'
51+
52+
];
53+
54+
$this->executeTest($testCase1);
55+
}
56+
57+
/**
58+
* Test getJsonConfig with getReadOnly() is false and canManageOptionDefaultOnly() is false
59+
*/
60+
public function testGetJsonConfigDataSet2()
61+
{
62+
$testCase1 = [
63+
'dataSet' => [
64+
'read_only' => false,
65+
'can_manage_option_default_only' => false,
66+
'upload_action_url' => 'http://magento.com/admin/swatches/iframe/show',
67+
'option_values' => [
68+
new \Magento\Framework\DataObject(['value' => 6, 'label' => 'red']),
69+
new \Magento\Framework\DataObject(['value' => 6, 'label' => 'blue']),
70+
]
71+
],
72+
'expectedResult' => '{"attributesData":[{"value":6,"label":"red"},{"value":6,"label":"blue"}],' .
73+
'"uploadActionUrl":"http:\/\/magento.com\/admin\/swatches\/iframe\/show","isSortable":1,"isReadOnly":0}'
74+
];
75+
76+
$this->executeTest($testCase1);
77+
}
78+
79+
/**
80+
* Execute test for getJsonConfig() function
81+
*/
82+
public function executeTest($testCase)
83+
{
84+
$this->model->expects($this->any())->method('getReadOnly')
85+
->willReturn($testCase['dataSet']['read_only']);
86+
$this->model->expects($this->any())->method('canManageOptionDefaultOnly')
87+
->willReturn($testCase['dataSet']['can_manage_option_default_only']);
88+
$this->model->expects($this->any())->method('getOptionValues')->willReturn(
89+
$testCase['dataSet']['option_values']
90+
);
91+
$this->model->expects($this->any())->method('getUrl')
92+
->willReturn($testCase['dataSet']['upload_action_url']);
93+
94+
$this->assertEquals($testCase['expectedResult'], $this->model->getJsonConfig());
95+
}
96+
}

0 commit comments

Comments
 (0)