Skip to content

Commit bf21c4b

Browse files
committed
covered untit & updated loadcssasync format
1 parent f9697f5 commit bf21c4b

File tree

2 files changed

+145
-2
lines changed

2 files changed

+145
-2
lines changed

app/code/Magento/Theme/Controller/Result/AsyncCssPlugin.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
class AsyncCssPlugin
1818
{
19-
private const XML_PATH_USE_CSS_CRITICAL_PATH = 'dev/css/use_css_critical_path';
19+
const XML_PATH_USE_CSS_CRITICAL_PATH = 'dev/css/use_css_critical_path';
2020

2121
/**
2222
* @var ScopeConfigInterface
@@ -60,7 +60,7 @@ function ($matches) use (&$cssMatches) {
6060
$loadCssAsync = sprintf(
6161
'<link rel="preload" as="style" media="%s"' .
6262
' onload="this.onload=null;this.rel=\'stylesheet\'"' .
63-
' href="%s">',
63+
' href="%s" />',
6464
$media,
6565
$href
6666
);
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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\AsyncCssPlugin;
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\AsyncCssPlugin.
20+
*/
21+
class AsyncCssPluginTest extends TestCase
22+
{
23+
/**
24+
* @var AsyncCssPlugin
25+
*/
26+
private $plugin;
27+
28+
/**
29+
* @var ScopeConfigInterface|MockObject
30+
*/
31+
private $scopeConfigMock;
32+
33+
/**
34+
* @var Http|MockObject
35+
*/
36+
private $httpMock;
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
protected function setUp(): void
42+
{
43+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
44+
->setMethods(['isSetFlag'])
45+
->disableOriginalConstructor()
46+
->getMockForAbstractClass();
47+
48+
$this->httpMock = $this->createMock(Http::class);
49+
50+
$objectManager = new ObjectManagerHelper($this);
51+
$this->plugin = $objectManager->getObject(
52+
AsyncCssPlugin::class,
53+
[
54+
'scopeConfig' => $this->scopeConfigMock
55+
]
56+
);
57+
}
58+
59+
/**
60+
* Data Provider for before send response
61+
*
62+
* @return void
63+
*/
64+
public function sendResponseDataProvider()
65+
{
66+
return [
67+
[
68+
"content" => "<body><h1>Test Title</h1>" .
69+
"<link rel=\"stylesheet\" href=\"css/critical.css\" />" .
70+
"<p>Test Content</p></body>",
71+
"flag" => true,
72+
"result" => "<body><h1>Test Title</h1>" .
73+
"<link rel=\"preload\" as=\"style\" media=\"all\"" .
74+
" onload=\"this.onload=null;this.rel='stylesheet'\" href=\"css/critical.css\" />" .
75+
"<p>Test Content</p>" .
76+
"<link rel=\"stylesheet\" href=\"css/critical.css\" />" .
77+
"\n</body>"
78+
],
79+
[
80+
"content" => "<body><p>Test Content</p></body>",
81+
"flag" => false,
82+
"result" => "<body><p>Test Content</p></body>"
83+
],
84+
[
85+
"content" => "<body><p>Test Content</p></body>",
86+
"flag" => true,
87+
"result" => "<body><p>Test Content</p></body>"
88+
]
89+
];
90+
}
91+
92+
/**
93+
* Test beforeSendResponse
94+
*
95+
* @param string $content
96+
* @param bool $isSetFlag
97+
* @param string $result
98+
* @return void
99+
* @dataProvider sendResponseDataProvider
100+
*/
101+
public function testBeforeSendResponse($content, $isSetFlag, $result): void
102+
{
103+
$this->httpMock->expects($this->once())
104+
->method('getContent')
105+
->willReturn($content);
106+
107+
$this->scopeConfigMock->expects($this->once())
108+
->method('isSetFlag')
109+
->with(
110+
AsyncCssPlugin::XML_PATH_USE_CSS_CRITICAL_PATH,
111+
ScopeInterface::SCOPE_STORE
112+
)
113+
->willReturn($isSetFlag);
114+
115+
$this->httpMock->expects($this->any())
116+
->method('setContent')
117+
->with($result);
118+
119+
$this->plugin->beforeSendResponse($this->httpMock);
120+
}
121+
122+
/**
123+
* Test BeforeSendResponse if content is not a string
124+
*
125+
* @return void
126+
*/
127+
public function testIfGetContentIsNotAString(): void
128+
{
129+
$this->httpMock->expects($this->once())
130+
->method('getContent')
131+
->willReturn([]);
132+
133+
$this->scopeConfigMock->expects($this->any())
134+
->method('isSetFlag')
135+
->with(
136+
AsyncCssPlugin::XML_PATH_USE_CSS_CRITICAL_PATH,
137+
ScopeInterface::SCOPE_STORE
138+
)
139+
->willReturn(false);
140+
141+
$this->plugin->beforeSendResponse($this->httpMock);
142+
}
143+
}

0 commit comments

Comments
 (0)