diff --git a/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php b/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php index 5db3c2e41c04adf9bf5f2105df266167c5a77364..f6ddb41abf583f96a2b791c7c382423cf02ff64c 100644 --- a/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php +++ b/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php @@ -12,6 +12,11 @@ namespace Magento\Backend\Test\Unit\Model\Session; */ class QuoteTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */ @@ -105,6 +110,7 @@ class QuoteTest extends \PHPUnit_Framework_TestCase */ protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->customerRepositoryMock = $this->getMockForAbstractClass( \Magento\Customer\Api\CustomerRepositoryInterface::class, [], @@ -227,11 +233,16 @@ class QuoteTest extends \PHPUnit_Framework_TestCase ] ); - $this->prepareObjectManager([ - [\Magento\Quote\Api\CartManagementInterface::class, $this->cartManagementMock] + $this->objectManager->mockObjectManager([ + \Magento\Quote\Api\CartManagementInterface::class => $this->cartManagementMock ]); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + /** * Run test getQuote method * @@ -416,19 +427,4 @@ class QuoteTest extends \PHPUnit_Framework_TestCase 'customer ids same' => [66, 66, 'never'], ]; } - - /** - * @param array $map - * @deprecated - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any())->method('get')->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php index b97b7d2f9842355af1195b7b0ce7ed1d4aa7e4e8..5f78d3dba0f60ede41672077f6e79600f7032648 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CollectionTest.php @@ -3,18 +3,18 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Product; -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; - /** - * Class CollectionTest - * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class CollectionTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -55,6 +55,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase */ protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $entityFactory = $this->getMock(\Magento\Framework\Data\Collection\EntityFactory::class, [], [], '', false); $logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class) ->disableOriginalConstructor() @@ -145,27 +146,17 @@ class CollectionTest extends \PHPUnit_Framework_TestCase $this->entityMock->expects($this->once())->method('getDefaultAttributes')->willReturn([]); $this->entityMock->expects($this->any())->method('getTable')->willReturnArgument(0); $this->connectionMock->expects($this->atLeastOnce())->method('select')->willReturn($this->selectMock); - $helper = new ObjectManager($this); - $this->prepareObjectManager([ - [ - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class, - $this->getMock(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class) - ], - [ - \Magento\Catalog\Model\ResourceModel\Product\Gallery::class, - $this->galleryResourceMock - ], - [ - \Magento\Framework\EntityManager\MetadataPool::class, - $this->metadataPoolMock - ], - [ - \Magento\Catalog\Model\Product\Gallery\ReadHandler::class, - $this->galleryReadHandlerMock - ] + $productLimitationMock = $this->getMock( + \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class + ); + $this->objectManager->mockObjectManager([ + \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class => $productLimitationMock, + \Magento\Catalog\Model\ResourceModel\Product\Gallery::class => $this->galleryResourceMock, + \Magento\Framework\EntityManager\MetadataPool::class => $this->metadataPoolMock, + \Magento\Catalog\Model\Product\Gallery\ReadHandler::class => $this->galleryReadHandlerMock ]); - $this->collection = $helper->getObject( + $this->collection = $this->objectManager->getObject( \Magento\Catalog\Model\ResourceModel\Product\Collection::class, [ 'entityFactory' => $entityFactory, @@ -193,6 +184,11 @@ class CollectionTest extends \PHPUnit_Framework_TestCase $this->collection->setConnection($this->connectionMock); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + public function testAddProductCategoriesFilter() { $condition = ['in' => [1, 2]]; @@ -259,20 +255,4 @@ class CollectionTest extends \PHPUnit_Framework_TestCase $this->assertSame($this->collection, $this->collection->addMediaGalleryData()); } - - /** - * @param $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php index 3c92cde30012dc1a4fdc4736f1c68c6a7b071d5b..11eb542773d59de25de891bff631ad96ee8c1331 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Link/Product/CollectionTest.php @@ -3,11 +3,8 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Product\Link\Product; -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; - /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.TooManyFields) @@ -17,8 +14,8 @@ class CollectionTest extends \PHPUnit_Framework_TestCase /** @var \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection */ protected $collection; - /** @var ObjectManagerHelper */ - protected $objectManagerHelper; + /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */ + private $objectManager; /** @var \Magento\Framework\Data\Collection\EntityFactory|\PHPUnit_Framework_MockObject_MockObject */ protected $entityFactoryMock; @@ -76,6 +73,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->entityFactoryMock = $this->getMock( \Magento\Framework\Data\Collection\EntityFactory::class, [], @@ -133,14 +131,12 @@ class CollectionTest extends \PHPUnit_Framework_TestCase $this->timezoneInterfaceMock = $this->getMock(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class); $this->sessionMock = $this->getMock(\Magento\Customer\Model\Session::class, [], [], '', false); $this->dateTimeMock = $this->getMock(\Magento\Framework\Stdlib\DateTime::class); - $this->objectManagerHelper = new ObjectManagerHelper($this); - $this->prepareObjectManager([ - [\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class, + $this->objectManager->mockObjectManager([ + \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class => $this->getMock(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class) - ] ]); - $this->collection = $this->objectManagerHelper->getObject( + $this->collection = $this->objectManager->getObject( \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection::class, [ 'entityFactory' => $this->entityFactoryMock, @@ -165,6 +161,11 @@ class CollectionTest extends \PHPUnit_Framework_TestCase ); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + public function testSetProduct() { /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $product */ @@ -175,20 +176,4 @@ class CollectionTest extends \PHPUnit_Framework_TestCase $this->collection->setProduct($product); $this->assertEquals(33, $this->collection->getStoreId()); } - - /** - * @param $map - */ - public function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php index decf8e66f6738e19b98f0a95c782ce5b0e9def34..0aa45ddb7fb4a22e5d60580a58ad95f37f47463f 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Option/CollectionTest.php @@ -3,20 +3,22 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - -// @codingStandardsIgnoreFile - namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Product\Option; use \Magento\Catalog\Model\ResourceModel\Product\Option\Collection; use \Magento\Catalog\Model\ResourceModel\Product\Option\Value; /** - * Class CollectionTest * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * @codingStandardsIgnoreFile */ class CollectionTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\Framework\EntityManager\MetadataPool */ @@ -79,6 +81,7 @@ class CollectionTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->entityFactoryMock = $this->getMock( \Magento\Framework\Data\Collection\EntityFactory::class, ['create'], [], '', false ); @@ -147,9 +150,9 @@ class CollectionTest extends \PHPUnit_Framework_TestCase $this->metadataPoolMock->expects($this->any())->method('getMetadata')->willReturn($metadata); $this->selectMock->expects($this->exactly(2))->method('join'); - $this->prepareObjectManager([ - [\Magento\Framework\EntityManager\MetadataPool::class, $this->metadataPoolMock], - [\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface::class, $this->joinProcessor] + $this->objectManager->mockObjectManager([ + \Magento\Framework\EntityManager\MetadataPool::class => $this->metadataPoolMock, + \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface::class => $this->joinProcessor ]); $this->collection = new Collection( @@ -164,24 +167,13 @@ class CollectionTest extends \PHPUnit_Framework_TestCase ); } - public function testReset() + protected function tearDown() { - $this->collection->reset(); + $this->objectManager->restoreObjectManager(); } - /** - * @param $map - */ - private function prepareObjectManager($map) + public function testReset() { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); + $this->collection->reset(); } } diff --git a/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php b/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php index a624b87ebbe13efa061c7e97a8590e78bd5b70fe..f9a8ead3abff95df2e453c1dc618078dd26dfa32 100644 --- a/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php @@ -3,14 +3,9 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\CatalogRule\Test\Unit\Model; -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; - /** - * Class RuleTest - * @package Magento\CatalogRule\Test\Unit\Model * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class RuleTest extends \PHPUnit_Framework_TestCase @@ -18,8 +13,8 @@ class RuleTest extends \PHPUnit_Framework_TestCase /** @var \Magento\CatalogRule\Model\Rule */ protected $rule; - /** @var ObjectManagerHelper */ - protected $objectManagerHelper; + /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */ + private $objectManager; /** @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $storeManager; @@ -63,6 +58,7 @@ class RuleTest extends \PHPUnit_Framework_TestCase */ protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->storeManager = $this->getMock(\Magento\Store\Model\StoreManagerInterface::class); $this->storeModel = $this->getMock(\Magento\Store\Model\Store::class, ['__wakeup', 'getId'], [], '', false); $this->combineFactory = $this->getMock( @@ -128,20 +124,14 @@ class RuleTest extends \PHPUnit_Framework_TestCase false ); - $this->objectManagerHelper = new ObjectManagerHelper($this); - - $this->prepareObjectManager([ - [ - \Magento\Framework\Api\ExtensionAttributesFactory::class, - $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false) - ], - [ - \Magento\Framework\Api\AttributeValueFactory::class, + $this->objectManager->mockObjectManager([ + \Magento\Framework\Api\ExtensionAttributesFactory::class => + $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false), + \Magento\Framework\Api\AttributeValueFactory::class => $this->getMock(\Magento\Framework\Api\AttributeValueFactory::class, [], [], '', false) - ], ]); - $this->rule = $this->objectManagerHelper->getObject( + $this->rule = $this->objectManager->getObject( \Magento\CatalogRule\Model\Rule::class, [ 'storeManager' => $this->storeManager, @@ -153,6 +143,11 @@ class RuleTest extends \PHPUnit_Framework_TestCase ); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + /** * @dataProvider dataProviderCallbackValidateProduct * @param bool $validate @@ -375,20 +370,4 @@ class RuleTest extends \PHPUnit_Framework_TestCase $expectedResult = 'form_namerule_conditions_fieldset_100'; $this->assertEquals($expectedResult, $this->rule->getConditionsFieldSetId($formName)); } - - /** - * @param $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php index 96580c6764e9387e522a8c6d3a92d43e45869594..0a7363d1821af47d089a76ce1f25d2d712dcb967 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Advanced/CollectionTest.php @@ -7,7 +7,6 @@ namespace Magento\CatalogSearch\Test\Unit\Model\ResourceModel\Advanced; use Magento\Catalog\Model\Product; use Magento\CatalogSearch\Test\Unit\Model\ResourceModel\BaseCollectionTest; -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; /** * Tests Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection @@ -16,6 +15,11 @@ use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHe */ class CollectionTest extends BaseCollectionTest { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection */ @@ -51,8 +55,7 @@ class CollectionTest extends BaseCollectionTest */ protected function setUp() { - $helper = new ObjectManagerHelper($this); - + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->eavConfig = $this->getMock(\Magento\Eav\Model\Config::class, [], [], '', false); $storeManager = $this->getStoreManager(); $universalFactory = $this->getUniversalFactory(); @@ -67,13 +70,12 @@ class CollectionTest extends BaseCollectionTest ); $this->search = $this->getMock(\Magento\Search\Api\SearchInterface::class, [], [], '', false); - $this->prepareObjectManager([ - [\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class, + $this->objectManager->mockObjectManager([ + \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class => $this->getMock(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class) - ], ]); - $this->advancedCollection = $helper->getObject( + $this->advancedCollection = $this->objectManager->getObject( \Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection::class, [ 'eavConfig' => $this->eavConfig, @@ -87,6 +89,11 @@ class CollectionTest extends BaseCollectionTest ); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + public function testLoadWithFilterNoFilters() { $this->advancedCollection->loadWithFilter(); @@ -150,20 +157,4 @@ class CollectionTest extends BaseCollectionTest ->getMock(); return $criteriaBuilder; } - - /** - * @param $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php index 84430c56476c05463043670d024b79b3482affcb..0b762646cc7f1e8e09e8e3b7ab0850966698c7ad 100644 --- a/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php +++ b/app/code/Magento/CatalogSearch/Test/Unit/Model/ResourceModel/Fulltext/CollectionTest.php @@ -14,6 +14,11 @@ use PHPUnit_Framework_MockObject_MockObject as MockObject; */ class CollectionTest extends BaseCollectionTest { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\Framework\Search\Adapter\Mysql\TemporaryStorage|\PHPUnit_Framework_MockObject_MockObject */ @@ -64,20 +69,17 @@ class CollectionTest extends BaseCollectionTest */ protected function setUp() { - $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->storeManager = $this->getStoreManager(); $this->universalFactory = $this->getUniversalFactory(); $this->scopeConfig = $this->getScopeConfig(); $this->criteriaBuilder = $this->getCriteriaBuilder(); $this->filterBuilder = $this->getFilterBuilder(); - $this->prepareObjectManager( + $this->objectManager->mockObjectManager( [ - [ - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class, + \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class => $this->getMock(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class) - ], ] ); @@ -92,7 +94,7 @@ class CollectionTest extends BaseCollectionTest ->method('create') ->willReturn($this->temporaryStorage); - $this->model = $helper->getObject( + $this->model = $this->objectManager->getObject( \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection::class, [ 'storeManager' => $this->storeManager, @@ -110,6 +112,11 @@ class CollectionTest extends BaseCollectionTest $this->model->setFilterBuilder($this->filterBuilder); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + /** * @expectedException \Exception * @expectedExceptionCode 333 @@ -208,22 +215,6 @@ class CollectionTest extends BaseCollectionTest return $filterBuilder; } - /** - * @param $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } - protected function createFilter() { $filter = $this->getMockBuilder(\Magento\Framework\Api\Filter::class) diff --git a/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php b/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php index afcc540a375458a6fc0be2f0740b7e4d0ff58c23..4a3443b6d28f4a8d523782d0abf76ce307c04ad4 100644 --- a/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php @@ -3,13 +3,15 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\CatalogWidget\Test\Unit\Model; -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; - class RuleTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\CatalogWidget\Model\Rule */ @@ -22,25 +24,20 @@ class RuleTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->combineFactory = $this->getMockBuilder(\Magento\CatalogWidget\Model\Rule\Condition\CombineFactory::class) ->setMethods(['create']) ->disableOriginalConstructor() ->getMock(); - $objectManagerHelper = new ObjectManagerHelper($this); - - $this->prepareObjectManager([ - [ - \Magento\Framework\Api\ExtensionAttributesFactory::class, - $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false) - ], - [ - \Magento\Framework\Api\AttributeValueFactory::class, + $this->objectManager->mockObjectManager([ + \Magento\Framework\Api\ExtensionAttributesFactory::class => + $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false), + \Magento\Framework\Api\AttributeValueFactory::class => $this->getMock(\Magento\Framework\Api\AttributeValueFactory::class, [], [], '', false) - ], ]); - $this->rule = $objectManagerHelper->getObject( + $this->rule = $this->objectManager->getObject( \Magento\CatalogWidget\Model\Rule::class, [ 'conditionsFactory' => $this->combineFactory @@ -48,6 +45,11 @@ class RuleTest extends \PHPUnit_Framework_TestCase ); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + public function testGetConditionsInstance() { $condition = $this->getMockBuilder(\Magento\CatalogWidget\Model\Rule\Condition\Combine::class) @@ -62,20 +64,4 @@ class RuleTest extends \PHPUnit_Framework_TestCase { $this->assertNull($this->rule->getActionsInstance()); } - - /** - * @param $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php b/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php index 04d347ec237a9435964a5f4fcce784708c4365a4..7a5438ba17a5da72d19673387367ec80c933e22c 100644 --- a/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php +++ b/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php @@ -11,6 +11,11 @@ namespace Magento\Checkout\Test\Unit\Model; */ class ShippingInformationManagementTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -103,6 +108,7 @@ class ShippingInformationManagementTest extends \PHPUnit_Framework_TestCase protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->paymentMethodManagementMock = $this->getMock(\Magento\Quote\Api\PaymentMethodManagementInterface::class); $this->paymentDetailsFactoryMock = $this->getMock( \Magento\Checkout\Model\PaymentDetailsFactory::class, @@ -175,10 +181,10 @@ class ShippingInformationManagementTest extends \PHPUnit_Framework_TestCase $this->shippingFactoryMock = $this->getMock(\Magento\Quote\Model\ShippingFactory::class, ['create'], [], '', false); - $this->prepareObjectManager([ - [\Magento\Quote\Model\ShippingAssignmentFactory::class, $this->shippingAssignmentFactoryMock], - [\Magento\Quote\Api\Data\CartExtensionFactory::class, $this->cartExtensionFactoryMock], - [\Magento\Quote\Model\ShippingFactory::class, $this->shippingFactoryMock], + $this->objectManager->mockObjectManager([ + \Magento\Quote\Model\ShippingAssignmentFactory::class => $this->shippingAssignmentFactoryMock, + \Magento\Quote\Api\Data\CartExtensionFactory::class => $this->cartExtensionFactoryMock, + \Magento\Quote\Model\ShippingFactory::class => $this->shippingFactoryMock, ]); $this->model = new \Magento\Checkout\Model\ShippingInformationManagement( @@ -194,6 +200,11 @@ class ShippingInformationManagementTest extends \PHPUnit_Framework_TestCase ); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + /** * @expectedException \Magento\Framework\Exception\InputException * @expectedExceptionMessage Shipping method is not applicable for empty cart @@ -457,20 +468,4 @@ class ShippingInformationManagementTest extends \PHPUnit_Framework_TestCase $this->model->saveAddressInformation($cartId, $addressInformationMock) ); } - - /** - * @param array $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php b/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php index d3cd979b31a9118250062408205c38c7a7c8adb7..f3d5d4f5a9db096391a21cf45a68c596b1f63518 100644 --- a/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php +++ b/app/code/Magento/DownloadableImportExport/Test/Unit/Model/Import/Product/Type/DownloadableTest.php @@ -710,8 +710,8 @@ class DownloadableTest extends \Magento\ImportExport\Test\Unit\Model\Import\Abst ->getMock(\Magento\Framework\EntityManager\MetadataPool::class, ['getLinkField'], [], '', false); $metadataPoolMock->expects($this->any())->method('getMetadata')->willReturnSelf(); - $this->prepareObjectManager([ - [\Magento\Framework\EntityManager\MetadataPool::class, $metadataPoolMock], + $this->objectManagerHelper->mockObjectManager([ + \Magento\Framework\EntityManager\MetadataPool::class => $metadataPoolMock ]); $this->downloadableModelMock = $this->objectManagerHelper->getObject( @@ -736,6 +736,12 @@ class DownloadableTest extends \Magento\ImportExport\Test\Unit\Model\Import\Abst $this->downloadableModelMock->saveData(); } + protected function tearDown() + { + parent::tearDown(); + $this->objectManagerHelper->restoreObjectManager(); + } + /** * Data for methods testSetUploaderDirFalse, testSetDestDirFalse, testDirWithoutPermissions * @@ -870,20 +876,4 @@ class DownloadableTest extends \Magento\ImportExport\Test\Unit\Model\Import\Abst $reflectionProperty->setValue($object, $value); return $object; } - - /** - * @param $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php b/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php index dae3d56aa04b093c76d33f671606e2acfedb57af..0929cc16fb3ec0f280c4396b61042c2f352b6adc 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/RepositoryTest.php @@ -12,6 +12,11 @@ namespace Magento\Quote\Test\Unit\Model\Quote\Item; */ class RepositoryTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\Quote\Api\CartItemRepositoryInterface */ @@ -68,6 +73,7 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase */ protected function setUp() { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->quoteRepositoryMock = $this->getMock(\Magento\Quote\Api\CartRepositoryInterface::class); $this->productRepositoryMock = $this->getMock(\Magento\Catalog\Api\ProductRepositoryInterface::class); $this->itemDataFactoryMock = @@ -100,11 +106,8 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase '', false ); - $this->prepareObjectManager([ - [ - \Magento\Quote\Model\Quote\Item\CartItemOptionsProcessor::class, - $this->optionsProcessorMock - ] + $this->objectManager->mockObjectManager([ + \Magento\Quote\Model\Quote\Item\CartItemOptionsProcessor::class => $this->optionsProcessorMock ]); $this->repository = new \Magento\Quote\Model\Quote\Item\Repository( @@ -115,6 +118,11 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase ); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + /** * @return void */ @@ -246,20 +254,4 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase $this->assertTrue($this->repository->deleteById($cartId, $itemId)); } - - /** - * @param array $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php b/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php index abae7bae110e6521fbf7b708270eb32cb6792c69..0b820cdfca03598cb31033356577e510ce8d2fce 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php @@ -104,11 +104,9 @@ class ShippingAddressManagementTest extends \PHPUnit_Framework_TestCase '', false ); - $this->prepareObjectManager([ - [ - \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage::class, - $this->amountErrorMessageMock - ] + $this->objectManager->mockObjectManager([ + \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage::class + => $this->amountErrorMessageMock ]); $this->service = $this->objectManager->getObject( @@ -124,6 +122,11 @@ class ShippingAddressManagementTest extends \PHPUnit_Framework_TestCase ); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + /** * @expectedException \Magento\Framework\Exception\NoSuchEntityException * @expected ExceptionMessage error345 @@ -375,20 +378,4 @@ class ShippingAddressManagementTest extends \PHPUnit_Framework_TestCase $this->service->get('cartId'); } - - /** - * @param $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php b/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php index 5c60007543916f163f5e13ea8c83bf1a0e178be7..b492d59f568955ec62620560d244d796bd000fd3 100644 --- a/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php +++ b/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php @@ -3,7 +3,6 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\SalesRule\Test\Unit\Model\ResourceModel; use Magento\SalesRule\Api\Data\RuleInterface; @@ -13,6 +12,11 @@ use Magento\SalesRule\Api\Data\RuleInterface; */ class RuleTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\SalesRule\Model\ResourceModel\Rule */ @@ -60,7 +64,7 @@ class RuleTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->rule = $this->getMockBuilder(\Magento\SalesRule\Model\Rule::class) ->disableOriginalConstructor() ->getMock(); @@ -135,14 +139,11 @@ class RuleTest extends \PHPUnit_Framework_TestCase ] ); - $this->prepareObjectManager([ - [ - \Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap::class, - $associatedEntitiesMap - ], + $this->objectManager->mockObjectManager([ + \Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap::class => $associatedEntitiesMap ]); - $this->model = $objectManager->getObject( + $this->model = $this->objectManager->getObject( \Magento\SalesRule\Model\ResourceModel\Rule::class, [ 'context' => $context, @@ -152,6 +153,11 @@ class RuleTest extends \PHPUnit_Framework_TestCase ); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + /** * test load */ @@ -184,20 +190,4 @@ class RuleTest extends \PHPUnit_Framework_TestCase ->with($this->rule); $this->assertEquals($this->model->delete($this->rule), $this->model); } - - /** - * @param $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } } diff --git a/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php b/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php index f3d16c188fdf9dec8d2404d52fd01047a5deed6f..adf98a1528cd8a9c09d2ad36905175a5e84db08e 100644 --- a/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php @@ -3,11 +3,15 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\SalesRule\Test\Unit\Model; class RuleTest extends \PHPUnit_Framework_TestCase { + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManager; + /** * @var \Magento\SalesRule\Model\Rule */ @@ -30,7 +34,7 @@ class RuleTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->coupon = $this->getMockBuilder(\Magento\SalesRule\Model\Coupon::class) ->disableOriginalConstructor() @@ -57,18 +61,14 @@ class RuleTest extends \PHPUnit_Framework_TestCase ->setMethods(['create']) ->getMock(); - $this->prepareObjectManager([ - [ - \Magento\Framework\Api\ExtensionAttributesFactory::class, - $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false) - ], - [ - \Magento\Framework\Api\AttributeValueFactory::class, + $this->objectManager->mockObjectManager([ + \Magento\Framework\Api\ExtensionAttributesFactory::class => + $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false), + \Magento\Framework\Api\AttributeValueFactory::class => $this->getMock(\Magento\Framework\Api\AttributeValueFactory::class, [], [], '', false) - ], ]); - $this->model = $objectManager->getObject( + $this->model = $this->objectManager->getObject( \Magento\SalesRule\Model\Rule::class, [ 'couponFactory' => $couponFactory, @@ -78,6 +78,11 @@ class RuleTest extends \PHPUnit_Framework_TestCase ); } + protected function tearDown() + { + $this->objectManager->restoreObjectManager(); + } + public function testLoadCouponCode() { $this->coupon->expects($this->once()) @@ -179,20 +184,4 @@ class RuleTest extends \PHPUnit_Framework_TestCase $expectedResult = 'form_namerule_actions_fieldset_100'; $this->assertEquals($expectedResult, $this->model->getActionsFieldSetId($formName)); } - - /** - * @param $map - */ - private function prepareObjectManager($map) - { - $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); - $objectManagerMock->expects($this->any()) - ->method('get') - ->will($this->returnValueMap($map)); - $reflectionClass = new \ReflectionClass(\Magento\Framework\App\ObjectManager::class); - $reflectionProperty = $reflectionClass->getProperty('_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($objectManagerMock); - } }