Skip to content

Commit 03cdbc6

Browse files
ShradddhaShradddha
authored andcommitted
AC-10621::PHPUnit 10 upgrade error: ‘RuntimeException: ObjectManager isn't initialized’
1 parent bfea788 commit 03cdbc6

File tree

10 files changed

+178
-19
lines changed

10 files changed

+178
-19
lines changed

app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/UrlRewriteHandlerTest.php

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use Magento\UrlRewrite\Model\UrlPersistInterface;
2222
use PHPUnit\Framework\MockObject\MockObject;
2323
use PHPUnit\Framework\TestCase;
24+
use Magento\CatalogUrlRewrite\Model\ProductScopeRewriteGenerator;
25+
use Magento\Framework\App\Config\ScopeConfigInterface;
2426

2527
/**
2628
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -77,6 +79,16 @@ class UrlRewriteHandlerTest extends TestCase
7779
*/
7880
private $serializerMock;
7981

82+
/**
83+
* @var ProductScopeRewriteGenerator
84+
*/
85+
private $productScopeRewriteGeneratorMock;
86+
87+
/**
88+
* @var ScopeConfigInterface
89+
*/
90+
private $scopeConfigMock;
91+
8092
/**
8193
* {@inheritDoc}
8294
*/
@@ -96,7 +108,7 @@ protected function setUp(): void
96108
->disableOriginalConstructor()
97109
->getMock();
98110
$this->mergeDataProviderFactoryMock = $this->getMockBuilder(MergeDataProviderFactory::class)
99-
->setMethods(['create'])
111+
->onlyMethods(['create'])
100112
->disableOriginalConstructor()
101113
->getMock();
102114
$this->mergeDataProviderMock = $this->getMockBuilder(MergeDataProvider::class)
@@ -111,6 +123,12 @@ protected function setUp(): void
111123
$this->serializerMock = $this->getMockBuilder(Json::class)
112124
->disableOriginalConstructor()
113125
->getMock();
126+
$this->productScopeRewriteGeneratorMock = $this->getMockBuilder(ProductScopeRewriteGenerator::class)
127+
->disableOriginalConstructor()
128+
->getMock();
129+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
130+
->disableOriginalConstructor()
131+
->getMock();
114132

115133
$this->urlRewriteHandler = new UrlRewriteHandler(
116134
$this->childrenCategoriesProviderMock,
@@ -120,7 +138,9 @@ protected function setUp(): void
120138
$this->collectionFactoryMock,
121139
$this->categoryBasedProductRewriteGeneratorMock,
122140
$this->mergeDataProviderFactoryMock,
123-
$this->serializerMock
141+
$this->serializerMock,
142+
$this->productScopeRewriteGeneratorMock,
143+
$this->scopeConfigMock
124144
);
125145
}
126146

