3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \Test ;
7
8
9
+ use Magento \Framework \App \Area ;
10
+ use Magento \Framework \App \AreaList ;
8
11
use Magento \Framework \App \Bootstrap ;
12
+ use Magento \Framework \App \ObjectManager \ConfigLoader ;
9
13
use Magento \Framework \App \State ;
14
+ use Magento \Framework \Autoload \ClassLoaderWrapper ;
15
+ use Magento \Framework \Config \Scope ;
16
+ use Magento \Framework \ObjectManagerInterface ;
17
+ use Magento \Framework \Shell ;
18
+ use Magento \TestFramework \Application ;
10
19
20
+ /**
21
+ * Provide tests for \Magento\TestFramework\Application.
22
+ */
11
23
class ApplicationTest extends \PHPUnit \Framework \TestCase
12
24
{
13
25
/**
14
- * @covers \Magento\TestFramework\Application::getTempDir
15
- * @covers \Magento\TestFramework\Application::getDbInstance()
16
- * @covers \Magento\TestFramework\ Application::getInitParams()
26
+ * Test subject.
27
+ *
28
+ * @var Application
17
29
*/
18
- public function testConstructor ()
30
+ private $ subject ;
31
+
32
+ /**
33
+ * @var string
34
+ */
35
+ private $ tempDir ;
36
+
37
+ /**
38
+ * @inheritdoc
39
+ */
40
+ protected function setUp ()
19
41
{
20
- $ shell = $ this ->createMock (\Magento \Framework \Shell::class);
21
- $ autoloadWrapper = $ this ->getMockBuilder (\Magento \Framework \Autoload \ClassLoaderWrapper::class)
42
+ /** @var Shell|\PHPUnit_Framework_MockObject_MockObject $shell */
43
+ $ shell = $ this ->createMock (Shell::class);
44
+ /** @var ClassLoaderWrapper|\PHPUnit_Framework_MockObject_MockObject $autoloadWrapper */
45
+ $ autoloadWrapper = $ this ->getMockBuilder (ClassLoaderWrapper::class)
22
46
->disableOriginalConstructor ()->getMock ();
23
- $ tempDir = '/temp/dir ' ;
47
+ $ this -> tempDir = '/temp/dir ' ;
24
48
$ appMode = \Magento \Framework \App \State::MODE_DEVELOPER ;
25
49
26
- $ object = new \ Magento \ TestFramework \ Application (
50
+ $ this -> subject = new Application (
27
51
$ shell ,
28
- $ tempDir ,
52
+ $ this -> tempDir ,
29
53
'config.php ' ,
30
54
'global-config.php ' ,
31
55
'' ,
32
56
$ appMode ,
33
57
$ autoloadWrapper
34
58
);
59
+ }
35
60
36
- $ this ->assertEquals ($ tempDir , $ object ->getTempDir (), 'Temp directory is not set in Application ' );
61
+ /**
62
+ * @covers \Magento\TestFramework\Application::getTempDir
63
+ * @covers \Magento\TestFramework\Application::getDbInstance()
64
+ * @covers \Magento\TestFramework\Application::getInitParams()
65
+ */
66
+ public function testConstructor ()
67
+ {
68
+ $ this ->assertEquals ($ this ->tempDir , $ this ->subject ->getTempDir (), 'Temp directory is not set in Application ' );
37
69
38
- $ initParams = $ object ->getInitParams ();
70
+ $ initParams = $ this -> subject ->getInitParams ();
39
71
$ this ->assertInternalType ('array ' , $ initParams , 'Wrong initialization parameters type ' );
40
72
$ this ->assertArrayHasKey (
41
73
Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS ,
@@ -49,4 +81,121 @@ public function testConstructor()
49
81
'Wrong application mode configured '
50
82
);
51
83
}
84
+
85
+ /**
86
+ * Test \Magento\TestFramework\Application will correctly load different areas.
87
+ *
88
+ * @dataProvider loadAreaDataProvider
89
+ *
90
+ * @param string $areaCode
91
+ * @param bool $partialLoad
92
+ */
93
+ public function testLoadArea (string $ areaCode , bool $ partialLoad )
94
+ {
95
+ $ objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
96
+ /** @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject $objectManagerMock */
97
+ $ objectManagerMock = $ this ->getMockBuilder (ObjectManagerInterface::class)
98
+ ->disableOriginalConstructor ()
99
+ ->getMock ();
100
+ $ configScope = $ this ->getMockBuilder (Scope::class)
101
+ ->disableOriginalConstructor ()
102
+ ->getMock ();
103
+ $ configScope ->expects ($ this ->once ())
104
+ ->method ('setCurrentScope ' )
105
+ ->with ($ this ->identicalTo ($ areaCode ));
106
+ $ configLoader = $ this ->getMockBuilder (ConfigLoader::class)
107
+ ->disableOriginalConstructor ()
108
+ ->getMock ();
109
+ $ configLoader ->expects ($ this ->once ())
110
+ ->method ('load ' )
111
+ ->with ($ this ->identicalTo ($ areaCode ))
112
+ ->willReturn ([]);
113
+ $ areaList = $ this ->getMockBuilder (AreaList::class)
114
+ ->disableOriginalConstructor ()
115
+ ->getMock ();
116
+ $ area = $ this ->getMockBuilder (Area::class)
117
+ ->disableOriginalConstructor ()
118
+ ->getMock ();
119
+ $ appState = $ this ->getMockBuilder (State::class)
120
+ ->disableOriginalConstructor ()
121
+ ->getMock ();
122
+ $ objectManagerMock ->expects ($ this ->once ())
123
+ ->method ('configure ' )
124
+ ->with ($ this ->identicalTo ([]));
125
+ if ($ partialLoad ) {
126
+ $ objectManagerMock ->expects ($ this ->exactly (3 ))
127
+ ->method ('get ' )
128
+ ->willReturnOnConsecutiveCalls (
129
+ $ configScope ,
130
+ $ configLoader ,
131
+ $ areaList
132
+ );
133
+ $ areaList ->expects ($ this ->once ())
134
+ ->method ('getArea ' )
135
+ ->with ($ this ->identicalTo ($ areaCode ))
136
+ ->willReturn ($ area );
137
+ $ area ->expects ($ this ->once ())
138
+ ->method ('load ' )
139
+ ->with ($ this ->identicalTo (Area::PART_CONFIG ));
140
+ } else {
141
+ $ area ->expects ($ this ->once ())
142
+ ->method ('load ' );
143
+ $ appState ->expects ($ this ->once ())
144
+ ->method ('setAreaCode ' )
145
+ ->with ($ this ->identicalTo ($ areaCode ));
146
+ $ areaList ->expects ($ this ->once ())
147
+ ->method ('getArea ' )
148
+ ->with ($ this ->identicalTo ($ areaCode ))
149
+ ->willReturn ($ area );
150
+ $ objectManagerMock ->expects ($ this ->exactly (5 ))
151
+ ->method ('get ' )
152
+ ->willReturnOnConsecutiveCalls (
153
+ $ configScope ,
154
+ $ configLoader ,
155
+ $ areaList ,
156
+ $ appState ,
157
+ $ areaList
158
+ );
159
+ }
160
+ \Magento \TestFramework \Helper \Bootstrap::setObjectManager ($ objectManagerMock );
161
+ $ this ->subject ->loadArea ($ areaCode );
162
+
163
+ //restore Object Manager to successfully finish the test.
164
+ \Magento \TestFramework \Helper \Bootstrap::setObjectManager ($ objectManager );
165
+ }
166
+
167
+ /**
168
+ * Provide test data for testLoadArea().
169
+ *
170
+ * @return array
171
+ */
172
+ public function loadAreaDataProvider ()
173
+ {
174
+ return [
175
+ [
176
+ 'area_code ' => Area::AREA_GLOBAL ,
177
+ 'partial_load ' => true ,
178
+ ],
179
+ [
180
+ 'area_code ' => Area::AREA_ADMINHTML ,
181
+ 'partial_load ' => false ,
182
+ ],
183
+ [
184
+ 'area_code ' => Area::AREA_FRONTEND ,
185
+ 'partial_load ' => false ,
186
+ ],
187
+ [
188
+ 'area_code ' => Area::AREA_WEBAPI_REST ,
189
+ 'partial_load ' => true ,
190
+ ],
191
+ [
192
+ 'area_code ' => Area::AREA_WEBAPI_SOAP ,
193
+ 'partial_load ' => true ,
194
+ ],
195
+ [
196
+ 'area_code ' => Area::AREA_CRONTAB ,
197
+ 'partial_load ' => true ,
198
+ ],
199
+ ];
200
+ }
52
201
}
0 commit comments