Created
October 10, 2016 11:21
-
-
Save samdark/e7f821ec790c1716832b9031daff77e5 to your computer and use it in GitHub Desktop.
PHPUnit array assertions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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