问题描述
我正在将PHPUnit集成到我的Phalcon项目中.我已经使其在MAMP中正常运行,但是当我在服务器上运行phpunit
时,总是出现一些错误.
I'm integrating PHPUnit in my Phalcon project. I had it running correctly in MAMP, but when I run phpunit
on my server, I keep getting some errors.
这是UnitTestCase:
This is UnitTestCase:
<?php
use \Phalcon\Di;
use \Phalcon\DI\FactoryDefault;
use \Phalcon\Test\UnitTestCase as PhalconTestCase;
use \Phalcon\Mvc\View;
use \Phalcon\Crypt;
use \Phalcon\Mvc\Dispatcher;
use \Phalcon\Mvc\Dispatcher as PhDispatcher;
use \Phalcon\Mvc\Url as UrlResolver;
use \Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use \Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use \Phalcon\Mvc\Model\Metadata\Files as MetaDataAdapter;
use \Phalcon\Session\Adapter\Files as SessionAdapter;
use \Phalcon\Flash\Direct as Flash;
use \Phalcon\Logger;
use \Phalcon\Events\Manager as EventsManager;
use \Phalcon\Logger\Adapter\File as LoggerFile;
use \Phalcon\Mvc\Model\Manager as ModelsManager;
abstract class UnitTestCase extends PhalconTestCase
{
/**
* @var \Voice\Cache
*/
protected $_cache;
/**
* @var \Phalcon\Config
*/
protected $_config;
/**
* @var bool
*/
private $_loaded = false;
public function setUp(Phalcon\DiInterface $di = NULL, Phalcon\Config $config = NULL)
{
// Load any additional services that might be required during testing
$di = new FactoryDefault();
DI::reset();
$config = include APP_DIR . '/config/config.php';
/**
* The URL component is used to generate all kind of urls in the application
*/
$di->set('url', function () use ($config) {
$url = new UrlResolver();
$url->setBaseUri($config->application->baseUri);
return $url;
}, true);
/**
* Setting up the view component
*/
$di->set('view', function () use ($config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
$view->registerEngines(array(
'.volt' => function ($view, $di) use ($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array(
'compiledPath' => $config->application->cacheDir . 'volt/',
'compiledSeparator' => '_'
));
return $volt;
}
));
return $view;
}, true);
...and some more...
$di->set(
'modelsManager',
function()
{
return new \Phalcon\Mvc\Model\Manager();
}
);
parent::setUp($di, $config);
$this->_loaded = true;
}
/**
* Check if the test case is setup properly
*
* @throws \PHPUnit_Framework_IncompleteTestError;
*/
public function __destruct()
{
if (!$this->_loaded) {
throw new \PHPUnit_Framework_IncompleteTestError('Please run parent::setUp().');
}
}
}
因此,当我在本地计算机上运行此程序时,它工作正常.当我在服务器上运行它时,它会抛出:
So, when I run this on my local machine, this works fine. When I run it on the server, it throws:
Phalcon\Di\Exception: Service 'modelsManager' wasn't found in the dependency injection container
我做错了什么?
推荐答案
我只是遇到了同样的问题,花了一些时间才弄清楚.我显然确实将所有服务都包含在单独的引导"文件中,以防止丢失/复制DI服务.
I just ran into the same issue and took me some time to figure it out. I obviously do include all the services from a separated 'bootstrap' file to prevent missing/duplicating DI services.
通过添加以下内容为我解决了该问题:
The issue was resolved for me by adding:
/**
* Function to setup the test case
*/
public function setUp()
{
// Get reference to the global DI var (with all my registered services):
global $di;
// Setup parent:
parent::setUp();
// --------------------------------------------------
// THIS IS WHAT SOLVED IT FOR ME, since Phalcon
// Set default DI
// uses the \Phalcon\Di::getDefault() for handling queries from a model:
\Phalcon\Di::setDefault($di);
// --------------------------------------------------
// Set the DI:
$this->setDI($di);
}
这篇关于未在Phalcon单元测试中设置modelsManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!