Skip to content

Instantly share code, notes, and snippets.

@woledzki
Created January 25, 2012 13:18
Show Gist options
  • Save woledzki/1676214 to your computer and use it in GitHub Desktop.
Save woledzki/1676214 to your computer and use it in GitHub Desktop.
hackish assertIsSubset
<?php
class SimpleTest extends \PHPUnit_Framework_TestCase {
protected function assertIsSubset(array $expected, $actual, $selector = '') {
$this->assertInternalType('array', $actual);
foreach ($expected as $key => $value) {
if(is_numeric($key) && !is_array($value)) {
// basic mode - for when $expected is a list of elements we expect to find in $actual
$this->assertTrue(in_array($value, $actual),
"Expected element [$selector.$value] is missing");
} else {
$this->assertTrue(array_key_exists($key, $actual),
"Expected key [$selector.$key] is missing");
if (is_array($value)) {
$this->assertTrue(is_array($actual[$key]),
"Expecting array for key [$selector.$key]");
$this->assertIsSubset($value, $actual[$key], $selector.'.'.$key);
} else {
$this->assertEquals($value, $actual[$key],
"Expected [$value] for key [$selector.$key] not equals to [{$actual[$key]}]");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment