doude5860 2014-07-17 23:59
浏览 116
已采纳

在wordpress项目中使用phpunit中的方法调用

I'm trying to understand how phpunit is working and how to apply phpunit to my wordpress projects.

I am able to run the official wp-tests suite of wordpress and also i made some simple test with my wordpress plugins code.

Now I'm stuck on trying to use the so called "stubbing". php manual says: The practice of replacing an object with a test double that (optionally) returns configured return values is refered to as stubbing.

i just made a sample like the one on phpunit manual but once I'm calling the getMock() method I have always a fatal error

Fatal error: Call to undefined method SampleMockTest::generate()

I'm working on

  • windows 7
  • XAMPP 1.7.7 with PHP: 5.3.8
  • PHPUNIT 3.7.5
  • phpstorm as IDE

code for file some-class.php

<?php

class SomeClass {

  public function doSomething() {
    return true;
  }

  public function doSomethingElse() {
    return true;
  }

}
?>

code for file samplemock.php

<?php

require_once 'C:\xampp\htdocs\wp-plugin-dev\wordpress\wp-content\plugins\secure-attachments\lib\some-class.php';

class SampleMockTest extends PHPUnit_Framework_TestCase
{
  public function test_sample_mock()
  {

    $stub = $this->getMock('SomeClass');

    $stub->expects($this->any())
        ->method('doSomething')
        ->will($this->returnValue('foo'));

    $this->assertEquals('foo', $stub->doSomething());

  }
}
?>

I read a tons of blogs and questions but I was not able to understand where is the problem and at least how to investigate it. I lost one day struggling on this without results.

the error once I start the test I have the error below

Fatal error: Call to undefined method SampleMockTest::generate() in C:\xampp\htdocs\wp-plugin-dev\vendor\phpunit\phpunit-mock-objects\src\Framework\MockObject\Generator.php on line 224

any idea?

  • 写回答

1条回答 默认 最新

  • du3932066 2014-07-18 08:31
    关注

    The issue was solved simply updating the phpunit version. now I installed the latest version fo 3.7.* (3.7.37) and the code above is working like a charm.

    I'm not installing the latest version of PHPUnit (4.*) since it has some issue working with the phpstorm IDE, that probably can be solved in some way but now I'm focused on phpunit :)

    So the trick was simply the PHPUNIT version. I saw that the mock feature was introduced earlier than 3.7 so I thougth that 3.7.5 was not the problem but I was wrong! PHPUnit 3.7.37 works ok with the code above

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

报告相同问题?

悬赏问题

  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)