forked from magento/magento2-functional-testing-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectManagerFactory.php
120 lines (105 loc) · 4.54 KB
/
ObjectManagerFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework;
use Magento\FunctionalTestingFramework\ObjectManager\Factory;
use Magento\FunctionalTestingFramework\Stdlib\BooleanUtils;
use Magento\FunctionalTestingFramework\ObjectManager as MagentoObjectManager;
/**
* Object Manager Factory.
*
* @api
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
// @codingStandardsIgnoreFile
class ObjectManagerFactory
{
/**
* Object Manager class name.
*
* @var string
*/
protected $locatorClassName = '\Magento\FunctionalTestingFramework\ObjectManager';
/**
* DI Config class name.
*
* @var string
*/
protected $configClassName = '\Magento\FunctionalTestingFramework\ObjectManager\Config';
/**
* Create Object Manager.
*
* @param array $sharedInstances
* @return ObjectManager
*/
public function create(array $sharedInstances = [])
{
/** @var \Magento\FunctionalTestingFramework\ObjectManager\Config $diConfig */
$diConfig = new $this->configClassName();
$factory = new Factory($diConfig);
$argInterpreter = $this->createArgumentInterpreter(new BooleanUtils());
$argumentMapper = new \Magento\FunctionalTestingFramework\ObjectManager\Config\Mapper\Dom($argInterpreter);
$sharedInstances['Magento\FunctionalTestingFramework\Data\Argument\InterpreterInterface'] = $argInterpreter;
$sharedInstances['Magento\FunctionalTestingFramework\ObjectManager\Config\Mapper\Dom'] = $argumentMapper;
/** @var \Magento\FunctionalTestingFramework\ObjectManager $objectManager */
$objectManager = new $this->locatorClassName($factory, $diConfig, $sharedInstances);
$factory->setObjectManager($objectManager);
ObjectManager::setInstance($objectManager);
self::configure($objectManager);
return $objectManager;
}
/**
* Return newly created instance on an argument interpreter, suitable for processing DI arguments.
*
* @param \Magento\FunctionalTestingFramework\Stdlib\BooleanUtils $booleanUtils
* @return \Magento\FunctionalTestingFramework\Data\Argument\InterpreterInterface
*/
protected function createArgumentInterpreter(
\Magento\FunctionalTestingFramework\Stdlib\BooleanUtils $booleanUtils
) {
$constInterpreter = new \Magento\FunctionalTestingFramework\Data\Argument\Interpreter\Constant();
$result = new \Magento\FunctionalTestingFramework\Data\Argument\Interpreter\Composite(
[
'boolean' => new \Magento\FunctionalTestingFramework\Data\Argument\Interpreter\Boolean($booleanUtils),
'string' => new \Magento\FunctionalTestingFramework\Data\Argument\Interpreter\StringUtils($booleanUtils),
'number' => new \Magento\FunctionalTestingFramework\Data\Argument\Interpreter\Number(),
'null' => new \Magento\FunctionalTestingFramework\Data\Argument\Interpreter\NullType(),
'const' => $constInterpreter,
'object' => new \Magento\FunctionalTestingFramework\Data\Argument\Interpreter\DataObject($booleanUtils),
'init_parameter' => new \Magento\FunctionalTestingFramework\Data\Argument\Interpreter\Argument($constInterpreter),
],
\Magento\FunctionalTestingFramework\ObjectManager\Config\Reader\Dom::TYPE_ATTRIBUTE
);
// Add interpreters that reference the composite
$result->addInterpreter('array', new \Magento\FunctionalTestingFramework\Data\Argument\Interpreter\ArrayType($result));
return $result;
}
/**
* Get Object Manager instance.
*
* @return ObjectManager
*/
public static function getObjectManager()
{
if (!$objectManager = ObjectManager::getInstance()) {
$objectManagerFactory = new self();
$objectManager = $objectManagerFactory->create();
}
return $objectManager;
}
/**
* Configure Object Manager.
* This method is static to have the ability to configure multiple instances of Object manager when needed.
*
* @param \Magento\FunctionalTestingFramework\ObjectManagerInterface $objectManager
* @return void
*/
public static function configure(\Magento\FunctionalTestingFramework\ObjectManagerInterface $objectManager)
{
$objectManager->configure(
$objectManager->get(\Magento\FunctionalTestingFramework\ObjectManager\ConfigLoader\Primary::class)->load()
);
}
}