Skip to content

Instantly share code, notes, and snippets.

@samdark
Created October 10, 2016 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samdark/e7f821ec790c1716832b9031daff77e5 to your computer and use it in GitHub Desktop.
Save samdark/e7f821ec790c1716832b9031daff77e5 to your computer and use it in GitHub Desktop.
PHPUnit array assertions
public static function assertArrayStructure($keys, $array)
{
$arrayKeys = array_keys($array);
$missing = array_diff($keys, $arrayKeys);
$unexpected = array_diff($arrayKeys, $keys);
$message = 'Keys are wrong.';
if ($missing !== []) {
$message .= ' Missing: ' . implode(', ', $missing) . '.';
}
if ($unexpected !== []) {
$message .= ' Unexpected: ' . implode(', ', $unexpected) . '.';
}
$message .= "\n" . json_encode($array, JSON_PRETTY_PRINT);
self::assertTrue($missing === [] && $unexpected === [], $message);
}
public static function assertArrayValues($expected, $actual)
{
foreach ($expected as $key => $value) {
self::assertArrayHasKey($key, $actual);
self::assertEquals($value, $actual[$key]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment