dtx6087 2013-09-03 16:00
浏览 48
已采纳

剔除静态方法调用

So I was writing my tests for PHPUnit when a test started exploding all over the place. Its main reason was that Config::get() was throwing - undefined get() for class Core.class.php

This class is called in the test as such:

class CoreTest extends PHPUnit_Framework_TestCase {

    protected $object;

    protected function setUp() {
        $this->object = new Core;
    }

    // .. other tests
}

So I went and investigated the class and I saw that the construct has the following:

public function __construct() {        
    $this->absUrl  = Config::get(Config::ABS_URL);
    $this->baseDir = Config::get(Config::BASE_DIR);
    $this->session = new Session('core');
    return $this;
}

is there a way I can stub these out? or deal with them in a way that the test does not explode?

I was reading This information on stubbing static methods but I am not sure how to apply it here. Thanks.

  • 写回答

1条回答 默认 最新

  • douyi8408 2013-09-03 17:07
    关注

    You would have to mock the class, and not execute the constructor, but rather, build in the values you need and then use the mock in your tests.

    Do you have a sample of what you have tried? You likely need to modify the Core() class to support dependency injection (the Config object and the Session object).

    $stub = $this->getMock('Core');
    
    // Configure the stub.
    $stub->expects($this->any())
         ->method('CreateSession')
         ->will($this->returnValue('foo'));
    
    $this->assertEquals('foo', $stub->CreateSession());
    

    In your example code though, you will likely need to modify the Core() class to accept the Session and Config objects to be passed (either via the constructor, or via a Set dependency) and some modifications to the Session class as well.

    class Core
    {
        private $SessionObject;
        private $ConfigObject;
        public function __construct(Config $Config, Session $Session)           // Constructor Dependency
        {
            $this->ConfigObject = $Config;
            $this->absUrl  = $this->ConfigObject::get(Config::ABS_URL);
            $this->baseDir = $this->ConfigObject::get(Config::BASE_DIR);
            $this->session = $Session;
            $this->session->SetSessionType('core');
            return $this;
        }
    }
    

    or

    class Core
    {
        private $SessionObject;
        private $ConfigObject;
        public function __construct()           
        {
        }
    
        // Set Dependencies
        public function SetConfigObject(Config $Config)
        {
            $this->ConfigObject = $Config;
        }
    
        public function SetSessionObject(Session $Session)
        {
            $this->SessionObject = $Session;
        }
    
        public function BuildObject($SessionType)
        {
            $this->absUrl  = $this->ConfigObject::get(Config::ABS_URL);
            $this->baseDir = $this->ConfigObject::get(Config::BASE_DIR);
            $this->session->SetSessionType($SessionType);
        }
    }
    

    Now your production code will pass the Config and Session objects properly, and you then use Mock objects in your tests to pass objects with the states that you need, to return hard set values when calling the get method on the object.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集