drgbpq5930 2013-12-27 01:48
浏览 47
已采纳

如何在Phalcon单元测试中引导我的依赖注入器服务?

I went through the Phalcon docs and got PHPUnit set up and working. However, I'm having trouble bootstrapping my existing services into the unit testing framework.

Currently, I load my config, routes, and services in the /public/index.php. For example, I'm loading the session library:

$di->set(
    'session',
    function () use ( $config ) {
        $session = new Phalcon\Session\Adapter\Redis(
            ... config ...
        );
    });

So now, in my application I'd have code that calls $this->session->get( 'user_id' ) to use the session library. I have many of these services -- one for SQL, MongoDB, cookies, etc.

The problem I'm having is correctly loading these services into the unit testing class. The Phalcon docs recommend loading my DI services in the UnitTestCase class (explained here) but I really do not want re-define the same services in this class loader. Additionally, I want to use the components in my application in the same ways; those components rely on the services being lazy-loaded.

So, to attempt this, I include the same /app/config/services.php file in my unit testing TestHelper.php init script thinking that I can just use the same $di object. This works in that my test cases can call $this->di->getSession()->get( 'var' ) but as soon as a component in my app tries to call $this->session->get( 'var' ) it throws an error:

1) Libraries\SessionTest::testGetSet
Undefined property: Libraries\SessionTest::$session

/home/mike/PhalconProject/app/library/Session.php:25
/home/mike/PhalconProject/tests/libraries/SessionTest.php:13

This error is telling me that my application session-management library Session.php is failing when accessing $this->session-> via the dependency injector.

Is there something I'm fundamentally doing wrong here? Do I need to redefine the services in my unit testing class? And if I do need to redefine them, will my application be able to load its services?

  • 写回答

2条回答 默认 最新

  • dtncv04228 2013-12-29 03:33
    关注

    So the solution here seems to be structuring the application libraries to statically access the DI services.

    The first step was to set the default DI in /public/index.php and /tests/bootstrap.php:

    \Phalcon\DI::setDefault( $di );
    

    Now, in the application libraries (like /app/Library/Auth.php or /app/Library/Session.php) the services are accessed statically (more info here in the Phalcon docs):

    $session = \Phalcon\DI::getDefault()->getSession();
    $session->get( 'user_id' )
    ...
    

    To make this easier, I set up a base library that all of my application libraries extend. The base library has a method to simplify this call.

    namespace Base;
    
    use \Phalcon\DI as DI;
    
    class Library extends \Phalcon\Mvc\User\Component
    {
        public function getService( $service )
        {
            $func = "get". ucfirst( $service );
            return DI::getDefault()->$func();
        }
    }
    

    Finally, /app/Library/Session.php would extend this \Base\Library class, and whenever I need the session service in a method I can just call:

    $session = self::getService( 'session' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图问题
  • ¥15 有段代码不知道怎么理解,const isToken = (config.headers || {}).isToken === false
  • ¥100 采用栈和深度优先算法对我的代码进行修改显示路径
  • ¥15 pointnet2包安装
  • ¥20 射频功率问题,解答者有酬谢!
  • ¥80 构建降雨和积水的预测模型
  • ¥15 #Qt Transform setTransform()在鼠标拖动移动视角是一致在原地不动,无法变换视角(细微观察似乎视图有在原地抖动),无法变换视角(细微观察似乎视图有在原地抖动)
  • ¥50 如何利用无人机拍摄的数码照片测量鸟卵的长短径
  • ¥100 github贡献者给与奖励
  • ¥15 使用DS18B20+ESP8266获取温度数据返回-127.00