Reputation: 13
I have an issue bootstapping zf2 and hooking it into behat
I have the following snippets that fails
/** @BeforeSuite */
public static function setup(SuiteEvent $event) {
chdir(dirname(dirname(dirname(dirname(__DIR__)))));
require 'init_autoloader.php';
$app = Zend\Mvc\Application::init(require 'config/application.config.php');
}
This has been a way of bootstrapping a zf2 app and I really can't figure out what to do.
Will really be grateful for any help
Upvotes: 0
Views: 453
Reputation: 17604
To init() the application means to init the MVC procedure. You don't want to run any controllers, you just want to bootstrap the Service Manager.
This class by Marco Pivetta will come in handy:
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/
namespace CommonTest;
use Zend\ServiceManager\ServiceManager;
use Zend\Mvc\Service\ServiceManagerConfig;
/**
* Utility used to retrieve a freshly bootstrapped application's service manager
*
* @author Marco Pivetta <[email protected]>
*/
class ServiceManagerFactory
{
/**
* @var array
*/
protected static $config;
/**
* @param array $config
*/
public static function setConfig(array $config)
{
static::$config = $config;
}
/**
* @static
* @return array
*/
public static function getConfiguration()
{
return static::$config;
}
/**
* Builds a new service manager
*/
public function getServiceManager()
{
$serviceManager = new ServiceManager(new ServiceManagerConfig(
isset(static::$config['service_manager']) ? static::$config['service_manager'] : []
));
$serviceManager->setService('ApplicationConfig', static::$config);
$serviceManager->setFactory('ServiceListener', 'Zend\Mvc\Service\ServiceListenerFactory');
/** @var $moduleManager \Zend\ModuleManager\ModuleManager */
$moduleManager = $serviceManager->get('ModuleManager');
$moduleManager->loadModules();
return $serviceManager;
}
}
Put this class under your tests' namespace and make sure it is available via the autoloader.
In your setup() method, you need to set the SM config, like this:
public static function setUp() {
$file = '/path/to/your/application.config.php';
\CommonTest\ServiceManagerFactory::setConfig($file);
}
Then, if you need the Service Manager, anywhere in your tests just call $sm = \CommonTest\ServiceManagerFactory::getServiceManager()
to get it.
Upvotes: 1
Reputation: 66
If you need to use the ServiceManager inside yout Context you might be interested in this extension https://github.com/mvlabs/zf2behat-extension.
Upvotes: 0