1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace JhUserTest\Util; |
21
|
|
|
|
22
|
|
|
use Zend\Mvc\Service\ServiceManagerConfig; |
23
|
|
|
use Zend\ServiceManager\ServiceManager; |
24
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
25
|
|
|
|
26
|
|
|
use Doctrine\Common\DataFixtures\Purger\ORMPurger as FixturePurger; |
27
|
|
|
use Doctrine\Common\DataFixtures\Executor\ORMExecutor as FixtureExecutor; |
28
|
|
|
|
29
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Base test case to be used when a new service manager instance is required |
33
|
|
|
* |
34
|
|
|
* @license MIT |
35
|
|
|
* @link https://github.com/zf-fr/ZfrRest |
36
|
|
|
* @author Marco Pivetta <[email protected]> |
37
|
|
|
*/ |
38
|
|
|
abstract class ServiceManagerFactory |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private static $config = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @static |
47
|
|
|
* @param array $config |
48
|
|
|
*/ |
49
|
|
|
public static function setApplicationConfig(array $config) |
50
|
|
|
{ |
51
|
|
|
static::$config = $config; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @static |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public static function getApplicationConfig() |
59
|
|
|
{ |
60
|
|
|
return static::$config; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array|null $config |
65
|
|
|
* @return ServiceManager |
66
|
|
|
*/ |
67
|
|
|
public static function getServiceManager(array $config = null) |
68
|
|
|
{ |
69
|
|
|
$config = $config ?: static::getApplicationConfig(); |
70
|
|
|
$serviceManager = new ServiceManager( |
71
|
|
|
new ServiceManagerConfig( |
72
|
|
|
isset($config['service_manager']) ? $config['service_manager'] : [] |
73
|
|
|
) |
74
|
|
|
); |
75
|
|
|
$serviceManager->setService('ApplicationConfig', $config); |
76
|
|
|
|
77
|
|
|
/* @var $moduleManager \Zend\ModuleManager\ModuleManagerInterface */ |
78
|
|
|
$moduleManager = $serviceManager->get('ModuleManager'); |
79
|
|
|
|
80
|
|
|
$moduleManager->loadModules(); |
81
|
|
|
|
82
|
|
|
// @todo move to own factory class/add to merged configuration? Create a test module? |
83
|
|
|
$serviceManager->setFactory( |
84
|
|
|
'Doctrine\Common\DataFixtures\Executor\AbstractExecutor', |
85
|
|
|
function (ServiceLocatorInterface $sl) { |
86
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
87
|
|
|
$em = $sl->get('Doctrine\ORM\EntityManager'); |
88
|
|
|
$schemaTool = new SchemaTool($em); |
89
|
|
|
$schemaTool->createSchema($em->getMetadataFactory()->getAllMetadata()); |
90
|
|
|
return new FixtureExecutor($em, new FixturePurger($em)); |
91
|
|
|
} |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
return $serviceManager; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: