一個關鍵考慮下面的類:的PHPUnit:驗證陣列與給定值
<?php
class Example {
private $Other;
public function __construct ($Other)
{
$this->Other = $Other;
}
public function query()
{
$params = array(
'key1' => 'Value 1'
, 'key2' => 'Value 2'
);
$this->Other->post($params);
}
}
這測試用例:
<?php
require_once 'Example.php';
require_once 'PHPUnit/Framework.php';
class ExampleTest extends PHPUnit_Framework_TestCase {
public function test_query_key1_value()
{
$Mock = $this->getMock('Other', array('post'));
$Mock->expects($this->once())
->method('post')
->with(YOUR_IDEA_HERE);
$Example = new Example($Mock);
$Example->query();
}
如何驗證$params
(這是一個數組)和傳遞給$Other->post()
包含名爲'key1'的鍵值爲'值1'?
我不想驗證所有的數組 - 這只是一個示例代碼,在實際代碼中,傳遞的數組有更多的值,我想驗證那裏只有一個鍵/值對。
有$this->arrayHasKey('keyname')
,我可以用它來驗證密鑰是否存在。
還有$this->contains('Value 1')
,它可以用來驗證數組是否有這個值。
我甚至可以結合這兩個與$this->logicalAnd
。但是這當然不能達到預期的效果。
到目前爲止,我一直在使用returnCallback,捕獲整個$ params,然後對此做出斷言,但是可能有另一種方法來做我想做的事情嗎?
$ this-> attribute(Constraint,value)能夠完成我之後的工作,但它不適用於數組。 – 2009-09-28 14:21:35