Skip to content

Commit 950e426

Browse files
ENGCOM-6219: [Review] Unit Test to cover Helper Data (Magento\Review\Helper\Data) #25409
2 parents b373ca6 + ab57c34 commit 950e426

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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\Review\Test\Unit\Helper;
10+
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
12+
use Magento\Review\Helper\Data as HelperData;
13+
use Magento\Framework\Escaper;
14+
use Magento\Framework\Filter\FilterManager;
15+
use Magento\Framework\App\Helper\Context;
16+
use Magento\Framework\App\Config\ScopeConfigInterface;
17+
use Magento\Store\Model\ScopeInterface;
18+
19+
/**
20+
* Class \Magento\Review\Test\Unit\Helper\DataTest
21+
*/
22+
class DataTest extends \PHPUnit\Framework\TestCase
23+
{
24+
/**
25+
* @var ObjectManagerHelper
26+
*/
27+
private $objectManager;
28+
29+
/**
30+
* @var HelperData
31+
*/
32+
private $helper;
33+
34+
/**
35+
* @var \PHPUnit_Framework_MockObject_MockObject|Escaper
36+
*/
37+
private $escaper;
38+
39+
/**
40+
* @var \PHPUnit_Framework_MockObject_MockObject|FilterManager
41+
*/
42+
private $filter;
43+
44+
/**
45+
* @var \PHPUnit_Framework_MockObject_MockObject|Context
46+
*/
47+
private $context;
48+
49+
/**
50+
* @var \PHPUnit_Framework_MockObject_MockObject|ScopeConfigInterface
51+
*/
52+
private $scopeConfig;
53+
54+
/**
55+
* Setup environment
56+
*/
57+
protected function setUp()
58+
{
59+
$this->context = $this->getMockBuilder(Context::class)
60+
->disableOriginalConstructor()
61+
->getMock();
62+
63+
$this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)
64+
->disableOriginalConstructor()
65+
->getMock();
66+
67+
$this->filter = $this->getMockBuilder(FilterManager::class)
68+
->disableOriginalConstructor()
69+
->setMethods(['truncate'])
70+
->getMock();
71+
72+
$this->escaper = $this->getMockBuilder(Escaper::class)
73+
->disableOriginalConstructor()
74+
->getMock();
75+
76+
$this->context->expects($this->once())
77+
->method('getScopeConfig')
78+
->willReturn($this->scopeConfig);
79+
80+
$this->objectManager = new ObjectManagerHelper($this);
81+
$this->helper = $this->objectManager->getObject(
82+
HelperData::class,
83+
[
84+
'context' => $this->context,
85+
'escaper' => $this->escaper,
86+
'filter' => $this->filter
87+
]
88+
);
89+
}
90+
91+
/**
92+
* Test getDetail() function
93+
*/
94+
public function testGetDetail()
95+
{
96+
$origDetail = "This\nis\na\nstring";
97+
$expected = "This<br />"."\n"."is<br />"."\n"."a<br />"."\n"."string";
98+
99+
$this->filter->expects($this->any())->method('truncate')
100+
->with($origDetail, ['length' => 50])
101+
->willReturn($origDetail);
102+
103+
$this->assertEquals($expected, $this->helper->getDetail($origDetail));
104+
}
105+
106+
/**
107+
* Test getDetailHtml() function
108+
*/
109+
public function getDetailHtml()
110+
{
111+
$origDetail = "<span>This\nis\na\nstring</span>";
112+
$origDetailEscapeHtml = "This\nis\na\nstring";
113+
$expected = "This<br />"."\n"."is<br />"."\n"."a<br />"."\n"."string";
114+
115+
$this->escaper->expects($this->any())->method('escapeHtml')
116+
->with($origDetail)
117+
->willReturn($origDetailEscapeHtml);
118+
119+
$this->filter->expects($this->any())->method('truncate')
120+
->with($origDetailEscapeHtml, ['length' => 50])
121+
->willReturn($origDetailEscapeHtml);
122+
123+
$this->assertEquals($expected, $this->helper->getDetail($origDetail));
124+
}
125+
126+
/**
127+
* Test getIsGuestAllowToWrite() function
128+
*/
129+
public function testGetIsGuestAllowToWrite()
130+
{
131+
$this->scopeConfig->expects($this->any())->method('isSetFlag')
132+
->with('catalog/review/allow_guest', ScopeInterface::SCOPE_STORE)
133+
->willReturn('1');
134+
135+
$this->assertEquals(true, $this->helper->getIsGuestAllowToWrite());
136+
}
137+
138+
/**
139+
* Test getReviewStatuses() function
140+
*/
141+
public function testGetReviewStatuses()
142+
{
143+
$expected = [
144+
1 => __('Approved'),
145+
2 => __('Pending'),
146+
3 => __('Not Approved')
147+
];
148+
$this->assertEquals($expected, $this->helper->getReviewStatuses());
149+
}
150+
151+
/**
152+
* Test getReviewStatusesOptionArray() function
153+
*/
154+
public function testGetReviewStatusesOptionArray()
155+
{
156+
$expected = [
157+
['value' => 1, 'label' => __('Approved')],
158+
['value' => 2, 'label' => __('Pending')],
159+
['value' => 3, 'label' => __('Not Approved')]
160+
];
161+
$this->assertEquals($expected, $this->helper->getReviewStatusesOptionArray());
162+
}
163+
}

0 commit comments

Comments
 (0)