6
6
namespace Magento \Framework \ObjectManager ;
7
7
8
8
use Magento \Framework \App \Utility \Classes ;
9
+ use Magento \Framework \Exception \RuntimeException ;
9
10
use Magento \Framework \ObjectManager \FactoryInterface as ObjectManagerFactoryInterface ;
10
11
use Magento \Framework \ObjectManagerInterface ;
11
12
use Magento \GraphQl \App \State \Collector ;
17
18
class ResetAfterRequestTest extends \PHPUnit \Framework \TestCase
18
19
{
19
20
20
- private static $ objectManager ;
21
-
21
+ private ?ObjectManagerInterface $ objectManager ;
22
22
private ?Comparator $ comparator ;
23
23
private ?Collector $ collector ;
24
24
25
- public static function setUpBeforeClass (): void
26
- {
27
- $ config = new \Magento \Framework \ObjectManager \Config \Config ();
28
- $ factory = new Factory \Dynamic \Developer ($ config );
29
- self ::$ objectManager = new \Magento \Framework \ObjectManager \ObjectManager ($ factory , $ config );
30
- // self::$objectManager->configure(
31
- // ['preferences' => [self::TEST_INTERFACE => self::TEST_INTERFACE_IMPLEMENTATION]]
32
- // );
33
- $ factory ->setObjectManager (self ::$ objectManager );
34
- }
35
-
36
- public static function tearDownAfterClass (): void
37
- {
38
- self ::$ objectManager = null ;
39
- }
40
-
41
25
/**
42
26
* @return void
43
27
*/
44
28
protected function setUp (): void
45
29
{
46
30
parent ::setUp ();
47
- $ this ->comparator = static ::$ objectManager ->create (Comparator::class);
48
- $ this ->collector = static ::$ objectManager ->create (Collector::class);
31
+ $ this ->objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
32
+ $ this ->objectManager ->configure (
33
+ $ this ->objectManager ->get (ConfigLoaderInterface::class)->load ('graphql ' )
34
+ );
35
+ $ this ->comparator = $ this ->objectManager ->create (Comparator::class);
36
+ $ this ->collector = $ this ->objectManager ->create (Collector::class);
49
37
}
50
38
51
39
/**
52
- * Data provider for testNewInstance
53
- *
54
40
* Provides list of all classes and virtual classes that implement ResetAfterRequestInterface
55
41
*
56
42
* @return array
57
43
*/
58
44
public function resetAfterRequestClassDataProvider ()
59
45
{
60
46
$ resetAfterRequestClasses = [];
61
- foreach (Classes::getVirtualClasses () as $ name => $ type ) {
47
+ foreach (Classes::getVirtualClasses () as $ name => $ type ) {
62
48
try {
63
49
if (!class_exists ($ type )) {
64
50
continue ;
@@ -77,19 +63,34 @@ public function resetAfterRequestClassDataProvider()
77
63
}
78
64
}
79
65
foreach (array_keys (Classes::collectModuleClasses ('[A-Z][a-z\d][A-Za-z\d \\\\]+ ' )) as $ type ) {
66
+ if (str_contains ($ type , "_files " )) {
67
+ continue ; // We have to skip the fixture files that collectModuleClasses returns;
68
+ }
80
69
try {
81
70
if (!class_exists ($ type )) {
82
71
continue ;
83
72
}
84
- if (is_a ($ type , ObjectManagerInterface::class)) {
73
+ if (!is_a ($ type , ResetAfterRequestInterface::class, true )) {
74
+ continue ; // We only want to return classes that implement ResetAfterRequestInterface
75
+ }
76
+ if (is_a ($ type , ObjectManagerInterface::class, true )) {
85
77
continue ;
86
78
}
87
- if (is_a ($ type , ObjectManagerFactoryInterface::class)) {
79
+ if (is_a ($ type , ObjectManagerFactoryInterface::class, true )) {
88
80
continue ;
89
81
}
90
- if (is_a ($ type , ResetAfterRequestInterface::class, true )) {
91
- $ resetAfterRequestClasses [] = [$ type ];
82
+ $ reflectionClass = new \ReflectionClass ($ type );
83
+ if ($ reflectionClass ->isAbstract ()) {
84
+ continue ; // We can't test abstract classes since they can't instantiate.
92
85
}
86
+ if (\Magento \Catalog \Model \ResourceModel \Collection \AbstractCollection::class == $ type ) {
87
+ continue ; // This class isn't abstract, but it can't be constructed itself without error
88
+ }
89
+ if (\Magento \Eav \Model \ResourceModel \Form \Attribute \Collection::class == $ type ) {
90
+ continue ; // Note: This class isn't abstract, but it cannot be constructed itself.
91
+ // It requires subclass to modify protected $_moduleName to be constructed.
92
+ }
93
+ $ resetAfterRequestClasses [] = [$ type ];
93
94
} catch (\Throwable $ throwable ) {
94
95
continue ;
95
96
}
@@ -105,18 +106,86 @@ public function resetAfterRequestClassDataProvider()
105
106
*/
106
107
public function testResetAfterRequestClasses (string $ className )
107
108
{
108
- /** @var ResetAfterRequestInterface $object */
109
- $ object = self ::$ objectManager ->get ($ className );
110
- $ beforeProperties = $ this ->collector ->getPropertiesFromObject ($ object );
111
- $ object ->_resetState ();
112
- $ afterProperties = $ this ->collector ->getPropertiesFromObject ($ object );
113
- $ differences = [];
114
- foreach ($ afterProperties as $ propertyName => $ propertyValue ) {
115
- $ result = $ this ->comparator ->checkValues ($ beforeProperties [$ propertyName ] ?? null , $ propertyValue );
116
- if ($ result ) {
117
- $ differences [$ propertyName ] = $ result ;
109
+ if (\Magento \Backend \Model \Locale \Resolver::class == $ className ) { // FIXME: ACPT-1369
110
+ static ::markTestSkipped (
111
+ "FIXME: Temporal coupling with Magento\Backend\Model\Locale\Resolver and its _request "
112
+ );
113
+ }
114
+ try {
115
+ $ object = $ this ->objectManager ->create ($ className );
116
+ } catch (\BadMethodCallException $ exception ) {
117
+ static ::markTestSkipped (sprintf (
118
+ 'The class "%s" cannot be be constructed without proper arguments %s ' ,
119
+ $ className ,
120
+ (string )$ exception
121
+ ));
122
+ } catch (\ReflectionException $ reflectionException ) {
123
+ static ::markTestSkipped (sprintf (
124
+ 'The class "%s" cannot be constructed. It may require different area. %s ' ,
125
+ $ className ,
126
+ (string )$ reflectionException
127
+ ));
128
+ } catch (\Error $ error ) {
129
+ static ::markTestSkipped (sprintf (
130
+ 'The class "%s" cannot be constructed. It had Error. %s ' ,
131
+ $ className ,
132
+ (string )$ error
133
+ ));
134
+ } catch (RuntimeException $ exception ) {
135
+ // TODO: We should find a way to test these classes that require additional run time data/configuration
136
+ static ::markTestSkipped (sprintf (
137
+ 'The class "%s" had RuntimeException. %s ' ,
138
+ $ className ,
139
+ (string )$ exception
140
+ ));
141
+ } catch (\Throwable $ throwable ) {
142
+ throw new \Exception (
143
+ sprintf ("testResetAfterRequestClasses failed on %s " , $ className ),
144
+ 0 ,
145
+ $ throwable
146
+ );
147
+ }
148
+ try {
149
+ /** @var ResetAfterRequestInterface $object */
150
+ $ beforeProperties = $ this ->collector ->getPropertiesFromObject ($ object , true );
151
+ $ object ->_resetState ();
152
+ $ afterProperties = $ this ->collector ->getPropertiesFromObject ($ object , true );
153
+ $ differences = [];
154
+ foreach ($ afterProperties as $ propertyName => $ propertyValue ) {
155
+ if ($ propertyValue instanceof ObjectManagerInterface) {
156
+ continue ; // We need to skip ObjectManagers
157
+ }
158
+ if ($ propertyValue instanceof \Magento \Framework \Model \ResourceModel \Db \AbstractDb) {
159
+ continue ; // The _tables array gets added to
160
+ }
161
+ if ($ propertyValue instanceof \Magento \Framework \Model \ResourceModel \Db \VersionControl \Snapshot) {
162
+ continue ;
163
+ }
164
+ if ('pluginList ' == $ propertyName ) {
165
+ continue ; // We can skip plugin List loading from intercepters.
166
+ }
167
+ if ('_select ' == $ propertyName ) {
168
+ continue ; // We can skip _select because we load a fresh new Select after reset
169
+ }
170
+ if ('_regionModels ' == $ propertyName
171
+ && is_a ($ className , \Magento \Customer \Model \Address \AbstractAddress::class, true ))
172
+ {
173
+ continue ; // AbstractAddress has static property _regionModels, so it would fail this test.
174
+ // TODO: Can we convert _regionModels to member variable,
175
+ // or move to a dependency injected service class instead?
176
+ }
177
+ $ result = $ this ->comparator ->checkValues ($ beforeProperties [$ propertyName ] ?? null , $ propertyValue , 3 );
178
+ if ($ result ) {
179
+ $ differences [$ propertyName ] = $ result ;
180
+ }
118
181
}
182
+ $ this ->assertEmpty ($ differences , var_export ($ differences , true ));
183
+ } catch (\Throwable $ throwable ) {
184
+ throw new \Exception (
185
+ sprintf ("testResetAfterRequestClasses failed on %s " , $ className ),
186
+ 0 ,
187
+ $ throwable
188
+ );
119
189
}
120
- $ this ->assertEmpty ($ differences , var_export ($ differences , true ));
121
190
}
122
191
}
0 commit comments