douyun4524 2016-01-23 04:17
浏览 85

如何在单元测试时模拟在PHP中静态声明的方法

I created the following example test case:

<?php

abstract class Model
{
    //...

    public static function factory($data)
    {
        $className = get_called_class();
        $obj = new $className($data);
        return $obj;
    }
}

class User extends Model
{

}

class ExampleController
{
    protected $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function create()
    {
        return $this->user->factory(array('name' => 'Jim'));
    }
}

class ExampleTest extends PHPUnit_Framework_TestCase
{
    public function testSomething()
    {
        $user = new User(array('name' => 'Jim'));

        $modelStub = $this->getMockBuilder('User')
            ->disableOriginalConstructor()
            ->getMock();

        $modelStub
            ->method('factory')
            ->with(array('name' => 'Jim'))
            ->willReturn($user);

        $example = new ExampleController($modelStub);

        $this->assertEquals($user, $example->create());
    }
}

However I get the following error:

1) ExampleTest::testSomething
PHPUnit_Framework_MockObject_BadMethodCallException:

I seems to work fine when I remove the static keyword, then my test passes. But I want my Model class to also allow, in other cases, the option to call certain methods without having to instantiate first:

// when instantiation is required
$userModel = new User();
$user = $userModel->factory(array('name' => 'Jim'));

// called statically, no initial instantiation required
$user = User::factory(array('name' => 'Jim'));

I came across this blog which states that methods declared statically, yet called dynamically, is ok. However, methods declared dynamically, yet called statically, will throw a STRICT error - http://www.lornajane.net/posts/2010/declaring-static-methods-in-php

I have also used Laravel's Eloquent before and it appears that both method calls are possible there:

// Eloquent example without initial instantiation is possible too
$user = User::find(1);

Anyway, regardless of whether my code works, I want to be able to mock these methods declared statically. It seems upon reading that PHPUnit just doesn't handle static methods well (I read there was a staticExpects method, but now deprecated as of PHPUnit 3.8). So I'm about to embark on trying some alternative testing frameworks (Codeception and AspectMock, PHPSpec, mockery) as I haven't much experience with others. Would really appreciate some pointers for this issue or advice on the matter as it would really help too in unit testing legacy applications at our company, thanks

  • 写回答

2条回答 默认 最新

  • douhushen3241 2016-04-29 08:15
    关注

    The answer is AspectMock. This library give answers for this questions:

    How would you fake the time() function to produce the same result for each test call? Is there any way to stub a static method of a class? Can you redefine a class method at runtime?

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)