php - SimpleTest - モックオブジェクトのメソッドへのすべての呼び出しを一覧表示します。
-
13-12-2019 - |
質問
私の問題は、私がそれらをチェックする必要があるので、私はモックオブジェクトへのすべての呼び出しをリストする必要があることです。SimpleTestのマニュアルでこの機能について何も見つけていません。:s
私のコードをテストするもう1つの方法があります:
class Clean_Collection_Tree_NestedArrayParser {
protected $path = array();
protected $depth = -1;
/** @var Clean_Collection_Tree_MapTreeInterface */
protected $tree;
public function setBasePath(array $path) {
$this->path = $path;
}
public function setTree(Clean_Collection_Tree_MapTreeInterface $tree) {
$this->tree = $tree;
}
public function parse($subject) {
$this->parseArray($subject);
}
public function parseArray(array $array) {
++$this->depth;
foreach ($array as $key => $value) {
$this->path[$this->depth] = $key;
if (is_array($value)) {
$this->tree->put($this->path, new Clean_Collection_Map_Map());
$this->parseArray($value);
} else
$this->tree->put($this->path, $value);
}
if (!empty($array))
array_pop($this->path);
--$this->depth;
}
}
.
これは、Mapオブジェクトツリーを作成するつもりのネストされた配列を待っているパーサです。 SetTree(Clean_Collection_Tree_MapTreeInterface $ tree)で実際のツリーを注入し、マップツリーインターフェイスは次のとおりです。
interface Clean_Collection_Tree_MapTreeInterface extends Clean_Collection_CollectionInterface {
public function putAll(array $array);
public function put(array $path, $value);
public function get(array $path);
public function getAll(array $pathes);
public function removeAll(array $pathes);
public function remove(array $path);
public function contains(array $path);
}
.
パーサーは put(array> put、$ value)メソッドのみを使用します。そのため、いわゆるPUTメソッドすべてのリストを表示すると、パーサーに何がうまくいかなかったかが表示されます。(SimpleMockにこの機能がない場合、私たちがインターフェースを実装する私自身のモックオブジェクトを作成できます。私はその上にいます。)
解決
問題はSimpleMockクラスのデザインにあります:
protected function addCall($method, $args) {
if (! isset($this->call_counts[$method])) {
$this->call_counts[$method] = 0;
}
$this->call_counts[$method]++;
}
.
SimpleMockでプロパティを設定する代わりに、コールプロパティをログに記録するためのロガークラスを作成したはずです... SimpleMockクラスを拡張することで回避策を作成できます。
class LoggedMock extends SimpleMock {
protected $invokes = array();
public function &invoke($method, $args) {
$this->invokes[] = array($method, $args);
return parent::invoke($method, $args);
}
public function getMockInvokes() {
return $this->invokes;
}
}
.
と基本モッククラスとして設定します。
require_once __DIR__.'simpletest/autorun.php';
SimpleTest::setMockBaseClass('LoggedMock');
.
その後、 $ mockobj-> getMockinvokes()。
でInvokeリストを取得できます。edit :Invokeメソッドの最初の行にはメソッド名が小文字に変換されるため、ADDCALLを拡張できませんので、ADDCALLを拡張することで、小文字の形式のみを記録できます。ラクダケース。(私は小文字の変換が間違っていると思います...)
私はデモのためのテストを作成しました:
interface Nike {
public function justDoIt();
}
class NikeUser {
protected $nike;
public function setNike(Nike $nike) {
$this->nike = $nike;
}
public function doIt() {
$this->nike->justDoIt();
}
}
Mock::generate('Nike', 'MockNike');
class NikeUserTest extends UnitTestCase {
public function testDoItButWeDontWantJustDoIt() {
$mockNike = new MockNike();
$nikeUser = new NikeUser();
$nikeUser->setNike($mockNike);
$expectedInvokes = array();
$nikeUser->doIt();
$expectedInvokes[] = array('justDoIt', array());
$this->assertEqual($expectedInvokes, $mockNike->getMockInvokes());
}
}
.