7
7
8
8
namespace Magento \Theme \Test \Unit \Controller \Result ;
9
9
10
- use Magento \Framework \App \Config \ScopeConfigInterface ;
11
- use Magento \Framework \App \Response \Http ;
12
- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
13
- use Magento \Store \Model \ScopeInterface ;
14
10
use Magento \Theme \Controller \Result \AsyncCssPlugin ;
15
- use PHPUnit \Framework \MockObject \ MockObject ;
11
+ use Magento \Framework \App \ Response \ Http ;
16
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 \View \Result \Layout ;
17
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
17
18
18
19
/**
19
20
* Unit test for Magento\Theme\Test\Unit\Controller\Result\AsyncCssPlugin.
@@ -37,6 +38,9 @@ class AsyncCssPluginTest extends TestCase
37
38
*/
38
39
private $ httpMock ;
39
40
41
+ /** @var Layout|MockObject */
42
+ private $ layoutMock ;
43
+
40
44
/**
41
45
* @inheritdoc
42
46
*/
@@ -48,6 +52,7 @@ protected function setUp(): void
48
52
->getMockForAbstractClass ();
49
53
50
54
$ this ->httpMock = $ this ->createMock (Http::class);
55
+ $ this ->layoutMock = $ this ->createMock (Layout::class);
51
56
52
57
$ objectManager = new ObjectManagerHelper ($ this );
53
58
$ this ->plugin = $ objectManager ->getObject (
@@ -59,87 +64,134 @@ protected function setUp(): void
59
64
}
60
65
61
66
/**
62
- * Data Provider for before send response
67
+ * Data Provider for testAfterRenderResult
63
68
*
64
69
* @return array
65
70
*/
66
- public function sendResponseDataProvider (): array
71
+ public function renderResultDataProvider (): array
67
72
{
68
73
return [
69
74
[
70
- "content " => "<body><h1>Test Title</h1> " .
71
- "<link rel= \"stylesheet \" href= \"css/critical.css \" /> " .
72
- "<p>Test Content</p></body> " ,
75
+ "content " => "<head><link rel= \"stylesheet \" href= \"css/async.css \"> " .
76
+ "<style>.critical-css{}</style> " .
77
+ "</head> " ,
78
+ "flag " => true ,
79
+ "result " => "<head><style>.critical-css{}</style> \n" .
80
+ "<link " .
81
+ "rel= \"stylesheet \" media= \"print \" onload= \"this.onload=null;this.media='all' \" " .
82
+ "href= \"css/async.css \"> \n" .
83
+ "</head> " ,
84
+ ],
85
+ [
86
+ "content " => "<head><link rel= \"stylesheet \" href= \"css/async.css \"> " .
87
+ "<link rel= \"preload \" href= \"other-file.html \"> " .
88
+ "</head> " ,
73
89
"flag " => true ,
74
- "result " => "<body><h1>Test Title</h1> " .
75
- "<link rel= \"preload \" as= \"style \" media= \"all \"" .
76
- " onload= \"this.onload=null;this.rel='stylesheet' \" href= \"css/critical.css \" /> " .
77
- "<p>Test Content</p> " .
78
- "<link rel= \"stylesheet \" href= \"css/critical.css \" /> " .
79
- "\n</body> "
90
+ "result " => "<head><link rel= \"preload \" href= \"other-file.html \"> \n" .
91
+ "<link " .
92
+ "rel= \"stylesheet \" media= \"print \" onload= \"this.onload=null;this.media='all' \" " .
93
+ "href= \"css/async.css \"> \n" .
94
+ "</head> " ,
80
95
],
81
96
[
82
- "content " => "<body><p>Test Content</p></body> " ,
97
+ "content " => "<head><link rel= \"stylesheet \" href= \"css/async.css \"> " .
98
+ "<link rel= \"preload \" href= \"other-file.html \"> " .
99
+ "</head> " ,
83
100
"flag " => false ,
84
- "result " => "<body><p>Test Content</p></body> "
101
+ "result " => "<head><link rel= \"stylesheet \" href= \"css/async.css \"> " .
102
+ "<link rel= \"preload \" href= \"other-file.html \"> " .
103
+ "</head> " ,
85
104
],
86
105
[
87
- "content " => "<body><p>Test Content</p></body> " ,
106
+ "content " => "<head><link rel= \"stylesheet \" href= \"css/first.css \"> " .
107
+ "<link rel= \"stylesheet \" href= \"css/second.css \"> " .
108
+ "<style>.critical-css{}</style> " .
109
+ "</head> " ,
88
110
"flag " => true ,
89
- "result " => "<body><p>Test Content</p></body> "
111
+ "result " => "<head><style>.critical-css{}</style> \n" .
112
+ "<link " .
113
+ "rel= \"stylesheet \" media= \"print \" onload= \"this.onload=null;this.media='all' \" " .
114
+ "href= \"css/first.css \"> \n" .
115
+ "<link " .
116
+ "rel= \"stylesheet \" media= \"print \" onload= \"this.onload=null;this.media='all' \" " .
117
+ "href= \"css/second.css \"> \n" .
118
+ "</head> " ,
119
+ ],
120
+ [
121
+ "content " => "<head><style>.critical-css{}</style></head> " ,
122
+ "flag " => false ,
123
+ "result " => "<head><style>.critical-css{}</style></head> "
124
+ ],
125
+ [
126
+ "content " => "<head><style>.critical-css{}</style></head> " ,
127
+ "flag " => true ,
128
+ "result " => "<head><style>.critical-css{}</style></head> "
90
129
]
91
130
];
92
131
}
93
132
94
133
/**
95
- * Test beforeSendResponse
134
+ * Test after render result response
96
135
*
97
136
* @param string $content
98
137
* @param bool $isSetFlag
99
138
* @param string $result
100
139
* @return void
101
- * @dataProvider sendResponseDataProvider
140
+ * @dataProvider renderResultDataProvider
102
141
*/
103
- public function testBeforeSendResponse ( $ content , $ isSetFlag , $ result ): void
142
+ public function testAfterRenderResult ( string $ content , bool $ isSetFlag , string $ result ): void
104
143
{
105
- $ this -> httpMock -> expects ( $ this -> once () )
106
- ->method ('getContent ' )
144
+ // Given (context )
145
+ $ this -> httpMock ->method ('getContent ' )
107
146
->willReturn ($ content );
108
147
109
- $ this ->scopeConfigMock ->expects ($ this ->once ())
110
- ->method ('isSetFlag ' )
111
- ->with (
112
- self ::STUB_XML_PATH_USE_CSS_CRITICAL_PATH ,
113
- ScopeInterface::SCOPE_STORE
114
- )
148
+ $ this ->scopeConfigMock ->method ('isSetFlag ' )
149
+ ->with (self ::STUB_XML_PATH_USE_CSS_CRITICAL_PATH , ScopeInterface::SCOPE_STORE )
115
150
->willReturn ($ isSetFlag );
116
151
152
+ // Expects
117
153
$ this ->httpMock ->expects ($ this ->any ())
118
154
->method ('setContent ' )
119
155
->with ($ result );
120
156
121
- $ this ->plugin ->beforeSendResponse ($ this ->httpMock );
157
+ // When
158
+ $ this ->plugin ->afterRenderResult ($ this ->layoutMock , $ this ->layoutMock , $ this ->httpMock );
122
159
}
123
160
124
161
/**
125
- * Test BeforeSendResponse if content is not a string
162
+ * Data Provider for testAfterRenderResultIfGetContentIsNotAString()
126
163
*
164
+ * @return array
165
+ */
166
+ public function ifGetContentIsNotAStringDataProvider (): array
167
+ {
168
+ return [
169
+ [
170
+ 'content ' => null
171
+ ]
172
+ ];
173
+ }
174
+
175
+ /**
176
+ * Test AfterRenderResult if content is not a string
177
+ *
178
+ * @param $content
127
179
* @return void
180
+ * @dataProvider ifGetContentIsNotAStringDataProvider
128
181
*/
129
- public function testIfGetContentIsNotAString ( ): void
182
+ public function testAfterRenderResultIfGetContentIsNotAString ( $ content ): void
130
183
{
184
+ $ this ->scopeConfigMock ->method ('isSetFlag ' )
185
+ ->with (self ::STUB_XML_PATH_USE_CSS_CRITICAL_PATH , ScopeInterface::SCOPE_STORE )
186
+ ->willReturn (true );
187
+
131
188
$ this ->httpMock ->expects ($ this ->once ())
132
189
->method ('getContent ' )
133
- ->willReturn ([] );
190
+ ->willReturn ($ content );
134
191
135
- $ this ->scopeConfigMock ->expects ($ this ->any ())
136
- ->method ('isSetFlag ' )
137
- ->with (
138
- self ::STUB_XML_PATH_USE_CSS_CRITICAL_PATH ,
139
- ScopeInterface::SCOPE_STORE
140
- )
141
- ->willReturn (false );
192
+ $ this ->httpMock ->expects ($ this ->never ())
193
+ ->method ('setContent ' );
142
194
143
- $ this ->plugin ->beforeSendResponse ( $ this ->httpMock );
195
+ $ this ->plugin ->afterRenderResult ( $ this -> layoutMock , $ this -> layoutMock , $ this ->httpMock );
144
196
}
145
197
}
0 commit comments