Skip to content

Commit b63c936

Browse files
authored
ENGCOM-6867: [Theme] Covered Unit Test for \Magento\Theme\Controller\Result\JsFooterPlugin #26820
2 parents 94538b9 + 3bf436e commit b63c936

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Theme\Test\Unit\Controller\Result;
9+
10+
use Magento\Theme\Controller\Result\JsFooterPlugin;
11+
use Magento\Framework\App\Response\Http;
12+
use PHPUnit\Framework\TestCase;
13+
use PHPUnit\Framework\MockObject\MockObject;
14+
use Magento\Framework\App\Config\ScopeConfigInterface;
15+
use Magento\Store\Model\ScopeInterface;
16+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
17+
18+
/**
19+
* Unit test for Magento\Theme\Test\Unit\Controller\Result\JsFooterPlugin.
20+
*/
21+
class JsFooterPluginTest extends TestCase
22+
{
23+
const STUB_XML_PATH_DEV_MOVE_JS_TO_BOTTOM = 'dev/js/move_script_to_bottom';
24+
25+
/**
26+
* @var JsFooterPlugin
27+
*/
28+
private $plugin;
29+
30+
/**
31+
* @var ScopeConfigInterface|MockObject
32+
*/
33+
private $scopeConfigMock;
34+
35+
/**
36+
* @var Http|MockObject
37+
*/
38+
private $httpMock;
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
protected function setUp(): void
44+
{
45+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
46+
->setMethods(['isSetFlag'])
47+
->disableOriginalConstructor()
48+
->getMockForAbstractClass();
49+
50+
$this->httpMock = $this->createMock(Http::class);
51+
52+
$objectManager = new ObjectManagerHelper($this);
53+
$this->plugin = $objectManager->getObject(
54+
JsFooterPlugin::class,
55+
[
56+
'scopeConfig' => $this->scopeConfigMock
57+
]
58+
);
59+
}
60+
61+
/**
62+
* Data Provider for testBeforeSendResponse()
63+
*
64+
* @return array
65+
*/
66+
public function sendResponseDataProvider(): array
67+
{
68+
return [
69+
'content_with_script_tag' => [
70+
"content" => "<body><h1>Test Title</h1>" .
71+
"<script type=\"text/x-magento-init\">test</script>" .
72+
"<script type=\"text/x-magento-template\">test</script>" .
73+
"<p>Test Content</p></body>",
74+
"flag" => true,
75+
"result" => "<body><h1>Test Title</h1>" .
76+
"<script type=\"text/x-magento-template\">test</script>" .
77+
"<p>Test Content</p>" .
78+
"<script type=\"text/x-magento-init\">test</script>" .
79+
"\n</body>"
80+
],
81+
'content_with_config_disable' => [
82+
"content" => "<body><p>Test Content</p></body>",
83+
"flag" => false,
84+
"result" => "<body><p>Test Content</p></body>"
85+
],
86+
'content_without_script_tag' => [
87+
"content" => "<body><p>Test Content</p></body>",
88+
"flag" => true,
89+
"result" => "<body><p>Test Content</p>\n</body>"
90+
]
91+
];
92+
}
93+
94+
/**
95+
* Test beforeSendResponse
96+
*
97+
* @param string $content
98+
* @param bool $isSetFlag
99+
* @param string $result
100+
* @return void
101+
* @dataProvider sendResponseDataProvider
102+
*/
103+
public function testBeforeSendResponse($content, $isSetFlag, $result): void
104+
{
105+
$this->httpMock->expects($this->once())
106+
->method('getContent')
107+
->willReturn($content);
108+
109+
$this->scopeConfigMock->expects($this->once())
110+
->method('isSetFlag')
111+
->with(
112+
self::STUB_XML_PATH_DEV_MOVE_JS_TO_BOTTOM,
113+
ScopeInterface::SCOPE_STORE
114+
)
115+
->willReturn($isSetFlag);
116+
117+
$this->httpMock->expects($this->any())
118+
->method('setContent')
119+
->with($result);
120+
121+
$this->plugin->beforeSendResponse($this->httpMock);
122+
}
123+
124+
/**
125+
* Data Provider for testBeforeSendResponseIfGetContentIsNotAString()
126+
*
127+
* @return array
128+
*/
129+
public function ifGetContentIsNotAStringDataProvider(): array
130+
{
131+
return [
132+
'empty_array' => [
133+
'content' => []
134+
],
135+
'null' => [
136+
'content' => null
137+
]
138+
];
139+
}
140+
141+
/**
142+
* Test BeforeSendResponse if content is not a string
143+
*
144+
* @param string $content
145+
* @return void
146+
* @dataProvider ifGetContentIsNotAStringDataProvider
147+
*/
148+
public function testBeforeSendResponseIfGetContentIsNotAString($content): void
149+
{
150+
$this->httpMock->expects($this->once())
151+
->method('getContent')
152+
->willReturn($content);
153+
154+
$this->scopeConfigMock->expects($this->never())
155+
->method('isSetFlag')
156+
->with(
157+
self::STUB_XML_PATH_DEV_MOVE_JS_TO_BOTTOM,
158+
ScopeInterface::SCOPE_STORE
159+
)
160+
->willReturn(false);
161+
162+
$this->plugin->beforeSendResponse($this->httpMock);
163+
}
164+
}

0 commit comments

Comments
 (0)