一个关键考虑下面的类:的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