PHPUnit Mock Objects and Static Methods

后端 未结 7 1465
梦谈多话
梦谈多话 2020-11-27 18:02

I am looking for the best way to go about testing the following static method (specifically using a Doctrine Model):

class Model_User extends Doctrine_Record         


        
7条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 18:40

    Sebastian Bergmann, the author of PHPUnit, recently had a blog post about Stubbing and Mocking Static Methods. With PHPUnit 3.5 and PHP 5.3 as well as consistent use of late static binding, you can do

    $class::staticExpects($this->any())
          ->method('helper')
          ->will($this->returnValue('bar'));
    

    Update: staticExpects is deprecated as of PHPUnit 3.8 and will be removed completely with later versions.

提交回复
热议问题