Skip to content

Commit

Permalink
2907: #2907: Integration Test Annotation magentoAppArea breaks with s…
Browse files Browse the repository at this point in the history
…ome valid values
  • Loading branch information
nmalevanec committed Dec 12, 2017
1 parent 059c8b4 commit 70122b5
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 13 deletions.
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,121 @@ public function testConstructor()
'Wrong application mode configured'
);
}

/**
* Test \Magento\TestFramework\Application will correctly load different areas.
*
* @dataProvider loadAreaDataProvider
*
* @param string $areaCode
* @param bool $partialLoad
*/
public function testLoadArea(string $areaCode, bool $partialLoad)
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject $objectManagerMock */
$objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class)
->disableOriginalConstructor()
->getMock();
$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([]);
$areaList = $this->getMockBuilder(AreaList::class)
->disableOriginalConstructor()
->getMock();
$area = $this->getMockBuilder(Area::class)
->disableOriginalConstructor()
->getMock();
$appState = $this->getMockBuilder(State::class)
->disableOriginalConstructor()
->getMock();
$objectManagerMock->expects($this->once())
->method('configure')
->with($this->identicalTo([]));
if ($partialLoad) {
$objectManagerMock->expects($this->exactly(3))
->method('get')
->willReturnOnConsecutiveCalls(
$configScope,
$configLoader,
$areaList
);
$areaList->expects($this->once())
->method('getArea')
->with($this->identicalTo($areaCode))
->willReturn($area);
$area->expects($this->once())
->method('load')
->with($this->identicalTo(Area::PART_CONFIG));
} else {
$area->expects($this->once())
->method('load');
$appState->expects($this->once())
->method('setAreaCode')
->with($this->identicalTo($areaCode));
$areaList->expects($this->once())
->method('getArea')
->with($this->identicalTo($areaCode))
->willReturn($area);
$objectManagerMock->expects($this->exactly(5))
->method('get')
->willReturnOnConsecutiveCalls(
$configScope,
$configLoader,
$areaList,
$appState,
$areaList
);
}
\Magento\TestFramework\Helper\Bootstrap::setObjectManager($objectManagerMock);
$this->subject->loadArea($areaCode);

//restore Object Manager to successfully finish the test.
\Magento\TestFramework\Helper\Bootstrap::setObjectManager($objectManager);
}

/**
* Provide test data for testLoadArea().
*
* @return array
*/
public function loadAreaDataProvider()
{
return [
[
'area_code' => Area::AREA_GLOBAL,
'partial_load' => true,
],
[
'area_code' => Area::AREA_ADMINHTML,
'partial_load' => false,
],
[
'area_code' => Area::AREA_FRONTEND,
'partial_load' => false,
],
[
'area_code' => Area::AREA_WEBAPI_REST,
'partial_load' => true,
],
[
'area_code' => Area::AREA_WEBAPI_SOAP,
'partial_load' => true,
],
[
'area_code' => Area::AREA_CRONTAB,
'partial_load' => true,
],
];
}
}

0 comments on commit 70122b5

Please sign in to comment.