diff --git a/app/code/Magento/Backend/Model/Session/Quote.php b/app/code/Magento/Backend/Model/Session/Quote.php index 12a4d4d138f5323f420dc56f5ba466e6efda0a46..6ca269488294a7d5036eedd9bacae42266ae8ea0 100644 --- a/app/code/Magento/Backend/Model/Session/Quote.php +++ b/app/code/Magento/Backend/Model/Session/Quote.php @@ -7,8 +7,6 @@ namespace Magento\Backend\Model\Session; use Magento\Customer\Api\CustomerRepositoryInterface; use Magento\Customer\Api\GroupManagementInterface; -use Magento\Framework\App\ObjectManager; -use Magento\Quote\Api\CartManagementInterface; /** * Adminhtml quote session @@ -81,11 +79,6 @@ class Quote extends \Magento\Framework\Session\SessionManager */ protected $quoteFactory; - /** - * @var \Magento\Quote\Api\CartManagementInterface; - */ - private $cartManagement; - /** * @param \Magento\Framework\App\Request\Http $request * @param \Magento\Framework\Session\SidResolverInterface $sidResolver 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 f6ddb41abf583f96a2b791c7c382423cf02ff64c..99d7fdfab21dedc37a5bc1b144b36f2acb073fbb 100644 --- a/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php +++ b/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php @@ -97,11 +97,6 @@ class QuoteTest extends \PHPUnit_Framework_TestCase */ protected $quoteFactoryMock; - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $cartManagementMock; - /** * Set up * @@ -203,13 +198,6 @@ class QuoteTest extends \PHPUnit_Framework_TestCase ); $this->quoteFactoryMock = $this->getMock(\Magento\Quote\Model\QuoteFactory::class, ['create'], [], '', false); - $this->cartManagementMock = $this->getMock( - \Magento\Quote\Api\CartManagementInterface::class, - [], - [], - '', - false - ); $this->quote = $this->getMock( \Magento\Backend\Model\Session\Quote::class, @@ -232,15 +220,6 @@ class QuoteTest extends \PHPUnit_Framework_TestCase 'quoteFactory' => $this->quoteFactoryMock ] ); - - $this->objectManager->mockObjectManager([ - \Magento\Quote\Api\CartManagementInterface::class => $this->cartManagementMock - ]); - } - - protected function tearDown() - { - $this->objectManager->restoreObjectManager(); } /** diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php index c24460981afbf8d6c6cccb79255031d8320b726f..e4af92baf85710b0cb1b66bb6a4079a48ac231cd 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php @@ -260,6 +260,11 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac */ private $metadataPool; + /** + * @var \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory + */ + private $productLimitationFiltersFactory; + /** * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory * @param \Psr\Log\LoggerInterface $logger @@ -316,7 +321,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac $this->_resourceHelper = $resourceHelper; $this->dateTime = $dateTime; $this->_groupManagement = $groupManagement; - $this->_productLimitationFilters = $this->createLimitationFilters(); + $this->_productLimitationFilters = $this->getProductLimitationFactory()->create(); parent::__construct( $entityFactory, $logger, @@ -2336,11 +2341,16 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac } /** - * @return Collection\ProductLimitation + * @deprecated + * @return \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory */ - private function createLimitationFilters() + private function getProductLimitationFactory() { - return \Magento\Framework\App\ObjectManager::getInstance() - ->create(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class); + if (null === $this->productLimitationFiltersFactory) { + $this->productLimitationFiltersFactory = ObjectManager::getInstance()->get( + \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory::class + ); + } + return $this->productLimitationFiltersFactory; } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php index 44ef53a34d0ef50d404eed4d55af158a71dcecf0..1842b8470c43a302560495303a93f73644f682fd 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductTypes/ConfigTest.php @@ -28,7 +28,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase private $serializerMock; /** - * @var \Magento\Catalog\Model\ProductTypes\Config|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Catalog\Model\ProductTypes\Config */ private $config; @@ -44,14 +44,16 @@ class ConfigTest extends \PHPUnit_Framework_TestCase ); $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] ); } protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); } /** @@ -67,7 +69,15 @@ class ConfigTest extends \PHPUnit_Framework_TestCase $this->serializerMock->method('unserialize') ->willReturn($value); - $this->config = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id'); + + $this->config = $this->objectManager->getObject( + \Magento\Catalog\Model\ProductTypes\Config::class, + [ + 'reader' => $this->readerMock, + 'cache' => $this->cacheMock, + 'cacheId' => 'cache_id', + ] + ); $this->assertEquals($expected, $this->config->getType('global')); } @@ -87,7 +97,15 @@ class ConfigTest extends \PHPUnit_Framework_TestCase ->willReturn(json_encode('"types":["Expected Data"]]')); $this->serializerMock->method('unserialize') ->willReturn(['types' => $expected]); - $this->config = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id'); + + $this->config = $this->objectManager->getObject( + \Magento\Catalog\Model\ProductTypes\Config::class, + [ + 'reader' => $this->readerMock, + 'cache' => $this->cacheMock, + 'cacheId' => 'cache_id', + ] + ); $this->assertEquals($expected, $this->config->getAll()); } @@ -98,8 +116,34 @@ class ConfigTest extends \PHPUnit_Framework_TestCase ->willReturn(''); $this->serializerMock->method('unserialize') ->willReturn([]); - $this->config = new \Magento\Catalog\Model\ProductTypes\Config($this->readerMock, $this->cacheMock, 'cache_id'); + $this->config = $this->objectManager->getObject( + \Magento\Catalog\Model\ProductTypes\Config::class, + [ + 'reader' => $this->readerMock, + 'cache' => $this->cacheMock, + 'cacheId' => 'cache_id', + ] + ); $this->assertEquals(false, $this->config->isProductSet('typeId')); } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($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 5f78d3dba0f60ede41672077f6e79600f7032648..b2ebc56e093974d05362dd8318d347ddb86c7d1c 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 @@ -5,6 +5,8 @@ */ namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Product; +use \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory; + /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ @@ -150,12 +152,14 @@ class CollectionTest extends \PHPUnit_Framework_TestCase $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 - ]); + $productLimitationFactoryMock = $this->getMock(ProductLimitationFactory::class, ['create']); + $productLimitationFactoryMock->method('create') + ->willReturn($productLimitationMock); + $this->mockObjectManager( + [ + ProductLimitationFactory::class => $productLimitationFactoryMock, + ] + ); $this->collection = $this->objectManager->getObject( \Magento\Catalog\Model\ResourceModel\Product\Collection::class, [ @@ -182,11 +186,28 @@ class CollectionTest extends \PHPUnit_Framework_TestCase ] ); $this->collection->setConnection($this->connectionMock); + $this->objectManager->setBackwardCompatibleProperty( + $this->collection, + 'mediaGalleryResource', + $this->galleryResourceMock + ); + $this->objectManager->setBackwardCompatibleProperty( + $this->collection, + 'metadataPool', + $this->metadataPoolMock + ); + $this->objectManager->setBackwardCompatibleProperty( + $this->collection, + 'productGalleryReadHandler', + $this->galleryReadHandlerMock + ); } protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); } public function testAddProductCategoriesFilter() @@ -255,4 +276,23 @@ class CollectionTest extends \PHPUnit_Framework_TestCase $this->assertSame($this->collection, $this->collection->addMediaGalleryData()); } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($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 11eb542773d59de25de891bff631ad96ee8c1331..93e5224387c5a7b07e23f44a14e54f811e7a902d 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 @@ -5,6 +5,9 @@ */ namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Product\Link\Product; +use \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory; +use \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation; + /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.TooManyFields) @@ -131,10 +134,10 @@ 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->objectManager->mockObjectManager([ - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class => - $this->getMock(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class) - ]); + $productLimitationFactory = $this->getMock(ProductLimitationFactory::class, ['create']); + $productLimitationFactory->method('create') + ->willReturn($this->getMock(ProductLimitation::class)); + $this->mockObjectManager([ProductLimitationFactory::class => $productLimitationFactory]); $this->collection = $this->objectManager->getObject( \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection::class, @@ -163,7 +166,9 @@ class CollectionTest extends \PHPUnit_Framework_TestCase protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); } public function testSetProduct() @@ -176,4 +181,23 @@ class CollectionTest extends \PHPUnit_Framework_TestCase $this->collection->setProduct($product); $this->assertEquals(33, $this->collection->getStoreId()); } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($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 0aa45ddb7fb4a22e5d60580a58ad95f37f47463f..33da480ba1a80b0330127d9f11e1e1836a88a2bc 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 @@ -150,9 +150,8 @@ class CollectionTest extends \PHPUnit_Framework_TestCase $this->metadataPoolMock->expects($this->any())->method('getMetadata')->willReturn($metadata); $this->selectMock->expects($this->exactly(2))->method('join'); - $this->objectManager->mockObjectManager([ + $this->mockObjectManager([ \Magento\Framework\EntityManager\MetadataPool::class => $this->metadataPoolMock, - \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface::class => $this->joinProcessor ]); $this->collection = new Collection( @@ -165,15 +164,41 @@ class CollectionTest extends \PHPUnit_Framework_TestCase null, $this->resourceMock ); + $this->objectManager->setBackwardCompatibleProperty( + $this->collection, + 'joinProcessor', + $this->joinProcessor + ); } protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); } public function testReset() { $this->collection->reset(); } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); + } } diff --git a/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php b/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php index f9a8ead3abff95df2e453c1dc618078dd26dfa32..22252d218f50cbd3e6e3271e40f3715a9473459f 100644 --- a/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php @@ -124,7 +124,7 @@ class RuleTest extends \PHPUnit_Framework_TestCase false ); - $this->objectManager->mockObjectManager([ + $this->mockObjectManager([ \Magento\Framework\Api\ExtensionAttributesFactory::class => $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false), \Magento\Framework\Api\AttributeValueFactory::class => @@ -145,7 +145,9 @@ class RuleTest extends \PHPUnit_Framework_TestCase protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); } /** @@ -370,4 +372,23 @@ class RuleTest extends \PHPUnit_Framework_TestCase $expectedResult = 'form_namerule_conditions_fieldset_100'; $this->assertEquals($expectedResult, $this->rule->getConditionsFieldSetId($formName)); } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($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 0a7363d1821af47d089a76ce1f25d2d712dcb967..a205d23ee2ac9f00ba742ba242440d47daff7a0d 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,6 +7,7 @@ namespace Magento\CatalogSearch\Test\Unit\Model\ResourceModel\Advanced; use Magento\Catalog\Model\Product; use Magento\CatalogSearch\Test\Unit\Model\ResourceModel\BaseCollectionTest; +use \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory; /** * Tests Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection @@ -70,10 +71,17 @@ class CollectionTest extends BaseCollectionTest ); $this->search = $this->getMock(\Magento\Search\Api\SearchInterface::class, [], [], '', false); - $this->objectManager->mockObjectManager([ - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class => - $this->getMock(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class) - ]); + $productLimitationMock = $this->getMock( + \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class + ); + $productLimitationFactoryMock = $this->getMock(ProductLimitationFactory::class, ['create']); + $productLimitationFactoryMock->method('create') + ->willReturn($productLimitationMock); + $this->mockObjectManager( + [ + ProductLimitationFactory::class => $productLimitationFactoryMock, + ] + ); $this->advancedCollection = $this->objectManager->getObject( \Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection::class, @@ -91,7 +99,9 @@ class CollectionTest extends BaseCollectionTest protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); } public function testLoadWithFilterNoFilters() @@ -157,4 +167,23 @@ class CollectionTest extends BaseCollectionTest ->getMock(); return $criteriaBuilder; } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($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 0b762646cc7f1e8e09e8e3b7ab0850966698c7ad..ae820a9d3aa8c00ce8b2c7021b866d057c99fb95 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 @@ -8,6 +8,7 @@ namespace Magento\CatalogSearch\Test\Unit\Model\ResourceModel\Fulltext; use Magento\CatalogSearch\Test\Unit\Model\ResourceModel\BaseCollectionTest; use Magento\Framework\Search\Adapter\Mysql\TemporaryStorageFactory; use PHPUnit_Framework_MockObject_MockObject as MockObject; +use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -76,10 +77,15 @@ class CollectionTest extends BaseCollectionTest $this->criteriaBuilder = $this->getCriteriaBuilder(); $this->filterBuilder = $this->getFilterBuilder(); - $this->objectManager->mockObjectManager( + $productLimitationMock = $this->getMock( + \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class + ); + $productLimitationFactoryMock = $this->getMock(ProductLimitationFactory::class, ['create']); + $productLimitationFactoryMock->method('create') + ->willReturn($productLimitationMock); + $this->mockObjectManager( [ - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class => - $this->getMock(\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class) + ProductLimitationFactory::class => $productLimitationFactoryMock, ] ); @@ -114,7 +120,9 @@ class CollectionTest extends BaseCollectionTest protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); } /** @@ -222,4 +230,23 @@ class CollectionTest extends BaseCollectionTest ->getMock(); return $filter; } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); + } } diff --git a/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php b/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php index 4a3443b6d28f4a8d523782d0abf76ce307c04ad4..5312dadf13c85921961e62503f74f1dcbd4889fa 100644 --- a/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php @@ -30,7 +30,7 @@ class RuleTest extends \PHPUnit_Framework_TestCase ->disableOriginalConstructor() ->getMock(); - $this->objectManager->mockObjectManager([ + $this->mockObjectManager([ \Magento\Framework\Api\ExtensionAttributesFactory::class => $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false), \Magento\Framework\Api\AttributeValueFactory::class => @@ -47,7 +47,9 @@ class RuleTest extends \PHPUnit_Framework_TestCase protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); } public function testGetConditionsInstance() @@ -64,4 +66,23 @@ class RuleTest extends \PHPUnit_Framework_TestCase { $this->assertNull($this->rule->getActionsInstance()); } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); + } } diff --git a/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php b/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php index 7a5438ba17a5da72d19673387367ec80c933e22c..c0649bef17f0a2efcb0c85b64e042577051a8e9a 100644 --- a/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php +++ b/app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php @@ -181,12 +181,6 @@ class ShippingInformationManagementTest extends \PHPUnit_Framework_TestCase $this->shippingFactoryMock = $this->getMock(\Magento\Quote\Model\ShippingFactory::class, ['create'], [], '', false); - $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( $this->paymentMethodManagementMock, $this->paymentDetailsFactoryMock, @@ -198,11 +192,21 @@ class ShippingInformationManagementTest extends \PHPUnit_Framework_TestCase $this->scopeConfigMock, $this->totalsCollectorMock ); - } - - protected function tearDown() - { - $this->objectManager->restoreObjectManager(); + $this->objectManager->setBackwardCompatibleProperty( + $this->model, + 'shippingAssignmentFactory', + $this->shippingAssignmentFactoryMock + ); + $this->objectManager->setBackwardCompatibleProperty( + $this->model, + 'cartExtensionFactory', + $this->cartExtensionFactoryMock + ); + $this->objectManager->setBackwardCompatibleProperty( + $this->model, + 'shippingFactory', + $this->shippingFactoryMock + ); } /** diff --git a/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php index 5eecaa147fcbddf55d8958b8c2625b7ea6edb477..4e78c15c4f373ecef6be9369e528acae90d3875d 100644 --- a/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Cron/Test/Unit/Model/Config/DataTest.php @@ -7,19 +7,30 @@ namespace Magento\Cron\Test\Unit\Model\Config; class DataTest extends \PHPUnit_Framework_TestCase { - /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager - */ - private $objectManager; - - protected function setUp() + protected function tearDown() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); } - protected function tearDown() + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) { - $this->objectManager->restoreObjectManager(); + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } /** @@ -53,7 +64,7 @@ class DataTest extends \PHPUnit_Framework_TestCase $dbReader->expects($this->once())->method('get')->will($this->returnValue($dbReaderData)); $serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $serializerMock] ); diff --git a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php index 40a754f78ec2f54bd94fa4d3697351262c74a390..d9499812cead9230f68a241f2868f470fa8ec9cb 100644 --- a/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php +++ b/app/code/Magento/Customer/Test/Unit/Model/Address/ConfigTest.php @@ -7,11 +7,6 @@ namespace Magento\Customer\Test\Unit\Model\Address; class ConfigTest extends \PHPUnit_Framework_TestCase { - /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager - */ - private $objectManager; - /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -59,7 +54,6 @@ class ConfigTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_storeMock = $this->getMock(\Magento\Store\Model\Store::class, [], [], '', false); $this->_scopeConfigMock = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class); @@ -104,7 +98,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase ); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] ); @@ -125,7 +119,28 @@ class ConfigTest extends \PHPUnit_Framework_TestCase protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testGetStore() diff --git a/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php b/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php index 2ae230da35c2229a57db6cc935cc4d856c0bcb95..4581c088e4bba5c6d4170b37e7b4cf95f1febd59 100644 --- a/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php +++ b/app/code/Magento/Directory/Test/Unit/Model/Country/Postcode/Config/DataTest.php @@ -7,11 +7,6 @@ namespace Magento\Directory\Test\Unit\Model\Country\Postcode\Config; class DataTest extends \PHPUnit_Framework_TestCase { - /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager - */ - private $objectManager; - /** * @var \Magento\Directory\Model\Country\Postcode\Config\Reader|\PHPUnit_Framework_MockObject_MockObject */ @@ -29,7 +24,6 @@ class DataTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->readerMock = $this->getMock( \Magento\Directory\Model\Country\Postcode\Config\Reader::class, [], @@ -45,7 +39,7 @@ class DataTest extends \PHPUnit_Framework_TestCase false ); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] ); } @@ -63,8 +57,29 @@ class DataTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, $configData->get()); } - public function tearDown() + protected function tearDown() + { + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) { - $this->objectManager->restoreObjectManager(); + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($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 f3d5d4f5a9db096391a21cf45a68c596b1f63518..592be58c9b4a5c6e9ed075e3218f6ec4bf75c71c 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,7 +710,7 @@ 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->objectManagerHelper->mockObjectManager([ + $this->mockObjectManager([ \Magento\Framework\EntityManager\MetadataPool::class => $metadataPoolMock ]); @@ -739,7 +739,28 @@ class DownloadableTest extends \Magento\ImportExport\Test\Unit\Model\Import\Abst protected function tearDown() { parent::tearDown(); - $this->objectManagerHelper->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } /** diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php index b111bf8c72a16a6920fcd79c25c95102c14582d6..90ac040af5be7cd95a776174ab751e0ff01388ed 100644 --- a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php +++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/ConfigTest.php @@ -11,11 +11,6 @@ namespace Magento\Eav\Test\Unit\Model\Entity\Attribute; class ConfigTest extends \PHPUnit_Framework_TestCase { - /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager - */ - private $objectManager; - /** * @var \Magento\Eav\Model\Entity\Attribute\Config */ @@ -48,7 +43,6 @@ class ConfigTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_attribute = $this->getMock(\Magento\Eav\Model\Entity\Attribute::class, [], [], '', false); $this->_entityType = $this->getMock(\Magento\Eav\Model\Entity\Type::class, [], [], '', false); $this->_readerMock = $this->getMock( @@ -69,7 +63,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase $serializerMock->method('unserialize') ->willReturn([]); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $serializerMock] ); $this->_model = new \Magento\Eav\Model\Entity\Attribute\Config( @@ -81,7 +75,28 @@ class ConfigTest extends \PHPUnit_Framework_TestCase protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testGetLockedFieldsEmpty() diff --git a/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php b/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php index 04cb6bbcab687444be9f9de7d99dd6714374f32b..d00642d86e9b9d44db0a3af442a7f0ce349b2815 100644 --- a/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php +++ b/app/code/Magento/ImportExport/Test/Unit/Model/Export/ConfigTest.php @@ -8,19 +8,14 @@ namespace Magento\ImportExport\Test\Unit\Model\Export; class ConfigTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + * @var \Magento\ImportExport\Model\Export\Config\Reader|\PHPUnit_Framework_MockObject_MockObject */ - private $objectManager; + protected $readerMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Config\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $_readerMock; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $_configScopeMock; + protected $cacheMock; /** * @var \Magento\Framework\Serialize\SerializerInterface @@ -30,33 +25,53 @@ class ConfigTest extends \PHPUnit_Framework_TestCase /** * @var string */ - protected $_cacheId = 'some_id'; + protected $cacheId = 'some_id'; /** * @var \Magento\ImportExport\Model\Export\Config */ - protected $_model; + protected $model; protected function setUp() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->_readerMock = $this->getMock( + $this->readerMock = $this->getMock( \Magento\ImportExport\Model\Export\Config\Reader::class, [], [], '', false ); - $this->_configScopeMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); + $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] ); } protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } /** @@ -66,22 +81,22 @@ class ConfigTest extends \PHPUnit_Framework_TestCase */ public function testGetEntities($value, $expected) { - $this->_configScopeMock->expects( + $this->cacheMock->expects( $this->any() )->method( 'load' )->with( - $this->_cacheId + $this->cacheId )->will( $this->returnValue(false) ); - $this->_readerMock->expects($this->any())->method('read')->will($this->returnValue($value)); - $this->_model = new \Magento\ImportExport\Model\Export\Config( - $this->_readerMock, - $this->_configScopeMock, - $this->_cacheId + $this->readerMock->expects($this->any())->method('read')->will($this->returnValue($value)); + $this->model = new \Magento\ImportExport\Model\Export\Config( + $this->readerMock, + $this->cacheMock, + $this->cacheId ); - $this->assertEquals($expected, $this->_model->getEntities('entities')); + $this->assertEquals($expected, $this->model->getEntities('entities')); } public function getEntitiesDataProvider() @@ -100,22 +115,22 @@ class ConfigTest extends \PHPUnit_Framework_TestCase */ public function testGetEntityTypes($configData, $entity, $expectedResult) { - $this->_configScopeMock->expects( + $this->cacheMock->expects( $this->any() )->method( 'load' )->with( - $this->_cacheId + $this->cacheId )->will( $this->returnValue(false) ); - $this->_readerMock->expects($this->any())->method('read')->will($this->returnValue($configData)); - $this->_model = new \Magento\ImportExport\Model\Export\Config( - $this->_readerMock, - $this->_configScopeMock, - $this->_cacheId + $this->readerMock->expects($this->any())->method('read')->will($this->returnValue($configData)); + $this->model = new \Magento\ImportExport\Model\Export\Config( + $this->readerMock, + $this->cacheMock, + $this->cacheId ); - $this->assertEquals($expectedResult, $this->_model->getEntityTypes($entity)); + $this->assertEquals($expectedResult, $this->model->getEntityTypes($entity)); } public function getEntityTypesDataProvider() @@ -153,22 +168,22 @@ class ConfigTest extends \PHPUnit_Framework_TestCase */ public function testGetFileFormats($value, $expected) { - $this->_configScopeMock->expects( + $this->cacheMock->expects( $this->any() )->method( 'load' )->with( - $this->_cacheId + $this->cacheId )->will( $this->returnValue(false) ); - $this->_readerMock->expects($this->any())->method('read')->will($this->returnValue($value)); - $this->_model = new \Magento\ImportExport\Model\Export\Config( - $this->_readerMock, - $this->_configScopeMock, - $this->_cacheId + $this->readerMock->expects($this->any())->method('read')->will($this->returnValue($value)); + $this->model = new \Magento\ImportExport\Model\Export\Config( + $this->readerMock, + $this->cacheMock, + $this->cacheId ); - $this->assertEquals($expected, $this->_model->getFileFormats('fileFormats')); + $this->assertEquals($expected, $this->model->getFileFormats('fileFormats')); } public function getFileFormatsDataProvider() diff --git a/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php b/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php index 2e182c4e35562e92f46cef27eeef3b7325891801..c3f9f6c81cf6777fa69fd4ed1a4b76f70d24d577 100644 --- a/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php +++ b/app/code/Magento/ImportExport/Test/Unit/Model/Import/ConfigTest.php @@ -8,19 +8,14 @@ namespace Magento\ImportExport\Test\Unit\Model\Import; class ConfigTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + * @var \Magento\ImportExport\Model\Import\Config\Reader|\PHPUnit_Framework_MockObject_MockObject */ - private $objectManager; + protected $readerMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\Config\CacheInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $_readerMock; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $_configScopeMock; + protected $cacheMock; /** * @var \Magento\Framework\Serialize\SerializerInterface @@ -30,33 +25,53 @@ class ConfigTest extends \PHPUnit_Framework_TestCase /** * @var string */ - protected $_cacheId = 'some_id'; + protected $cacheId = 'some_id'; /** * @var \Magento\ImportExport\Model\Import\Config */ - protected $_model; + protected $model; protected function setUp() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->_readerMock = $this->getMock( + $this->readerMock = $this->getMock( \Magento\ImportExport\Model\Import\Config\Reader::class, [], [], '', false ); - $this->_configScopeMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); + $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] ); } - public function tearDown() + protected function tearDown() + { + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) { - $this->objectManager->restoreObjectManager(); + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } /** @@ -66,22 +81,22 @@ class ConfigTest extends \PHPUnit_Framework_TestCase */ public function testGetEntities($value, $expected) { - $this->_configScopeMock->expects( + $this->cacheMock->expects( $this->any() )->method( 'load' )->with( - $this->_cacheId + $this->cacheId )->will( $this->returnValue(false) ); - $this->_readerMock->expects($this->any())->method('read')->will($this->returnValue($value)); - $this->_model = new \Magento\ImportExport\Model\Import\Config( - $this->_readerMock, - $this->_configScopeMock, - $this->_cacheId + $this->readerMock->expects($this->any())->method('read')->will($this->returnValue($value)); + $this->model = new \Magento\ImportExport\Model\Import\Config( + $this->readerMock, + $this->cacheMock, + $this->cacheId ); - $this->assertEquals($expected, $this->_model->getEntities('entities')); + $this->assertEquals($expected, $this->model->getEntities('entities')); } public function getEntitiesDataProvider() @@ -100,22 +115,22 @@ class ConfigTest extends \PHPUnit_Framework_TestCase */ public function testGetEntityTypes($configData, $entity, $expectedResult) { - $this->_configScopeMock->expects( + $this->cacheMock->expects( $this->any() )->method( 'load' )->with( - $this->_cacheId + $this->cacheId )->will( $this->returnValue(false) ); - $this->_readerMock->expects($this->any())->method('read')->will($this->returnValue($configData)); - $this->_model = new \Magento\ImportExport\Model\Import\Config( - $this->_readerMock, - $this->_configScopeMock, - $this->_cacheId + $this->readerMock->expects($this->any())->method('read')->will($this->returnValue($configData)); + $this->model = new \Magento\ImportExport\Model\Import\Config( + $this->readerMock, + $this->cacheMock, + $this->cacheId ); - $this->assertEquals($expectedResult, $this->_model->getEntityTypes($entity)); + $this->assertEquals($expectedResult, $this->model->getEntityTypes($entity)); } public function getEntityTypesDataProvider() diff --git a/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php index 6747025eed2a83b1a46284a9eee24b18fe01903c..9096bb8af1b822f493961c9a28f155202aa1c2b0 100644 --- a/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Indexer/Test/Unit/Model/Config/DataTest.php @@ -7,11 +7,6 @@ namespace Magento\Indexer\Test\Unit\Model\Config; class DataTest extends \PHPUnit_Framework_TestCase { - /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager - */ - private $objectManager; - /** * @var \Magento\Indexer\Model\Config\Data */ @@ -49,7 +44,6 @@ class DataTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->reader = $this->getMock(\Magento\Framework\Indexer\Config\Reader::class, ['read'], [], '', false); $this->cache = $this->getMockForAbstractClass( \Magento\Framework\Config\CacheInterface::class, @@ -68,14 +62,35 @@ class DataTest extends \PHPUnit_Framework_TestCase false ); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] ); } protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testConstructorWithCache() 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 0929cc16fb3ec0f280c4396b61042c2f352b6adc..c6e5241282cd0e9212ad342b0d48ee6da006ed51 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 @@ -106,9 +106,6 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase '', false ); - $this->objectManager->mockObjectManager([ - \Magento\Quote\Model\Quote\Item\CartItemOptionsProcessor::class => $this->optionsProcessorMock - ]); $this->repository = new \Magento\Quote\Model\Quote\Item\Repository( $this->quoteRepositoryMock, @@ -116,11 +113,11 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase $this->itemDataFactoryMock, ['custom_options' => $this->customOptionProcessor] ); - } - - protected function tearDown() - { - $this->objectManager->restoreObjectManager(); + $this->objectManager->setBackwardCompatibleProperty( + $this->repository, + 'cartItemOptionsProcessor', + $this->optionsProcessorMock + ); } /** diff --git a/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php b/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php index 0b820cdfca03598cb31033356577e510ce8d2fce..d8e65de847457e243a02d9535196e4c42f0499f8 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/ShippingAddressManagementTest.php @@ -104,10 +104,6 @@ class ShippingAddressManagementTest extends \PHPUnit_Framework_TestCase '', false ); - $this->objectManager->mockObjectManager([ - \Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage::class - => $this->amountErrorMessageMock - ]); $this->service = $this->objectManager->getObject( \Magento\Quote\Model\ShippingAddressManagement::class, @@ -120,11 +116,11 @@ class ShippingAddressManagementTest extends \PHPUnit_Framework_TestCase 'addressRepository' => $this->addressRepository ] ); - } - - protected function tearDown() - { - $this->objectManager->restoreObjectManager(); + $this->objectManager->setBackwardCompatibleProperty( + $this->service, + 'minimumAmountErrorMessage', + $this->amountErrorMessageMock + ); } /** diff --git a/app/code/Magento/Review/Test/Unit/Model/ResourceModel/Review/Product/CollectionTest.php b/app/code/Magento/Review/Test/Unit/Model/ResourceModel/Review/Product/CollectionTest.php index 4a61406e8a139bc0ee2688670e955b3e252db9a2..d2a959e0a3a005f66e43052e8f92022f374cf220 100644 --- a/app/code/Magento/Review/Test/Unit/Model/ResourceModel/Review/Product/CollectionTest.php +++ b/app/code/Magento/Review/Test/Unit/Model/ResourceModel/Review/Product/CollectionTest.php @@ -5,6 +5,8 @@ */ namespace Magento\Review\Test\Unit\Model\ResourceModel\Review\Product; +use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory; + /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ @@ -82,10 +84,15 @@ class CollectionTest extends \PHPUnit_Framework_TestCase $productLimitationMock = $this->getMock( \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class ); + $productLimitationFactoryMock = $this->getMock(ProductLimitationFactory::class, ['create']); + $productLimitationFactoryMock->method('create') + ->willReturn($productLimitationMock); + $this->mockObjectManager( + [ + ProductLimitationFactory::class => $productLimitationFactoryMock, + ] + ); $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->objectManager->mockObjectManager([ - \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation::class => $productLimitationMock - ]); $this->model = $this->objectManager->getObject( \Magento\Review\Model\ResourceModel\Review\Product\Collection::class, [ @@ -99,7 +106,28 @@ class CollectionTest extends \PHPUnit_Framework_TestCase protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } /** diff --git a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php index ed001a14c992416b6adef726aee0706222906cc1..cfb2f63c99fec36ca98dc5c06cb3bc088c77c8e9 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Config/DataTest.php @@ -7,11 +7,6 @@ namespace Magento\Sales\Test\Unit\Model\Config; class DataTest extends \PHPUnit_Framework_TestCase { - /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager - */ - private $objectManager; - /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -29,7 +24,6 @@ class DataTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_readerMock = $this->getMockBuilder( \Magento\Sales\Model\Config\Reader::class )->disableOriginalConstructor()->getMock(); @@ -37,14 +31,35 @@ class DataTest extends \PHPUnit_Framework_TestCase \Magento\Framework\App\Cache\Type\Config::class )->disableOriginalConstructor()->getMock(); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] ); } protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testGet() 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 b492d59f568955ec62620560d244d796bd000fd3..c0de06df83087035937690cbfd05a3aa3f457e99 100644 --- a/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php +++ b/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php @@ -139,7 +139,7 @@ class RuleTest extends \PHPUnit_Framework_TestCase ] ); - $this->objectManager->mockObjectManager([ + $this->mockObjectManager([ \Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap::class => $associatedEntitiesMap ]); @@ -155,7 +155,28 @@ class RuleTest extends \PHPUnit_Framework_TestCase protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } /** diff --git a/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php b/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php index adf98a1528cd8a9c09d2ad36905175a5e84db08e..d7f58dce73790646f4ce3002a3ea0fc839d1947b 100644 --- a/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php @@ -61,7 +61,7 @@ class RuleTest extends \PHPUnit_Framework_TestCase ->setMethods(['create']) ->getMock(); - $this->objectManager->mockObjectManager([ + $this->mockObjectManager([ \Magento\Framework\Api\ExtensionAttributesFactory::class => $this->getMock(\Magento\Framework\Api\ExtensionAttributesFactory::class, [], [], '', false), \Magento\Framework\Api\AttributeValueFactory::class => @@ -80,7 +80,28 @@ class RuleTest extends \PHPUnit_Framework_TestCase protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testLoadCouponCode() diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php index ab200ea88ef17a9de52fd83d391eeddc6003e104..2d7f61da267a0bdd8bfeaf3bfd54698c117c3372 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Config/InitialTest.php @@ -17,11 +17,6 @@ class InitialTest extends \PHPUnit_Framework_TestCase */ private $config; - /** - * @var \Magento\Framework\App\Config\Initial\Reader|\PHPUnit_Framework_MockObject_MockObject - */ - private $readerMock; - /** * @var \Magento\Framework\App\Cache\Type\Config|\PHPUnit_Framework_MockObject_MockObject */ @@ -42,13 +37,6 @@ class InitialTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->readerMock = $this->getMock( - \Magento\Framework\App\Config\Initial\Reader::class, - [], - [], - '', - false - ); $this->cacheMock = $this->getMock( \Magento\Framework\App\Cache\Type\Config::class, [], @@ -64,19 +52,42 @@ class InitialTest extends \PHPUnit_Framework_TestCase $serializerMock->method('unserialize') ->willReturn($this->data); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $serializerMock] ); - $this->config = new \Magento\Framework\App\Config\Initial( - $this->readerMock, - $this->cacheMock + $this->config = $this->objectManager->getObject( + \Magento\Framework\App\Config\Initial::class, + [ + 'cache' => $this->cacheMock, + ] ); } protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } /** diff --git a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php index 993faa1dcd67912425ca367deba666fcaba0f931..744e293029f0efa64be26ee019aeb6358d89bf1f 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/Router/ActionListTest.php @@ -50,14 +50,35 @@ class ActionListTest extends \PHPUnit_Framework_TestCase false ); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] ); } protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testConstructActionsCached() diff --git a/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php b/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php index 8af81fe5dc60868d9f9f165a4ade27c2c9e471af..2b0a677b51848ca8909cfd53ed5343617b19c958 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/Data/ScopedTest.php @@ -43,23 +43,22 @@ class ScopedTest extends \PHPUnit_Framework_TestCase $this->_readerMock = $this->getMock(\Magento\Framework\Config\ReaderInterface::class); $this->_configScopeMock = $this->getMock(\Magento\Framework\Config\ScopeInterface::class); $this->_cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); - $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( - [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] - ); - $this->_model = new \Magento\Framework\Config\Data\Scoped( - $this->_readerMock, - $this->_configScopeMock, - $this->_cacheMock, - 'tag' + $this->_model = $this->objectManager->getObject( + \Magento\Framework\Config\Data\Scoped::class, + [ + 'reader' => $this->_readerMock, + 'configScope' => $this->_configScopeMock, + 'cache' => $this->_cacheMock, + 'cacheId' => 'tag', + ] + ); + $this->objectManager->setBackwardCompatibleProperty( + $this->_model, + 'serializer', + $this->serializerMock ); - } - - protected function tearDown() - { - $this->objectManager->restoreObjectManager(); } /** diff --git a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php index b04257713389fa66a06244f47ce6a2d5ca0d5c0e..d527f1b0da4b5d3e28be0234e3be5c2a8e55e84b 100644 --- a/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php +++ b/lib/internal/Magento/Framework/Config/Test/Unit/DataTest.php @@ -10,12 +10,7 @@ namespace Magento\Framework\Config\Test\Unit; class DataTest extends \PHPUnit_Framework_TestCase { - /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager - */ - private $objectManager; - - /** + /** * @var \Magento\Framework\Config\ReaderInterface|\PHPUnit_Framework_MockObject_MockObject */ private $readerMock; @@ -32,16 +27,36 @@ class DataTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->readerMock = $this->getMock(\Magento\Framework\Config\ReaderInterface::class); $this->cacheMock = $this->getMock(\Magento\Framework\Config\CacheInterface::class); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager([\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock]); + $this->mockObjectManager([\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock]); } protected function tearDown() { - $this->objectManager->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testGetConfigNotCached() diff --git a/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php b/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php index db0218d00afc15674aeeaa89fa7415b1b6576bea..7ff809810cc0814c4890601092eb71de4939473b 100644 --- a/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php +++ b/lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php @@ -77,12 +77,33 @@ class ConfigTest extends \PHPUnit_Framework_TestCase ); $this->serializerMock = $this->getMock(SerializerInterface::class); $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->objectManagerHelper->mockObjectManager([SerializerInterface::class => $this->serializerMock]); + $this->mockObjectManager([SerializerInterface::class => $this->serializerMock]); } protected function tearDown() { - $this->objectManagerHelper->restoreObjectManager(); + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) + { + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } /** diff --git a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php index ce9a97b7ac90ade86acf727844d4cb2316dff574..8b8b3e737fe953c0b322ff93aa317c3b5584d667 100644 --- a/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php +++ b/lib/internal/Magento/Framework/Mview/Test/Unit/Config/DataTest.php @@ -7,11 +7,6 @@ namespace Magento\Framework\Mview\Test\Unit\Config; class DataTest extends \PHPUnit_Framework_TestCase { - /** - * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager - */ - private $objectManager; - /** * @var \Magento\Framework\Mview\Config\Data */ @@ -49,7 +44,6 @@ class DataTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->reader = $this->getMock(\Magento\Framework\Mview\Config\Reader::class, ['read'], [], '', false); $this->cache = $this->getMockForAbstractClass( \Magento\Framework\Config\CacheInterface::class, @@ -71,14 +65,35 @@ class DataTest extends \PHPUnit_Framework_TestCase ); $this->serializerMock = $this->getMock(\Magento\Framework\Serialize\SerializerInterface::class); - $this->objectManager->mockObjectManager( + $this->mockObjectManager( [\Magento\Framework\Serialize\SerializerInterface::class => $this->serializerMock] ); } - public function tearDown() + protected function tearDown() + { + $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue(null); + } + + /** + * Mock application object manager to return configured dependencies. + * + * @param array $dependencies + * @return void + */ + private function mockObjectManager($dependencies) { - $this->objectManager->restoreObjectManager(); + $dependencyMap = []; + foreach ($dependencies as $type => $instance) { + $dependencyMap[] = [$type, $instance]; + } + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($dependencyMap)); + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); } public function testConstructorWithCache() diff --git a/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php b/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php index 91ecf8424748f7068e7bb5839efeea93972a7f82..16493899d7d4ed7d3457bdc66ebc6bd24a802105 100644 --- a/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php +++ b/lib/internal/Magento/Framework/TestFramework/Unit/Helper/ObjectManager.php @@ -342,42 +342,4 @@ class ObjectManager $reflectionProperty->setAccessible(true); $reflectionProperty->setValue($object, $propertyValue); } - - /** - * Mock application object manager to return configured dependencies. Can be used in the case when need to add a - * new dependency that is used in the constructor keeping backwards compatibility - * - * $dependencies = [\Magento\Framework\Json\JsonInterface::class => $jsonMock] - * - * @param array $dependencies - * @return void - */ - public function mockObjectManager($dependencies) - { - $dependencyMap = []; - foreach ($dependencies as $type => $instance) { - $dependencyMap[] = [$type, $instance]; - } - $objectManagerMock = $this->_testObject->getMock(\Magento\Framework\ObjectManagerInterface::class); - $objectManagerMock->expects($this->_testObject->any()) - ->method('getInstance') - ->willReturnSelf(); - $objectManagerMock->expects($this->_testObject->any()) - ->method('get') - ->will($this->_testObject->returnValueMap($dependencyMap)); - \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); - } - - /** - * Unset mocked object manager, must be used to restore - * \Magento\Framework\App\ObjectManager::_instance after mockObjectManager called - * - * @return void - */ - public function restoreObjectManager() - { - $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance'); - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue(null); - } }