数组键匹配 PHPUnit

Array keys match PHPUnit

我正在尝试编写一个 PHPUnit 测试,它断言我正在测试的数组具有正确的键。

$structure = ['title', 'message', 'action'];
$structure = array_flip($structure);
$result = array_diff_key($structure, $response);

$this->assertEquals($result, []);

此测试有效,但必须有更简洁的方法来使用 PHPUnit 4.8 执行此操作?

你应该为每个键写一个断言:

$this->assertArrayHasKey('key', $response);  
$this->assertArrayHasKey('message', $response); 
$this->assertArrayHasKey('action', $response);

希望对您有所帮助