Skip to content

Instantly share code, notes, and snippets.

@popovserhii
Last active March 27, 2020 10:59
Show Gist options
  • Save popovserhii/de3eacd05ca5a0c0bc6f84a3a057d56e to your computer and use it in GitHub Desktop.
Save popovserhii/de3eacd05ca5a0c0bc6f84a3a057d56e to your computer and use it in GitHub Desktop.
ZF3 PHPUnit Bootstrap
<?php
namespace StagemTest\Order;
use Psr\Container\ContainerInterface;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\ArrayUtils;
/**
* Test bootstrap, for setting up autoloading
*/
class Bootstrap
{
/** @var ContainerInterface */
protected static $serviceManager;
protected static $autoloader;
public static function init()
{
$zfModulePaths = [dirname(dirname(__DIR__))];
if (($path = static::findParentPath('vendor'))) {
$zfModulePaths[] = $path;
}
if (($path = static::findParentPath('module')) !== $zfModulePaths[0]) {
$zfModulePaths[] = $path;
}
static::initAutoloader();
self::$autoloader->setPsr4(__NAMESPACE__ . '\\' , array(__DIR__));
// use ModuleManager to load this module and its dependencies
$config = [
'module_listener_options' => [
'module_paths' => $zfModulePaths,
],
'modules' => [
'Stagem\Order', // enable modules which should be used in tests
],
];
// if DON'T NEED to use custom service manager
$smConfig = new ServiceManagerConfig();
$serviceManager = new ServiceManager();
$smConfig->configureServiceManager($serviceManager);
$serviceManager->setService('ApplicationConfig', $config);
$serviceManager->get('ModuleManager')->loadModules();
static::$serviceManager = $serviceManager;
// if NEED to use custom service manager
#$applicationConfig = require 'config/application.config.php.sample';
#$config = ArrayUtils::merge($config, $applicationConfig);
#$smConfig = new ServiceManagerConfig($config['service_manager']);
#$serviceManager = new ServiceManager();
#$smConfig->configureServiceManager($serviceManager);
#$serviceManager->setService('ApplicationConfig', $config);
#$serviceManager->get('ModuleManager')->loadModules();
#static::$serviceManager = $serviceManager;
// if NEED to use full project configuration
// @see http://stackoverflow.com/a/22906423/1335142
#$config = require 'config/application.config.php';
/** @var \Zend\Mvc\Application $result */
#$application = \Zend\Mvc\Application::init($config);
#static::$serviceManager = $application->getServiceManager();
##$serviceManager = new ServiceManager(new ServiceManagerConfig());
##$serviceManager->setService('ApplicationConfig', $config);
##$serviceManager->get('ModuleManager')->loadModules();
##static::$serviceManager = $serviceManager;
}
public static function chroot()
{
$rootPath = dirname(static::findParentPath('module'));
chdir($rootPath);
}
public static function getServiceManager()
{
return static::$serviceManager;
}
public static function getAutoloader()
{
return static::$autoloader;
}
protected static function initAutoloader()
{
$vendorPath = static::findParentPath('vendor');
if (file_exists($vendorPath . '/autoload.php')) {
static::$autoloader = include $vendorPath . '/autoload.php';
}
}
protected static function findParentPath($path)
{
$dir = __DIR__;
$previousDir = '.';
while (!is_dir($dir . '/' . $path)) {
$dir = dirname($dir);
if ($previousDir === $dir) {
return false;
}
$previousDir = $dir;
}
return $dir . '/' . $path;
}
}
Bootstrap::init();
Bootstrap::chroot();
<?php
namespace StagemTest\Order\Parser;
use Mockery;
use PHPUnit\Framework\TestCase;
use StagemTest\Order\Bootstrap;
class OrderParserTest extends TestCase
{
public function setUp()
{
$containter = Bootstrap::getServiceManager();
//$item = $container->get(...);
}
public function testOnePlusOneEqualsTwo()
{
$this->assertEquals(2, 1 + 1);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="Bootstrap.php" colors="true">
<testsuites>
<testsuite name="StagemTest/Order">
<directory>./</directory>
</testsuite>
</testsuites>
</phpunit>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment