Skip to content

Commit

Permalink
MAGETWO-85537: 2907: Integration Test Annotation magentoAppArea break…
Browse files Browse the repository at this point in the history
…s with some valid values. #996

 - Merge Pull Request magento-engcom/magento2ce#996 from nmalevanec/magento2:2907
 - Merged commits:
   1. 1cd27f4
   2. 059c8b4
   3. 70122b5
   4. baaff95
   5. 6b749c0
   6. 7be1e58
  • Loading branch information
Stanislav Idolov committed Dec 12, 2017
2 parents b9ab1ea + 7be1e58 commit 3f949d7
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ class AppArea
private $_application;

/**
* List of allowed areas
* List of allowed areas.
*
* @var array
*/
private $_allowedAreas = [
\Magento\Framework\App\Area::AREA_GLOBAL,
\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
\Magento\Framework\App\Area::AREA_ADMINHTML,
\Magento\Framework\App\Area::AREA_FRONTEND,
'webapi_rest',
'webapi_soap',
'cron',
\Magento\Framework\App\Area::AREA_WEBAPI_REST,
\Magento\Framework\App\Area::AREA_WEBAPI_SOAP,
\Magento\Framework\App\Area::AREA_CRONTAB,
];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ public function getArea()
*
* @param string $areaCode
* @return void
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function loadArea($areaCode)
{
Expand All @@ -616,7 +617,13 @@ public function loadArea($areaCode)
)
);
$app = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\App\AreaList::class);
if ($areaCode == \Magento\TestFramework\Application::DEFAULT_APP_AREA) {
$areasForPartialLoading = [
\Magento\Framework\App\Area::AREA_GLOBAL,
\Magento\Framework\App\Area::AREA_WEBAPI_REST,
\Magento\Framework\App\Area::AREA_WEBAPI_SOAP,
\Magento\Framework\App\Area::AREA_CRONTAB,
];
if (in_array($areaCode, $areasForPartialLoading, true)) {
$app->getArea($areaCode)->load(\Magento\Framework\App\Area::PART_CONFIG);
} else {
\Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea($areaCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Test\Annotation;

use Magento\Framework\App\Area;

class AppAreaTest extends \PHPUnit\Framework\TestCase
{
/**
Expand All @@ -13,12 +16,12 @@ class AppAreaTest extends \PHPUnit\Framework\TestCase
protected $_object;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var \Magento\TestFramework\Application|\PHPUnit_Framework_MockObject_MockObject
*/
protected $_applicationMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var \PHPUnit\Framework\TestCase|\PHPUnit_Framework_MockObject_MockObject
*/
protected $_testCaseMock;

Expand Down Expand Up @@ -69,6 +72,22 @@ public function testGetTestAppAreaWithInvalidArea()
$this->_object->startTest($this->_testCaseMock);
}

/**
* Check startTest() with different allowed area codes.
*
* @dataProvider startTestWithDifferentAreaCodes
* @param string $areaCode
*/
public function testStartTestWithDifferentAreaCodes(string $areaCode)
{
$annotations = ['method' => ['magentoAppArea' => [$areaCode]]];
$this->_testCaseMock->expects($this->once())->method('getAnnotations')->will($this->returnValue($annotations));
$this->_applicationMock->expects($this->any())->method('getArea')->willReturn(null);
$this->_applicationMock->expects($this->once())->method('reinitialize');
$this->_applicationMock->expects($this->once())->method('loadArea')->with($areaCode);
$this->_object->startTest($this->_testCaseMock);
}

public function testStartTestPreventDoubleAreaLoadingAfterReinitialization()
{
$annotations = ['method' => ['magentoAppArea' => ['global']]];
Expand All @@ -89,4 +108,33 @@ public function testStartTestPreventDoubleAreaLoading()
$this->_applicationMock->expects($this->never())->method('loadArea');
$this->_object->startTest($this->_testCaseMock);
}

/**
* Provide test data for testStartTestWithDifferentAreaCodes().
*
* @return array
*/
public function startTestWithDifferentAreaCodes()
{
return [
[
'area_code' => Area::AREA_GLOBAL,
],
[
'area_code' => Area::AREA_ADMINHTML,
],
[
'area_code' => Area::AREA_FRONTEND,
],
[
'area_code' => Area::AREA_WEBAPI_REST,
],
[
'area_code' => Area::AREA_WEBAPI_SOAP,
],
[
'area_code' => Area::AREA_CRONTAB,
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,71 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Test;

use Magento\Framework\App\Area;
use Magento\Framework\App\AreaList;
use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\ObjectManager\ConfigLoader;
use Magento\Framework\App\State;
use Magento\Framework\Autoload\ClassLoaderWrapper;
use Magento\Framework\Config\Scope;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Shell;
use Magento\TestFramework\Application;

/**
* Provide tests for \Magento\TestFramework\Application.
*/
class ApplicationTest extends \PHPUnit\Framework\TestCase
{
/**
* @covers \Magento\TestFramework\Application::getTempDir
* @covers \Magento\TestFramework\Application::getDbInstance()
* @covers \Magento\TestFramework\Application::getInitParams()
* Test subject.
*
* @var Application
*/
public function testConstructor()
private $subject;

/**
* @var string
*/
private $tempDir;

/**
* @inheritdoc
*/
protected function setUp()
{
$shell = $this->createMock(\Magento\Framework\Shell::class);
$autoloadWrapper = $this->getMockBuilder(\Magento\Framework\Autoload\ClassLoaderWrapper::class)
/** @var Shell|\PHPUnit_Framework_MockObject_MockObject $shell */
$shell = $this->createMock(Shell::class);
/** @var ClassLoaderWrapper|\PHPUnit_Framework_MockObject_MockObject $autoloadWrapper */
$autoloadWrapper = $this->getMockBuilder(ClassLoaderWrapper::class)
->disableOriginalConstructor()->getMock();
$tempDir = '/temp/dir';
$this->tempDir = '/temp/dir';
$appMode = \Magento\Framework\App\State::MODE_DEVELOPER;

$object = new \Magento\TestFramework\Application(
$this->subject = new Application(
$shell,
$tempDir,
$this->tempDir,
'config.php',
'global-config.php',
'',
$appMode,
$autoloadWrapper
);
}

$this->assertEquals($tempDir, $object->getTempDir(), 'Temp directory is not set in Application');
/**
* @covers \Magento\TestFramework\Application::getTempDir
* @covers \Magento\TestFramework\Application::getDbInstance()
* @covers \Magento\TestFramework\Application::getInitParams()
*/
public function testConstructor()
{
$this->assertEquals($this->tempDir, $this->subject->getTempDir(), 'Temp directory is not set in Application');

$initParams = $object->getInitParams();
$initParams = $this->subject->getInitParams();
$this->assertInternalType('array', $initParams, 'Wrong initialization parameters type');
$this->assertArrayHasKey(
Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS,
Expand All @@ -49,4 +81,85 @@ public function testConstructor()
'Wrong application mode configured'
);
}

/**
* Test \Magento\TestFramework\Application will correctly load specified areas.
*
* @dataProvider partialLoadAreaDataProvider
* @param string $areaCode
*/
public function testPartialLoadArea(string $areaCode)
{
$configScope = $this->getMockBuilder(Scope::class)
->disableOriginalConstructor()
->getMock();
$configScope->expects($this->once())
->method('setCurrentScope')
->with($this->identicalTo($areaCode));

$configLoader = $this->getMockBuilder(ConfigLoader::class)
->disableOriginalConstructor()
->getMock();
$configLoader->expects($this->once())
->method('load')
->with($this->identicalTo($areaCode))
->willReturn([]);

$area = $this->getMockBuilder(Area::class)
->disableOriginalConstructor()
->getMock();
$area->expects($this->once())
->method('load')
->with($this->identicalTo(Area::PART_CONFIG));

$areaList = $this->getMockBuilder(AreaList::class)
->disableOriginalConstructor()
->getMock();
$areaList->expects($this->once())
->method('getArea')
->with($this->identicalTo($areaCode))
->willReturn($area);

/** @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject $objectManager */
$objectManager = $this->getMockBuilder(ObjectManagerInterface::class)
->disableOriginalConstructor()
->getMock();
$objectManager->expects($this->once())
->method('configure')
->with($this->identicalTo([]));
$objectManager->expects($this->exactly(3))
->method('get')
->willReturnOnConsecutiveCalls(
$configScope,
$configLoader,
$areaList
);

\Magento\TestFramework\Helper\Bootstrap::setObjectManager($objectManager);

$this->subject->loadArea($areaCode);
}

/**
* Provide test data for testPartialLoadArea().
*
* @return array
*/
public function partialLoadAreaDataProvider()
{
return [
[
'area_code' => Area::AREA_GLOBAL,
],
[
'area_code' => Area::AREA_WEBAPI_REST,
],
[
'area_code' => Area::AREA_WEBAPI_SOAP,
],
[
'area_code' => Area::AREA_CRONTAB,
],
];
}
}

0 comments on commit 3f949d7

Please sign in to comment.