Skip to content

Commit d2e11ef

Browse files
ACPT-1186
* fixing static test failures * adding "readonly" to more member variable definitions * use proper reset method for objects in the weakMap
1 parent aff1c9a commit d2e11ef

File tree

7 files changed

+37
-31
lines changed

7 files changed

+37
-31
lines changed

dev/tests/integration/testsuite/Magento/Framework/ObjectManager/ResetAfterRequestTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Framework\ObjectManager;
79

810
use Magento\Framework\App\Utility\Classes;
@@ -26,7 +28,7 @@ class ResetAfterRequestTest extends \PHPUnit\Framework\TestCase
2628
private ?Collector $collector;
2729

2830
/**
29-
* @return void
31+
* @inheritDoc
3032
*/
3133
protected function setUp(): void
3234
{

dev/tests/integration/testsuite/Magento/GraphQl/App/State/CollectedObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Magento\GraphQl\App\State;
99

1010
/**
11-
* Collects shared objects from ObjectManager and clones properties for later comparison
11+
* Immutable recursive data structure that holds copy of properties from collected objects. Created by Collector.
1212
*/
1313
class CollectedObject
1414
{

dev/tests/integration/testsuite/Magento/GraphQl/App/State/CollectedObjectConstructedAndCurrent.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
class CollectedObjectConstructedAndCurrent
1414
{
1515
public function __construct(
16-
private object $object,
17-
private CollectedObject $constructedCollected,
18-
private CollectedObject $currentCollected,
16+
private readonly object $object,
17+
private readonly CollectedObject $constructedCollected,
18+
private readonly CollectedObject $currentCollected,
1919
) {
2020
}
2121

dev/tests/integration/testsuite/Magento/GraphQl/App/State/Collector.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
*/
1616
class Collector
1717
{
18-
private array $skipListFromConstructed;
19-
private array $skipListBetweenRequests;
18+
private readonly array $skipListFromConstructed;
19+
private readonly array $skipListBetweenRequests;
2020

2121
/** @var ObjectManager $objectManager Note: Using ObjectManagerInterface for DI to get correct instance */
2222

2323
public function __construct(
24-
private ObjectManagerInterface $objectManager,
24+
private readonly ObjectManagerInterface $objectManager,
2525
SkipListAndFilterList $skipListAndFilterList
2626
) {
2727
$this->skipListFromConstructed =
@@ -116,12 +116,7 @@ public function getPropertiesConstructedAndCurrent(): array
116116
throw new \Exception("Not the correct type of ObjectManager");
117117
}
118118
// Calling _resetState helps us avoid adding skip/filter for these classes.
119-
foreach ($objectManager->getWeakMap() as $object => $propertiesBefore) {
120-
if ($object instanceof ResetAfterRequestInterface) {
121-
$object->_resetState();
122-
}
123-
unset($object);
124-
}
119+
$objectManager->resetStateWeakMapObjects();
125120
/* Note: We must force garbage collection to clean up cyclic referenced objects after _resetState()
126121
Otherwise, they may still show up in the WeakMap. */
127122
gc_collect_cycles();
@@ -143,6 +138,7 @@ public function getPropertiesConstructedAndCurrent(): array
143138
* @param CompareType $compareType
144139
* @param int $recursionLevel
145140
* @return CollectedObject
141+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
146142
*/
147143
public function getPropertiesFromObject(
148144
object $object,

dev/tests/integration/testsuite/Magento/GraphQl/App/State/Comparator.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Magento\GraphQl\App\State;
99

1010
/**
11-
* Compare object state between requests
11+
* Compare object state between requests and between first instantiation by ObjectManager
1212
*/
1313
class Comparator
1414
{
@@ -20,8 +20,10 @@ class Comparator
2020
*/
2121
private array $objectsStateAfter = [];
2222

23-
public function __construct(private Collector $collector, private SkipListAndFilterList $skipListAndFilterList)
24-
{
23+
public function __construct(
24+
private readonly Collector $collector,
25+
private readonly SkipListAndFilterList $skipListAndFilterList
26+
) {
2527
}
2628

2729
/**
@@ -94,10 +96,6 @@ public function compareConstructedAgainstCurrent(string $operationName): array
9496
$compareResults = [];
9597
$skipList = $this->skipListAndFilterList
9698
->getSkipList($operationName, CompareType::CompareConstructedAgainstCurrent);
97-
$filterList = $this->skipListAndFilterList->getFilterList();
98-
$filterListParentClasses = $filterList['parents'] ?? [];
99-
$filterListServices = $filterList['services'] ?? [];
100-
$filterListAll = $filterList['all'] ?? [];
10199
foreach ($this->collector->getPropertiesConstructedAndCurrent() as $objectAndProperties) {
102100
$object = $objectAndProperties->getObject();
103101
$constructedObject = $objectAndProperties->getConstructedCollected();
@@ -128,6 +126,7 @@ public function compareConstructedAgainstCurrent(string $operationName): array
128126
* @param string $serviceName
129127
* @return array
130128
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
129+
* @SuppressWarnings(PHPMD.NPathComplexity)
131130
*/
132131
private function compare(
133132
CollectedObject $before,

dev/tests/integration/testsuite/Magento/GraphQl/App/State/DynamicFactoryDecorator.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,21 @@
1313
use WeakReference;
1414

1515
/**
16-
* Dynamic Factory Decorator for State test.
16+
* Dynamic Factory Decorator for State test. Stores collected properties from created objects in WeakMap
1717
*/
1818
class DynamicFactoryDecorator extends Developer implements ResetAfterRequestInterface
1919
{
20-
private WeakMap $weakMap; // values are CollectedObject
21-
private Collector $collector;
22-
private array $skipList;
20+
/** @var WeakMap $weakMap Where CollectedObject is stored after object is created by us */
21+
private WeakMap $weakMap;
22+
private readonly Collector $collector;
23+
private readonly array $skipList;
2324

25+
/**
26+
* Constructs this instance by copying $developer
27+
*
28+
* @param Developer $developer
29+
* @param ObjectManager $objectManager
30+
*/
2431
public function __construct(Developer $developer, ObjectManager $objectManager)
2532
{
2633
/* Note: PHP doesn't have copy constructors, so we have to use get_object_vars,
@@ -59,17 +66,19 @@ public function create($type, array $arguments = [])
5966
*/
6067
public function _resetState(): void
6168
{
62-
/* Note: we can't iterate resetAfterWeakMap itself because it gets indirectly modified (shrinks) as some of the
69+
/* Note: we can't iterate weakMap itself because it gets indirectly modified (shrinks) as some of the
6370
* service classes that get reset will destruct some of the other service objects. The iterator to WeakMap
6471
* returns actual objects, not WeakReferences. Therefore, we create a temporary list of weak references which
6572
* is safe to iterate. */
66-
$temporaryWeakReferenceList = [];
67-
foreach($this->weakMap as $weakMapObject => $value) {
68-
$temporaryWeakReferenceList[] = WeakReference::create($weakMapObject);
73+
$weakReferenceListToReset = [];
74+
foreach ($this->weakMap as $weakMapObject => $value) {
75+
if ($weakMapObject instanceof ResetAfterRequestInterface) {
76+
$weakReferenceListToReset[] = WeakReference::create($weakMapObject);
77+
}
6978
unset($weakMapObject);
7079
unset($value);
7180
}
72-
foreach ($temporaryWeakReferenceList as $weakReference) {
81+
foreach ($weakReferenceListToReset as $weakReference) {
7382
$object = $weakReference->get();
7483
if (!$object) {
7584
continue;

dev/tests/integration/testsuite/Magento/GraphQl/App/State/ObjectManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Weakmap;
1313

1414
/**
15-
* Collects shared objects from ObjectManager and clones properties for later comparison
15+
* ObjectManager decorator used by GraphQlStateTest for resetting objects and getting initial properties from objects
1616
*/
1717
class ObjectManager extends TestFrameworkObjectManager
1818
{

0 commit comments

Comments
 (0)