Skip to content

Commit 763a37c

Browse files
author
Dale Sikkema
committed
MAGETWO-37224: Create unit tests in Variable module
1 parent 9800042 commit 763a37c

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

app/code/Magento/Variable/Model/Variable.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ protected function _construct()
6969
*
7070
* @param integer $storeId
7171
* @return $this
72+
* @codeCoverageIgnore
7273
*/
7374
public function setStoreId($storeId)
7475
{
@@ -80,6 +81,7 @@ public function setStoreId($storeId)
8081
* Getter
8182
*
8283
* @return integer
84+
* @codeCoverageIgnore
8385
*/
8486
public function getStoreId()
8587
{
@@ -91,6 +93,7 @@ public function getStoreId()
9193
*
9294
* @param string $code
9395
* @return $this
96+
* @codeCoverageIgnore
9497
*/
9598
public function loadByCode($code)
9699
{
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?php
2+
/***
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Variable\Test\Unit\Model;
7+
8+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
9+
10+
class VariableTest extends \PHPUnit_Framework_TestCase
11+
{
12+
/** @var \Magento\Variable\Model\Variable */
13+
private $model;
14+
15+
/** @var \PHPUnit_Framework_MockObject_MockObject */
16+
private $escaperMock;
17+
18+
/** @var \PHPUnit_Framework_MockObject_MockObject */
19+
private $resourceMock;
20+
21+
/** @var \Magento\Framework\Phrase */
22+
private $validationFailedPhrase;
23+
24+
/** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
25+
private $objectManager;
26+
27+
public function setUp()
28+
{
29+
$this->objectManager = new ObjectManager($this);
30+
$this->escaperMock = $this->getMockBuilder('Magento\Framework\Escaper')
31+
->disableOriginalConstructor()
32+
->getMock();
33+
$this->resourceMock = $this->getMockBuilder('Magento\Variable\Model\Resource\Variable')
34+
->disableOriginalConstructor()
35+
->getMock();
36+
$this->model = $this->objectManager->getObject(
37+
'Magento\Variable\Model\Variable',
38+
[
39+
'escaper' => $this->escaperMock,
40+
'resource' => $this->resourceMock
41+
]
42+
);
43+
$this->validationFailedPhrase = __('Validation has failed.');
44+
}
45+
46+
public function testGetValueHtml()
47+
{
48+
$type = \Magento\Variable\Model\Variable::TYPE_HTML;
49+
$html = '<html/>';
50+
$this->model->setData('html_value', $html);
51+
$this->assertSame($html, $this->model->getValue($type));
52+
}
53+
54+
public function testGetValueEmptyHtml()
55+
{
56+
$type = \Magento\Variable\Model\Variable::TYPE_HTML;
57+
$html = '';
58+
$plain = 'unescaped_plain_text';
59+
$escapedPlain = 'escaped_plain_text';
60+
$this->model->setData('html_value', $html);
61+
$this->model->setData('plain_value', $plain);
62+
$this->escaperMock->expects($this->once())
63+
->method('escapeHtml')
64+
->with($plain)
65+
->willReturn($escapedPlain);
66+
$this->assertSame($escapedPlain, $this->model->getValue($type));
67+
}
68+
69+
public function testGetValueText()
70+
{
71+
$type = \Magento\Variable\Model\Variable::TYPE_TEXT;
72+
$plain = 'plain';
73+
$this->model->setData('plain_value', $plain);
74+
$this->assertSame($plain, $this->model->getValue($type));
75+
}
76+
77+
/**
78+
* @dataProvider missingInfoProvider
79+
*/
80+
public function testValidateMissingInfo($code, $name)
81+
{
82+
$this->model->setCode($code)->setName($name);
83+
$this->assertEquals($this->validationFailedPhrase, $this->model->validate());
84+
85+
}
86+
87+
/**
88+
* @dataProvider variableAndIdProvider
89+
*/
90+
public function testValidate($variableArray, $objectId, $expectedResult)
91+
{
92+
$code = 'variable_code';
93+
$this->model->setCode($code)->setName('some_name');
94+
$this->resourceMock->expects($this->once())
95+
->method('getVariableByCode')
96+
->with($code)
97+
->willReturn($variableArray);
98+
$this->model->setId($objectId);
99+
$this->assertEquals($expectedResult, $this->model->validate($variableArray));
100+
}
101+
102+
public function testGetVariablesOptionArrayNoGroup()
103+
{
104+
$origOptions = [
105+
['value' => 'VAL', 'label' => 'LBL',]
106+
];
107+
108+
$transformedOptions = [
109+
['value' => '{{customVar code=VAL}}', 'label' => __('%1', 'LBL')]
110+
];
111+
112+
$collectionMock = $this->getMockBuilder('\Magento\Variable\Model\Resource\Variable\Collection')
113+
->disableOriginalConstructor()
114+
->getMock();
115+
$collectionMock->expects($this->any())
116+
->method('toOptionArray')
117+
->willReturn($origOptions);
118+
$mockVariable = $this->getMock(
119+
'Magento\Variable\Model\Variable',
120+
['getCollection'],
121+
$this->objectManager->getConstructArguments('Magento\Variable\Model\Variable')
122+
);
123+
$mockVariable->expects($this->any())
124+
->method('getCollection')
125+
->willReturn($collectionMock);
126+
$this->assertEquals($transformedOptions, $mockVariable->getVariablesOptionArray());
127+
}
128+
129+
public function testGetVariablesOptionArrayWithGroup()
130+
{
131+
$origOptions = [
132+
['value' => 'VAL', 'label' => 'LBL',]
133+
];
134+
135+
$transformedOptions = [
136+
'label' => __('Custom Variables'),
137+
'value' => [
138+
['value' => '{{customVar code=VAL}}', 'label' => __('%1', 'LBL')]
139+
]
140+
];
141+
142+
$collectionMock = $this->getMockBuilder('\Magento\Variable\Model\Resource\Variable\Collection')
143+
->disableOriginalConstructor()
144+
->getMock();
145+
$collectionMock->expects($this->any())
146+
->method('toOptionArray')
147+
->willReturn($origOptions);
148+
$mockVariable = $this->getMock(
149+
'Magento\Variable\Model\Variable',
150+
['getCollection'],
151+
$this->objectManager->getConstructArguments('Magento\Variable\Model\Variable')
152+
);
153+
$mockVariable->expects($this->any())
154+
->method('getCollection')
155+
->willReturn($collectionMock);
156+
$this->assertEquals($transformedOptions, $mockVariable->getVariablesOptionArray(true));
157+
}
158+
159+
public function variableAndIdProvider()
160+
{
161+
$variable = [
162+
'variable_id' => 'matching_id',
163+
];
164+
return [
165+
'Empty Variable' => [[], null, true],
166+
'IDs match' => [$variable, 'matching_id', true],
167+
'IDs do not match' => [$variable, 'non_matching_id', __('Variable Code must be unique.')]
168+
];
169+
}
170+
171+
public function missingInfoProvider()
172+
{
173+
return [
174+
'Missing code' => ['', 'some-name'],
175+
'Missing name' => ['some-code', '']
176+
];
177+
}
178+
}

0 commit comments

Comments
 (0)