考虑
class Foo {
public $att;
public function __construct ( $a ) { $this->att = $a; }
}
class Some {
public function callMe ( Foo $f ) {}
}
// class I want to test
class SuT {
public function testMe ( Some $s ) {
echo $s->callMe( new Foo('hi') );
}
}
Run Code Online (Sandbox Code Playgroud)
我想检查是否Sut::testMe()正确调用Some::callMe().由于参数是一个(Foo)对象(不是标量类型),我无法弄清楚如何调用PHPUnit with()来对其运行断言.assertAttributeEquals例如,有一种方法,但我如何向它提供调用的参数?
我想做的是:
class SuTTest extends PHPUnit_Framework_TestCase {
public function testSuT () {
$stub = $this->getMock( 'Some' …Run Code Online (Sandbox Code Playgroud) <?php
class Super {
public $my;
public function __construct ( $someArg ) {
if ( class_exists('Sub') ) { // or some other condition
return new Sub( $someArg );
}
$this->my = $someArg;
}
}
class Sub extends Super {}
?>
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为new Super()它将是一个"空" Super对象(所有成员都是NULL).(PHP不允许分配$this,因此$this = new Sub()也不起作用).
我知道正确的模式将是这里的工厂.但这需要对代码进行大量更改,所以我想知道是否可以这样做.既然Sub是-a Super,我不明白为什么不应该从OOP的角度来限制它.