Skip to content

Commit 6220474

Browse files
committed
[Theme] Covered Unit Test for \Magento\Theme\controller\Result\JsFooterPlugin
1 parent 90ea3f1 commit 6220474

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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 beforeSendResponse()
63+
*
64+
* @return array
65+
*/
66+
public function sendResponseDataProvider(): array
67+
{
68+
return [
69+
[
70+
"content" => "<body><h1>Test Title</h1>" .
71+
"<script text=\"text/javascript\">test</script>" .
72+
"<script text=\"text/x-magento-template\">test</script>" .
73+
"<p>Test Content</p></body>",
74+
"flag" => true,
75+
"result" => "<body><h1>Test Title</h1>" .
76+
"<script text=\"text/x-magento-template\">test</script>" .
77+
"<p>Test Content</p>" .
78+
"<script text=\"text/javascript\">test</script>" .
79+
"\n</body>"
80+
],
81+
[
82+
"content" => "<body><p>Test Content</p></body>",
83+
"flag" => false,
84+
"result" => "<body><p>Test Content</p></body>"
85+
],
86+
[
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+
* Test BeforeSendResponse if content is not a string
126+
*
127+
* @return void
128+
*/
129+
public function testBeforeSendResponseIfGetContentIsNotAString(): void
130+
{
131+
$this->httpMock->expects($this->once())
132+
->method('getContent')
133+
->willReturn([]);
134+
135+
$this->scopeConfigMock->expects($this->never())
136+
->method('isSetFlag')
137+
->with(
138+
self::STUB_XML_PATH_DEV_MOVE_JS_TO_BOTTOM,
139+
ScopeInterface::SCOPE_STORE
140+
)
141+
->willReturn(false);
142+
143+
$this->plugin->beforeSendResponse($this->httpMock);
144+
}
145+
}

0 commit comments

Comments
 (0)