Skip to content

Commit 979f1d9

Browse files
committed
MAGETWO-86568: Integration Test Annotation magentoAppArea breaks with some valid values[forwardport]. #1194
- Merge Pull Request magento-engcom/magento2ce#1194 from nmalevanec/magento2:996 - Merged commits: 1. d7f13d0
2 parents e150a7e + d7f13d0 commit 979f1d9

File tree

4 files changed

+188
-19
lines changed

4 files changed

+188
-19
lines changed

dev/tests/integration/framework/Magento/TestFramework/Annotation/AppArea.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ class AppArea
1515
private $_application;
1616

1717
/**
18-
* List of allowed areas
18+
* List of allowed areas.
1919
*
2020
* @var array
2121
*/
2222
private $_allowedAreas = [
2323
\Magento\Framework\App\Area::AREA_GLOBAL,
24-
\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
24+
\Magento\Framework\App\Area::AREA_ADMINHTML,
2525
\Magento\Framework\App\Area::AREA_FRONTEND,
26-
'webapi_rest',
27-
'webapi_soap',
28-
'cron',
26+
\Magento\Framework\App\Area::AREA_WEBAPI_REST,
27+
\Magento\Framework\App\Area::AREA_WEBAPI_SOAP,
28+
\Magento\Framework\App\Area::AREA_CRONTAB,
2929
];
3030

3131
/**

dev/tests/integration/framework/Magento/TestFramework/Application.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ public function getArea()
604604
*
605605
* @param string $areaCode
606606
* @return void
607+
* @throws \Magento\Framework\Exception\LocalizedException
607608
*/
608609
public function loadArea($areaCode)
609610
{
@@ -620,7 +621,13 @@ public function loadArea($areaCode)
620621
)
621622
);
622623
$app = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\App\AreaList::class);
623-
if ($areaCode == \Magento\TestFramework\Application::DEFAULT_APP_AREA) {
624+
$areasForPartialLoading = [
625+
\Magento\Framework\App\Area::AREA_GLOBAL,
626+
\Magento\Framework\App\Area::AREA_WEBAPI_REST,
627+
\Magento\Framework\App\Area::AREA_WEBAPI_SOAP,
628+
\Magento\Framework\App\Area::AREA_CRONTAB,
629+
];
630+
if (in_array($areaCode, $areasForPartialLoading, true)) {
624631
$app->getArea($areaCode)->load(\Magento\Framework\App\Area::PART_CONFIG);
625632
} else {
626633
\Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea($areaCode);

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: 125 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,86 @@ public function testConstructor()
4981
'Wrong application mode configured'
5082
);
5183
}
84+
85+
/**
86+
* Test \Magento\TestFramework\Application will correctly load specified areas.
87+
*
88+
* @dataProvider partialLoadAreaDataProvider
89+
* @param string $areaCode
90+
* @return void
91+
*/
92+
public function testPartialLoadArea(string $areaCode)
93+
{
94+
$configScope = $this->getMockBuilder(Scope::class)
95+
->disableOriginalConstructor()
96+
->getMock();
97+
$configScope->expects($this->once())
98+
->method('setCurrentScope')
99+
->with($this->identicalTo($areaCode));
100+
101+
$configLoader = $this->getMockBuilder(ConfigLoader::class)
102+
->disableOriginalConstructor()
103+
->getMock();
104+
$configLoader->expects($this->once())
105+
->method('load')
106+
->with($this->identicalTo($areaCode))
107+
->willReturn([]);
108+
109+
$area = $this->getMockBuilder(Area::class)
110+
->disableOriginalConstructor()
111+
->getMock();
112+
$area->expects($this->once())
113+
->method('load')
114+
->with($this->identicalTo(Area::PART_CONFIG));
115+
116+
$areaList = $this->getMockBuilder(AreaList::class)
117+
->disableOriginalConstructor()
118+
->getMock();
119+
$areaList->expects($this->once())
120+
->method('getArea')
121+
->with($this->identicalTo($areaCode))
122+
->willReturn($area);
123+
124+
/** @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject $objectManager */
125+
$objectManager = $this->getMockBuilder(ObjectManagerInterface::class)
126+
->disableOriginalConstructor()
127+
->getMock();
128+
$objectManager->expects($this->once())
129+
->method('configure')
130+
->with($this->identicalTo([]));
131+
$objectManager->expects($this->exactly(3))
132+
->method('get')
133+
->willReturnOnConsecutiveCalls(
134+
$configScope,
135+
$configLoader,
136+
$areaList
137+
);
138+
139+
\Magento\TestFramework\Helper\Bootstrap::setObjectManager($objectManager);
140+
141+
$this->subject->loadArea($areaCode);
142+
}
143+
144+
/**
145+
* Provide test data for testPartialLoadArea().
146+
*
147+
* @return array
148+
*/
149+
public function partialLoadAreaDataProvider()
150+
{
151+
return [
152+
[
153+
'area_code' => Area::AREA_GLOBAL,
154+
],
155+
[
156+
'area_code' => Area::AREA_WEBAPI_REST,
157+
],
158+
[
159+
'area_code' => Area::AREA_WEBAPI_SOAP,
160+
],
161+
[
162+
'area_code' => Area::AREA_CRONTAB,
163+
],
164+
];
165+
}
52166
}

0 commit comments

Comments
 (0)