PHPUnit和specking方法别名

时间:2014-04-27 17:28:52

标签: php phpunit

考虑这个课程

class Foo {      

  public function alias_method($input) {
    return $this->actual_method($input);
  }

  public function actual_method($input) {
    # Do something
  }
}

现在,我已经编写了用于验证actual_method行为的测试,因此对于alias_method,我想要确保的是它使用actual_method调用$input

我该怎么做?

1 个答案:

答案 0 :(得分:0)

正如您所要求的代码示例一样,这是一个PHPUnit测试方法体,已经有了您的期望设置:

/**
 * TODO Find out whether or not the test is necessary
 *
 * Note: This example is a slight adoption from the Observer/Subject
 *       mock example from PHPUnit docs:
 *       Example 9.11: Testing that a method gets called once and with 
 *       a specified argument
 *
 * @link http://phpunit.de/manual/current/en/test-doubles.html
 *
 * @test
 */
public function methodIsCalledWithInputArgument()
{
    // Create an Input object to test with as method argument
    $input = new Input();

    // Create a mock for the Foo class,
    // only mock the actual_method() method.
    $foo = $this->getMock('Foo', array('actual_method'));

    // Set up the expectation for the actual_method() method
    // to be called only once and with the argument $input
    // as its parameter.
    $foo->expects($this->once())
       ->method('actual_method')
       ->with($this->identicalTo$(input))
    ;

    // Call the alias_method() method on the $foo object
    // which we expect to call the mocked Foo object's
    // actual_method() method with $input.
    $foo->alias_method($input);
}