Skip to content

Commit 16fada2

Browse files
author
Codeliner
committed
Add PHPUnit structure
1 parent 1ed28a3 commit 16fada2

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

module/Application/Module.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public function getAutoloaderConfig()
3232
'Zend\Loader\StandardAutoloader' => array(
3333
'namespaces' => array(
3434
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
35+
'Domain' => __DIR__ . '/src/Domain',
36+
'Infrastructure' => __DIR__ . 'src/Infrastructure'
3537
),
3638
),
3739
);
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
return array(
3+
'modules' => array(
4+
'Application',
5+
),
6+
'module_listener_options' => array(
7+
'config_glob_paths' => array(
8+
'../../../config/autoload/{,*.}{global,local}.php',
9+
),
10+
'module_paths' => array(
11+
'module',
12+
'vendor',
13+
),
14+
),
15+
);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit bootstrap="Bootstrap.php">
4+
<testsuites>
5+
<testsuite name="CargoShippingSystem">
6+
<directory>./ApplicationTest</directory>
7+
</testsuite>
8+
</testsuites>
9+
</phpunit>

0 commit comments

Comments
 (0)