在Phalcon Unittest中没有设置modelsManager[英] modelsManager not set in Phalcon Unittest

问题描述

我正在将 PHPUnit 集成到我的 Phalcon 项目中.我让它在 MAMP 中正常运行,但是当我在服务器上运行 phpunit 时,我不断收到一些错误.

这是 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().');
        }
    }
}

所以,当我在本地机器上运行它时,它工作正常.当我在服务器上运行它时,它会抛出:

Phalcon\Di\Exception: Service 'modelsManager' wasn't found in the dependency injection container

我做错了什么?

推荐答案

我刚刚遇到了同样的问题,花了一些时间才弄清楚.我显然确实包含了一个单独的"引导程序"文件中的所有服务,以防止丢失/重复 DI 服务.

通过添加以下内容为我解决了问题:

 /**
 * 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);
}

本文地址:https://www.itbaoku.cn/post/1739802.html