Skip to content

Commit 70122b5

Browse files
committed
2907: #2907: Integration Test Annotation magentoAppArea breaks with some valid values
1 parent 059c8b4 commit 70122b5

File tree

2 files changed

+210
-13
lines changed

2 files changed

+210
-13
lines changed

dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/Annotation/AppAreaTest.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Test\Annotation;
78

9+
use Magento\Framework\App\Area;
10+
811
class AppAreaTest extends \PHPUnit\Framework\TestCase
912
{
1013
/**
@@ -13,12 +16,12 @@ class AppAreaTest extends \PHPUnit\Framework\TestCase
1316
protected $_object;
1417

1518
/**
16-
* @var \PHPUnit_Framework_MockObject_MockObject
19+
* @var \Magento\TestFramework\Application|\PHPUnit_Framework_MockObject_MockObject
1720
*/
1821
protected $_applicationMock;
1922

2023
/**
21-
* @var \PHPUnit_Framework_MockObject_MockObject
24+
* @var \PHPUnit\Framework\TestCase|\PHPUnit_Framework_MockObject_MockObject
2225
*/
2326
protected $_testCaseMock;
2427

@@ -69,6 +72,22 @@ public function testGetTestAppAreaWithInvalidArea()
6972
$this->_object->startTest($this->_testCaseMock);
7073
}
7174

75+
/**
76+
* Check startTest() with different allowed area codes.
77+
*
78+
* @dataProvider startTestWithDifferentAreaCodes
79+
* @param string $areaCode
80+
*/
81+
public function testStartTestWithDifferentAreaCodes(string $areaCode)
82+
{
83+
$annotations = ['method' => ['magentoAppArea' => [$areaCode]]];
84+
$this->_testCaseMock->expects($this->once())->method('getAnnotations')->will($this->returnValue($annotations));
85+
$this->_applicationMock->expects($this->any())->method('getArea')->willReturn(null);
86+
$this->_applicationMock->expects($this->once())->method('reinitialize');
87+
$this->_applicationMock->expects($this->once())->method('loadArea')->with($areaCode);
88+
$this->_object->startTest($this->_testCaseMock);
89+
}
90+
7291
public function testStartTestPreventDoubleAreaLoadingAfterReinitialization()
7392
{
7493
$annotations = ['method' => ['magentoAppArea' => ['global']]];
@@ -89,4 +108,33 @@ public function testStartTestPreventDoubleAreaLoading()
89108
$this->_applicationMock->expects($this->never())->method('loadArea');
90109
$this->_object->startTest($this->_testCaseMock);
91110
}
111+
112+
/**
113+
* Provide test data for testStartTestWithDifferentAreaCodes().
114+
*
115+
* @return array
116+
*/
117+
public function startTestWithDifferentAreaCodes()
118+
{
119+
return [
120+
[
121+
'area_code' => Area::AREA_GLOBAL,
122+
],
123+
[
124+
'area_code' => Area::AREA_ADMINHTML,
125+
],
126+
[
127+
'area_code' => Area::AREA_FRONTEND,
128+
],
129+
[
130+
'area_code' => Area::AREA_WEBAPI_REST,
131+
],
132+
[
133+
'area_code' => Area::AREA_WEBAPI_SOAP,
134+
],
135+
[
136+
'area_code' => Area::AREA_CRONTAB,
137+
],
138+
];
139+
}
92140
}

dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/ApplicationTest.php

Lines changed: 160 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,71 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Test;
78

9+
use Magento\Framework\App\Area;
10+
use Magento\Framework\App\AreaList;
811
use Magento\Framework\App\Bootstrap;
12+
use Magento\Framework\App\ObjectManager\ConfigLoader;
913
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;
1019

20+
/**
21+
* Provide tests for \Magento\TestFramework\Application.
22+
*/
1123
class ApplicationTest extends \PHPUnit\Framework\TestCase
1224
{
1325
/**
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
1729
*/
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()
1941
{
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)
2246
->disableOriginalConstructor()->getMock();
23-
$tempDir = '/temp/dir';
47+
$this->tempDir = '/temp/dir';
2448
$appMode = \Magento\Framework\App\State::MODE_DEVELOPER;
2549

26-
$object = new \Magento\TestFramework\Application(
50+
$this->subject = new Application(
2751
$shell,
28-
$tempDir,
52+
$this->tempDir,
2953
'config.php',
3054
'global-config.php',
3155
'',
3256
$appMode,
3357
$autoloadWrapper
3458
);
59+
}
3560

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');
3769

38-
$initParams = $object->getInitParams();
70+
$initParams = $this->subject->getInitParams();
3971
$this->assertInternalType('array', $initParams, 'Wrong initialization parameters type');
4072
$this->assertArrayHasKey(
4173
Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS,
@@ -49,4 +81,121 @@ public function testConstructor()
4981
'Wrong application mode configured'
5082
);
5183
}
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+
}
52201
}

0 commit comments

Comments
 (0)