1+ <?php
2+ namespace ApplicationTest ;
3+
4+ use Zend \Loader \AutoloaderFactory ;
5+ use Zend \Mvc \Service \ServiceManagerConfig ;
6+ use Zend \ServiceManager \ServiceManager ;
7+ use Zend \Stdlib \ArrayUtils ;
8+ use RuntimeException ;
9+
10+ error_reporting (E_ALL | E_STRICT );
11+ chdir (__DIR__ );
12+
13+ class Bootstrap
14+ {
15+ protected static $ serviceManager ;
16+ protected static $ config ;
17+ protected static $ bootstrap ;
18+
19+ public static function init ()
20+ {
21+ // Load the user-defined test configuration file, if it exists; otherwise, load
22+ if (is_readable (__DIR__ . '/TestConfig.php ' )) {
23+ $ testConfig = include __DIR__ . '/TestConfig.php ' ;
24+ } else {
25+ $ testConfig = include __DIR__ . '/TestConfig.php.dist ' ;
26+ }
27+
28+ $ zf2ModulePaths = array ();
29+
30+ if (isset ($ testConfig ['module_listener_options ' ]['module_paths ' ])) {
31+ $ modulePaths = $ testConfig ['module_listener_options ' ]['module_paths ' ];
32+ foreach ($ modulePaths as $ modulePath ) {
33+ if (($ path = static ::findParentPath ($ modulePath )) ) {
34+ $ zf2ModulePaths [] = $ path ;
35+ }
36+ }
37+ }
38+
39+ $ zf2ModulePaths = implode (PATH_SEPARATOR , $ zf2ModulePaths ) . PATH_SEPARATOR ;
40+ $ zf2ModulePaths .= getenv ('ZF2_MODULES_TEST_PATHS ' ) ?: (defined ('ZF2_MODULES_TEST_PATHS ' ) ? ZF2_MODULES_TEST_PATHS : '' );
41+
42+ static ::initAutoloader ();
43+
44+ // use ModuleManager to load this module and it's dependencies
45+ $ baseConfig = array (
46+ 'module_listener_options ' => array (
47+ 'module_paths ' => explode (PATH_SEPARATOR , $ zf2ModulePaths ),
48+ ),
49+ );
50+
51+ $ config = ArrayUtils::merge ($ baseConfig , $ testConfig );
52+
53+ $ serviceManager = new ServiceManager (new ServiceManagerConfig ());
54+ $ serviceManager ->setService ('ApplicationConfig ' , $ config );
55+ $ serviceManager ->get ('ModuleManager ' )->loadModules ();
56+
57+ static ::$ serviceManager = $ serviceManager ;
58+ static ::$ config = $ config ;
59+ }
60+
61+ public static function getServiceManager ()
62+ {
63+ return static ::$ serviceManager ;
64+ }
65+
66+ public static function getConfig ()
67+ {
68+ return static ::$ config ;
69+ }
70+
71+ protected static function initAutoloader ()
72+ {
73+ $ vendorPath = static ::findParentPath ('vendor ' );
74+
75+ if (is_readable ($ vendorPath . '/autoload.php ' )) {
76+ $ loader = include $ vendorPath . '/autoload.php ' ;
77+ } else {
78+ $ zf2Path = getenv ('ZF2_PATH ' ) ?: (defined ('ZF2_PATH ' ) ? ZF2_PATH : (is_dir ($ vendorPath . '/ZF2/library ' ) ? $ vendorPath . '/ZF2/library ' : false ));
79+
80+ if (!$ zf2Path ) {
81+ throw new RuntimeException ('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable. ' );
82+ }
83+
84+ include $ zf2Path . '/Zend/Loader/AutoloaderFactory.php ' ;
85+
86+ }
87+
88+ AutoloaderFactory::factory (array (
89+ 'Zend\Loader\StandardAutoloader ' => array (
90+ 'autoregister_zf ' => true ,
91+ 'namespaces ' => array (
92+ __NAMESPACE__ => __DIR__ . '/ ' . __NAMESPACE__ ,
93+ ),
94+ ),
95+ ));
96+ }
97+
98+ protected static function findParentPath ($ path )
99+ {
100+ $ dir = __DIR__ ;
101+ $ previousDir = '. ' ;
102+ while (!is_dir ($ dir . '/ ' . $ path )) {
103+ $ dir = dirname ($ dir );
104+ if ($ previousDir === $ dir ) return false ;
105+ $ previousDir = $ dir ;
106+ }
107+ return $ dir . '/ ' . $ path ;
108+ }
109+ }
110+
111+ Bootstrap::init ();
0 commit comments