@@ -131,7 +151,8 @@ public function testGenerateProductUrlRewrites()
131151
{
132152
/* @var \Magento\Catalog\Model\Category|MockObject $category */
133153
$category = $this->getMockBuilder(Category::class)
134-
->setMethods(['getEntityId', 'getStoreId', 'getData', 'getChangedProductIds'])
154+
->addMethods(['getChangedProductIds'])
155+
->onlyMethods(['getEntityId', 'getStoreId', 'getData'])
135156
->disableOriginalConstructor()
136157
->getMock();
137158
$category->expects($this->any())
@@ -142,18 +163,22 @@ public function testGenerateProductUrlRewrites()
142163
->willReturn(1);
143164
$category->expects($this->any())
144165
->method('getData')
145-
->withConsecutive(
146-
[$this->equalTo('save_rewrites_history')],
147-
[$this->equalTo('initial_setup_flag')]
148-
)
149-
->willReturnOnConsecutiveCalls(
150-
true,
151-
null
152-
);
166+
->willReturnCallback(fn ($operation) => match ([$operation]) {
167+
[$this->equalTo('save_rewrites_history')] => true,
168+
[$this->equalTo('initial_setup_flag')] => null
169+
});
170+
// ->withConsecutive(
171+
// [$this->equalTo('save_rewrites_history')],
172+
// [$this->equalTo('initial_setup_flag')]
173+
// )
174+
// ->willReturnOnConsecutiveCalls(
175+
// true,
176+
// null
177+
// );
153178

154179
/* @var \Magento\Catalog\Model\Category|MockObject $childCategory1 */
155180
$childCategory1 = $this->getMockBuilder(Category::class)
156-
->setMethods(['getEntityId'])
181+
->onlyMethods(['getEntityId'])
157182
->disableOriginalConstructor()
158183
->getMock();
159184
$childCategory1->expects($this->any())
@@ -162,7 +187,7 @@ public function testGenerateProductUrlRewrites()
162187

163188
/* @var \Magento\Catalog\Model\Category|MockObject $childCategory1 */
164189
$childCategory2 = $this->getMockBuilder(Category::class)
165-
->setMethods(['getEntityId'])
190+
->onlyMethods(['getEntityId'])
166191
->disableOriginalConstructor()
167192
->getMock();
168193
$childCategory1->expects($this->any())

app/code/Magento/Checkout/Test/Unit/Block/Checkout/AttributeMergerTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Customer\Model\Session as CustomerSession;
1414
use Magento\Directory\Helper\Data as DirectoryHelper;
1515
use PHPUnit\Framework\TestCase;
16+
use Magento\Directory\Model\AllowedCountries;
1617

1718
class AttributeMergerTest extends TestCase
1819
{
@@ -41,6 +42,11 @@ class AttributeMergerTest extends TestCase
4142
*/
4243
private $attributeMerger;
4344

45+
/**
46+
* @var AllowedCountries
47+
*/
48+
private $allowedCountryReader;
49+
4450
/**
4551
* @inheritdoc
4652
*/
@@ -50,12 +56,14 @@ protected function setUp(): void
5056
$this->customerSession = $this->createMock(CustomerSession::class);
5157
$this->addressHelper = $this->createMock(AddressHelper::class);
5258
$this->directoryHelper = $this->createMock(DirectoryHelper::class);
59+
$this->allowedCountryReader = $this->createMock(AllowedCountries::class);
5360

5461
$this->attributeMerger = new AttributeMerger(
5562
$this->addressHelper,
5663
$this->customerSession,
5764
$this->customerRepository,
58-
$this->directoryHelper
65+
$this->directoryHelper,
66+
$this->allowedCountryReader
5967
);
6068
}
6169

app/code/Magento/Checkout/Test/Unit/Block/LinkTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99

1010
use Magento\Checkout\Block\Link;
1111
use Magento\Checkout\Helper\Data;
12+
use Magento\Framework\Math\Random;
1213
use Magento\Framework\Module\Manager;
1314
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1415
use Magento\Framework\UrlInterface;
1516
use Magento\Framework\View\Element\Template\Context;
17+
use Magento\Framework\View\Helper\SecureHtmlRenderer;
1618
use PHPUnit\Framework\TestCase;
1719

1820
class LinkTest extends TestCase
@@ -25,6 +27,18 @@ class LinkTest extends TestCase
2527
protected function setUp(): void
2628
{
2729
$this->_objectManagerHelper = new ObjectManager($this);
30+
31+
$objects = [
32+
[
33+
SecureHtmlRenderer::class,
34+
$this->createMock(SecureHtmlRenderer::class)
35+
],
36+
[
37+
Random::class,
38+
$this->createMock(Random::class)
39+
]
40+
];
41+
$this->_objectManagerHelper->prepareObjectManager($objects);
2842
}
2943

3044
public function testGetUrl()

app/code/Magento/Checkout/Test/Unit/Model/DefaultConfigProviderTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use Magento\Store\Model\StoreManagerInterface;
4444
use PHPUnit\Framework\MockObject\MockObject;
4545
use PHPUnit\Framework\TestCase;
46+
use Magento\Framework\Escaper;
4647

4748
/**
4849
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -84,6 +85,11 @@ class DefaultConfigProviderTest extends TestCase
8485
*/
8586
private $configPostProcessor;
8687

88+
/**
89+
* @var Escaper
90+
*/
91+
private $escaper;
92+
8793
/**
8894
* @inheritdoc
8995
*/
@@ -122,6 +128,7 @@ protected function setUp(): void
122128
$this->addressMetadata = $this->createMock(AddressMetadataInterface::class);
123129
$attributeOptionManager = $this->createMock(AttributeOptionManagementInterface::class);
124130
$customerAddressData = $this->createMock(CustomerAddressDataProvider::class);
131+
$escaper = $this->createMock(Escaper::class);
125132
$this->model = new DefaultConfigProvider(
126133
$checkoutHelper,
127134
$this->checkoutSession,
@@ -152,7 +159,8 @@ protected function setUp(): void
152159
$this->configPostProcessor,
153160
$this->addressMetadata,
154161
$attributeOptionManager,
155-
$customerAddressData
162+
$customerAddressData,
163+
$escaper
156164
);
157165
}
158166

app/code/Magento/Checkout/Test/Unit/Model/SessionTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\Event\ManagerInterface;
1414
use Magento\Framework\Exception\NoSuchEntityException;
1515
use Magento\Framework\Message\CollectionFactory;
16+
use Magento\Framework\Session\SessionStartChecker;
1617
use Magento\Framework\Session\Storage;
1718
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1819
use Magento\Quote\Api\CartRepositoryInterface;
@@ -51,6 +52,14 @@ class SessionTest extends TestCase
5152
protected function setUp(): void
5253
{
5354
$this->helper = new ObjectManager($this);
55+
$objects = [
56+
[
57+
SessionStartChecker::class,
58+
$this->createMock(SessionStartChecker::class)
59+
]
60+
];
61+
$this->helper->prepareObjectManager($objects);
62+
5463
}
5564

5665
/**

app/code/Magento/Cms/Test/Unit/Block/Adminhtml/Wysiwyg/Images/TreeTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
use Magento\Cms\Block\Adminhtml\Wysiwyg\Images\Tree;
1212
use Magento\Cms\Helper\Wysiwyg\Images;
1313
use Magento\Cms\Model\Wysiwyg\Images\Storage;
14+
use Magento\Directory\Helper\Data as DirectoryHelper;
1415
use Magento\Framework\App\Filesystem\DirectoryList;
1516
use Magento\Framework\DataObject;
1617
use Magento\Framework\Filesystem;
1718
use Magento\Framework\Filesystem\Directory\Read;
19+
use Magento\Framework\Json\Helper\Data as JsonHelper;
1820
use Magento\Framework\Registry;
1921
use Magento\Framework\Serialize\Serializer\Json;
2022
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
@@ -62,6 +64,19 @@ class TreeTest extends TestCase
6264
protected function setUp(): void
6365
{
6466
$objectManager = new ObjectManager($this);
67+
68+
$objects = [
69+
[
70+
JsonHelper::class,
71+
$this->createMock(JsonHelper::class)
72+
],
73+
[
74+
DirectoryHelper::class,
75+
$this->createMock(DirectoryHelper::class)
76+
]
77+
];
78+
$this->objectManager->prepareObjectManager($objects);
79+
6580
$contextMock = $this->createMock(Context::class);
6681
$this->cmsWysiwygImagesMock = $this->createMock(Images::class);
6782
$this->coreRegistryMock = $this->createMock(Registry::class);

app/code/Magento/Cms/Test/Unit/Block/Widget/Page/LinkTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
use Magento\Cms\Block\Widget\Page\Link;
1111
use Magento\Cms\Helper\Page;
12+
use Magento\Framework\Math\Random;
13+
use Magento\Framework\ObjectManagerInterface;
1214
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use Magento\Framework\View\Helper\SecureHtmlRenderer;
1316
use PHPUnit\Framework\MockObject\MockObject;
1417
use PHPUnit\Framework\TestCase;
1518

@@ -35,9 +38,26 @@ class LinkTest extends TestCase
3538
*/
3639
protected $mockResourcePage;
3740

