我有一個我想模擬的界面。我知道我可以模擬該接口的實現,但有沒有辦法模擬接口?我可以用PHPUnit模擬一個接口實現嗎?
<?php
require __DIR__ . '/../vendor/autoload.php';
use My\Http\IClient as IHttpClient; // The interface
use My\SomethingElse\Client as SomethingElseClient;
class SomethingElseClientTest extends PHPUnit_Framework_TestCase {
public function testPost() {
$url = 'some_url';
$http_client = $this->getMockBuilder('Cpm\Http\IClient');
$something_else = new SomethingElseClient($http_client, $url);
}
}
我來到這裏是:
1) SomethingElseTest::testPost
Argument 1 passed to Cpm\SomethingElse\Client::__construct() must be an instance of
My\Http\IClient, instance of PHPUnit_Framework_MockObject_MockBuilder given, called in
$PATH_TO_PHP_TEST_FILE on line $NUMBER and defined
有趣的是,PHPUnit, mocked interfaces, and instanceof建議這可能工作。
你誤解了,其他的問題,它是使用' - > getMock()'不' - > getMockBuilder()'因爲你這樣做 - 那就是你找出你的問題的答案。然而,IIRC在這裏也存在你的問題的重複,但我現在找不到它。 – hakre
有趣。我在搜索中找不到它。感謝編輯。 –
您還可以在下面接受您的回答,以便您的問題被標記爲已回答。 – hakre