如何验证一个键是否在 Behat 中的数组中
How to verify if a key is into array in Behat
我的 behat 测试有问题。所以我有这样的场景:
Scenario: Teste la route /settlement/rank/types
Given I request "http:/localhost/admin"
Then the response should be JSON
And I should have code with value 200
And I should have error with value 0
And The response has a "aUser" property
And This property "aUser" is not empty
And The property "aUser" has the keys:
|id_user |login |first_name |last_name |email |id_company |enable |id_language |label_language |
现在 FeatureContext 是:
/**
* @Given /^The property "([^"]*)" has the keys:$/
*/
public function thePropertyHasTheKeys($aProperty, TableNode $table){
$data = json_decode($this->_response->getBody(true),true);
foreach($table as $value){
print_r($value['id_user']);
if(!in_array($value,$data['data'][$aProperty])){
throw new Exception("The property ".$value. " is not set\n");
}
}
}
在 $data
中,我有一个具有这种形式的数组:
|id_user |login |first_name |last_name |email |id_company |enable |id_language |label_language |
所以我想比较数组 $data
和数组 $table
,如果键相同。
也许是这样的?
$data = json_decode($this->_response->getBody(true),true);
foreach($table as $key=>$value)
{
if( !isset($data[$key]) ) { throw new Exception("The key ".$key. " is not set\n"); }
if( $data[$key]!=$value ) { throw new Exception("The value ".$value. " is wrong\n"); }
}
我喜欢重用其他人的代码,以便更清楚地了解正在发生的事情 - 在这种情况下,PHPunit 可以很好地处理很多事情:
// outside the test class
require_once 'PHPUnit/Framework/Assert/Functions.php';
// convert $table to a clean array ....
// compare
$data = json_decode($this->_response->getBody(true), true);
assertEquals($table, $data, 'Decoded JSON does not match the given example');
我的 behat 测试有问题。所以我有这样的场景:
Scenario: Teste la route /settlement/rank/types
Given I request "http:/localhost/admin"
Then the response should be JSON
And I should have code with value 200
And I should have error with value 0
And The response has a "aUser" property
And This property "aUser" is not empty
And The property "aUser" has the keys:
|id_user |login |first_name |last_name |email |id_company |enable |id_language |label_language |
现在 FeatureContext 是:
/**
* @Given /^The property "([^"]*)" has the keys:$/
*/
public function thePropertyHasTheKeys($aProperty, TableNode $table){
$data = json_decode($this->_response->getBody(true),true);
foreach($table as $value){
print_r($value['id_user']);
if(!in_array($value,$data['data'][$aProperty])){
throw new Exception("The property ".$value. " is not set\n");
}
}
}
在 $data
中,我有一个具有这种形式的数组:
|id_user |login |first_name |last_name |email |id_company |enable |id_language |label_language |
所以我想比较数组 $data
和数组 $table
,如果键相同。
也许是这样的?
$data = json_decode($this->_response->getBody(true),true);
foreach($table as $key=>$value)
{
if( !isset($data[$key]) ) { throw new Exception("The key ".$key. " is not set\n"); }
if( $data[$key]!=$value ) { throw new Exception("The value ".$value. " is wrong\n"); }
}
我喜欢重用其他人的代码,以便更清楚地了解正在发生的事情 - 在这种情况下,PHPunit 可以很好地处理很多事情:
// outside the test class
require_once 'PHPUnit/Framework/Assert/Functions.php';
// convert $table to a clean array ....
// compare
$data = json_decode($this->_response->getBody(true), true);
assertEquals($table, $data, 'Decoded JSON does not match the given example');