模拟某些东西PHPUnit
会创建一个你告诉它模拟的类的子类。
如果Bar
实现了迭代器,你BarMock
也将实现迭代器。
示例.php
<?php
interface myInterface {
public function myInterfaceMethod();
}
class Bar implements myInterface {
public function myInterfaceMethod() {
}
}
class TestMe {
public function iNeedABar(Bar $bar) {
if ($bar instanceOf myInterface) {
echo "Works";
}
}
}
class TestMeTest extends PHPUnit_Framework_TestCase {
public function testBar() {
$class = new TestMe();
$bar = $this->getMock('Bar');
$class->iNeedABar($bar);
}
}
输出:
phpunit Sample.php
PHPUnit 3.7.8 by Sebastian Bergmann.
.Works
Time: 0 seconds, Memory: 5.25Mb
OK (1 test, 0 assertions)