41+
/**
42+
* @var ObjectManagerHelper
43+
*/
44+
protected $objectManagerHelper;
45+
3846
protected function setUp(): void
3947
{
4048
$this->objectManager = new ObjectManager($this);
49+
$objects = [
50+
[
51+
SecureHtmlRenderer::class,
52+
$this->createMock(SecureHtmlRenderer::class)
53+
],
54+
[
55+
Random::class,
56+
$this->createMock(Random::class)
57+
]
58+
];
59+
$this->objectManager->prepareObjectManager($objects);
60+
4161
$this->mockCmsPage = $this->createMock(Page::class);
4262
$this->mockResourcePage = $this->createMock(\Magento\Cms\Model\ResourceModel\Page::class);
4363

@@ -161,4 +181,22 @@ public function testGetLabelEmpty()
161181
{
162182
$this->assertEmpty($this->linkElement->getLabel());
163183
}
184+
185+
// /**
186+
// * @param $map
187+
// */
188+
// private function prepareObjectManager($map)
189+
// {
190+
// $objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class)
191+
// ->addMethods(['getInstance'])
192+
// ->onlyMethods(['get'])
193+
// ->getMockForAbstractClass();
194+
//
195+
// $objectManagerMock->method('getInstance')->willReturnSelf();
196+
// $objectManagerMock->method('get')->willReturnMap($map);
197+
//
198+
// $reflectionProperty = new \ReflectionProperty(\Magento\Framework\App\ObjectManager::class, '_instance');
199+
// $reflectionProperty->setAccessible(true);
200+
// $reflectionProperty->setValue($objectManagerMock);
201+
// }
164202
}

app/code/Magento/Cms/Test/Unit/Model/PageRepository/ValidationCompositeTest.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Magento\Framework\Exception\LocalizedException;
1717
use PHPUnit\Framework\MockObject\MockObject;
1818
use PHPUnit\Framework\TestCase;
19+
use Magento\Framework\EntityManager\HydratorInterface;
1920

2021
/**
2122
* Validate behavior of the validation composite
@@ -27,10 +28,18 @@ class ValidationCompositeTest extends TestCase
2728
*/
2829
private $subject;
2930

31+
/**
32+
* @var HydratorInterface|MockObject
33+
*/
34+
private $hydratorMock;
35+
3036
protected function setUp(): void
3137
{
3238
/** @var PageRepositoryInterface subject */
3339
$this->subject = $this->getMockForAbstractClass(PageRepositoryInterface::class);
40+
41+
/** @var PageRepositoryInterface subject */
42+
$this->hydratorMock = $this->getMockForAbstractClass(HydratorInterface::class);
3443
}
3544

3645
/**
@@ -40,7 +49,7 @@ protected function setUp(): void
4049
public function testConstructorValidation($validators)
4150
{
4251
$this->expectException('InvalidArgumentException');
43-
new ValidationComposite($this->subject, $validators);
52+
new ValidationComposite($this->subject, $validators, $this->hydratorMock);
4453
}
4554

4655
public function testSaveInvokesValidatorsWithSuccess()
@@ -66,7 +75,7 @@ public function testSaveInvokesValidatorsWithSuccess()
6675
->with($page)
6776
->willReturn('foo');
6877

69-
$composite = new ValidationComposite($this->subject, [$validator1, $validator2]);
78+
$composite = new ValidationComposite($this->subject, [$validator1, $validator2], $this->hydratorMock);
7079
$result = $composite->save($page);
7180

7281
self::assertSame('foo', $result);
@@ -97,7 +106,7 @@ public function testSaveInvokesValidatorsWithErrors()
97106
->expects($this->never())
98107
->method('save');
99108

100-
$composite = new ValidationComposite($this->subject, [$validator1, $validator2]);
109+
$composite = new ValidationComposite($this->subject, [$validator1, $validator2], $this->hydratorMock);
101110
$composite->save($page);
102111
}
103112

@@ -113,7 +122,7 @@ public function testPassthroughMethods($method, $arg)
113122
->with($arg)
114123
->willReturn('foo');
115124

116-
$composite = new ValidationComposite($this->subject, []);
125+
$composite = new ValidationComposite($this->subject, [], $this->hydratorMock);
117126
$result = $composite->{$method}($arg);
118127

119128
self::assertSame('foo', $result);

0 commit comments

Comments
 (0)