stubbing a method call in phpunit in a wordpress project

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.

Read More

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:xampphtdocswp-plugin-devwordpresswp-contentpluginssecure-attachmentslibsome-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:xampphtdocswp-plugin-devvendorphpunitphpunit-mock-objectssrcFrameworkMockObjectGenerator.php on line 224

any idea?

Related posts

Leave a Reply

1 comment

  1. 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