题
给定以下课程:
<?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,然后对此做出断言,但是是否有另一种方法可以做我想做的事?
解决方案 3
我最终根据属性创建了自己的约束类
<?php
class Test_Constraint_ArrayHas extends PHPUnit_Framework_Constraint
{
protected $arrayKey;
protected $constraint;
protected $value;
/**
* @param PHPUnit_Framework_Constraint $constraint
* @param string $arrayKey
*/
public function __construct(PHPUnit_Framework_Constraint $constraint, $arrayKey)
{
$this->constraint = $constraint;
$this->arrayKey = $arrayKey;
}
/**
* Evaluates the constraint for parameter $other. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
* @param mixed $other Value or object to evaluate.
* @return bool
*/
public function evaluate($other)
{
if (!array_key_exists($this->arrayKey, $other)) {
return false;
}
$this->value = $other[$this->arrayKey];
return $this->constraint->evaluate($other[$this->arrayKey]);
}
/**
* @param mixed $other The value passed to evaluate() which failed the
* constraint check.
* @param string $description A string with extra description of what was
* going on while the evaluation failed.
* @param boolean $not Flag to indicate negation.
* @throws PHPUnit_Framework_ExpectationFailedException
*/
public function fail($other, $description, $not = FALSE)
{
parent::fail($other[$this->arrayKey], $description, $not);
}
/**
* Returns a string representation of the constraint.
*
* @return string
*/
public function toString ()
{
return 'the value of key "' . $this->arrayKey . '"(' . $this->value . ') ' . $this->constraint->toString();
}
/**
* Counts the number of constraint elements.
*
* @return integer
*/
public function count ()
{
return count($this->constraint) + 1;
}
protected function customFailureDescription ($other, $description, $not)
{
return sprintf('Failed asserting that %s.', $this->toString());
}
可以这样使用:
... ->with(new Test_Constraint_ArrayHas($this->equalTo($value), $key));
其他提示
这 $this->arrayHasKey('keyname');
方法存在,但其名称为 assertArrayHasKey
:
// In your PHPUnit test method
$hi = array(
'fr' => 'Bonjour',
'en' => 'Hello'
);
$this->assertArrayHasKey('en', $hi); // Succeeds
$this->assertArrayHasKey('de', $hi); // Fails
代替创建可重复使用的约束类,我能够使用phpunit中的现有回调约束来主张数组密钥的值。在我的用例中,我需要检查第二个参数中的数组值对模拟方法(Mongocollection :: SuseIndex(), ,如果有人好奇)。这是我想到的:
$mockedObject->expects($this->once())
->method('mockedMethod')
->with($this->anything(), $this->callback(function($o) {
return isset($o['timeout']) && $o['timeout'] === 10000;
}));
这 回调约束 期望其构造函数中有一个可可,并在评估过程中简单地调用它。主张通过可可返回对还是错,通过或失败。
对于一个大型项目,我当然建议您创建一个可重复使用的约束(如上述解决方案)或请愿 PR#312 合并为phpunit,但这是一次性需求的技巧。很容易看出回调约束也可能对更复杂的断言有用。
如果您希望对该参数进行一些复杂的测试,并且还具有有用的消息和比较,则总有可以在回调中放置断言的选择。
例如
$clientMock->expects($this->once())->method('post')->with($this->callback(function($input) {
$this->assertNotEmpty($input['txn_id']);
unset($input['txn_id']);
$this->assertEquals($input, array(
//...
));
return true;
}));
请注意,回调返回true。否则,它将永远失败。
抱歉,我不是说英语的人。
我认为您可以通过Array_key_exists函数来测试数组中是否存在键,并且可以测试是否存在Array_search的值是否存在
例如:
function checkKeyAndValueExists($key,$value,$arr){
return array_key_exists($key, $arr) && array_search($value,$arr)!==false;
}
利用 !==
因为Array_search如果存在,则返回该值的键,并且可能为0。