I know only one way how to get numeric string keys: by converting object to array
$object = new stdClass();
$object->{'100'} = 0;
$object->strKey1 = 0;
$object->strKey2 = 0;
$array1 = (array) $object;
var_dump($array1);
//array(3) {
// '100' => -- string
// int(0)
// 'strKey1' =>
// int(0)
// 'strKey2' =>
// int(0)
//}
So, $array1 is not equal to this $array2
$array2 = array('100' => 0, 'strKey1' => 0, 'strKey2' => 0,);
var_dump($array2);
//array(3) {
// [100] => -- integer
// int(0)
// 'strKey1' =>
// int(0)
// 'strKey2' =>
// int(0)
//}
var_dump($array1 == $array2);
//bool(false)
It's not a bug: https://bugs.php.net/bug.php?id=61655
Also PHPUnit have its own rules of comparison.
$this->assertEquals($array1, $array1); // Fail
$this->assertEquals($array1, $array1); // Pass
https://github.com/sebastianbergmann/phpunit/blob/3.7/PHPUnit/Framework/Comparator/Array.php
Short description of PHPUnit comparison:
$expected = $array1;
$actual = $array1;
$remaining = $actual;
$equal = TRUE;
foreach ($expected as $key => $value){
unset($remaining[$key]);} // string numeric keys would not be unsetted.
if ($remaining)
$equal = FALSE;
var_dump($equal);
//bool(false)
So... you doing it right. To get "normal" array you need to recreate array. You could use foreach. But shoter way is in using serialization functions.
$this->assertEquals(unserialize(serialize($array1)), unserialize(serialize($array1)));
//Pass
But it does not look much better. :^ )
Also you could use assertSame
.