From 70122b57918fd8709f48afedbc85c1de2802a71b Mon Sep 17 00:00:00 2001 From: nmalevanec Date: Tue, 12 Dec 2017 14:47:14 +0200 Subject: [PATCH] 2907: magento/magento2#2907: Integration Test Annotation magentoAppArea breaks with some valid values --- .../Magento/Test/Annotation/AppAreaTest.php | 52 +++++- .../Magento/Test/ApplicationTest.php | 171 ++++++++++++++++-- 2 files changed, 210 insertions(+), 13 deletions(-) diff --git a/dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/Annotation/AppAreaTest.php b/dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/Annotation/AppAreaTest.php index a4c8816b13ee1..dd361a5d0ed74 100644 --- a/dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/Annotation/AppAreaTest.php +++ b/dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/Annotation/AppAreaTest.php @@ -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 { /** @@ -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; @@ -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']]]; @@ -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, + ], + ]; + } } diff --git a/dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/ApplicationTest.php b/dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/ApplicationTest.php index f8f49613c41b4..ee794fc262a4d 100644 --- a/dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/ApplicationTest.php +++ b/dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/ApplicationTest.php @@ -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, @@ -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, + ], + ]; + } }