Core\Test::assertEqualsDeep
Core\Test::assertEqualsDeep() compares two values part by part. It goes into every array and every object and compares what is inside. Two objects are equal when they have the same class and every property is equal. The objects do not need a compareTo method.
When the values are different, the check throws a Core\Test\Failure. The message gives the path to the first difference, written the way you write it in code, such as $actual->lines["0"]->quantity. It also shows the two values at that place.
The check goes at most 64 levels deep. A value nested deeper than that throws an error, so an object that points back to itself cannot make the check run forever.
The examples below show two nested arrays, the message of a failed check, and a test of an order with its lines.
Parameters
$actualTThe value under test.$expectedTThe value$actualmust match everywhere.Tis the type of$actual, so$expectedmust have the same type. Otherwise the call does not compile.
Options
messagestringoptionalYour own text, added at the start of the failure message. The default is no text.
Return value
Returns void. Nothing. The check is recorded as passed.
Errors
Core\Test\FailureThe two values differ. The message gives the path to the first difference, written the way code indexes into$actual. The failure is recorded before it is thrown.RuntimeErrorThe values are nested more than 64 levels deep.
Examples
Compare nested arrays
<?nvs// Shows how to compare two nested arrays, one level at a time.
array<array<int>> $stock = [ 'apples' => ['shelf' => 12, 'store' => 40], 'pears' => ['shelf' => 3, 'store' => 0],];
// Every key and every value is compared, inside every inner array too.// The two arrays are equal, so the check passes.Core\Test::assertEqualsDeep($stock, [ 'apples' => ['shelf' => 12, 'store' => 40], 'pears' => ['shelf' => 3, 'store' => 0],]);echo "the stock list is correct\n";the stock list is correctFind the difference
<?nvs// Shows how a failed check tells you where two values differ.
array<array<int>> $stock = [ 'apples' => ['shelf' => 12, 'store' => 40], 'pears' => ['shelf' => 3, 'store' => 0],];
// Only one number is different. The message gives its path, the way you write// it in code, and the two values at that place.try { Core\Test::assertEqualsDeep($stock, [ 'apples' => ['shelf' => 12, 'store' => 40], 'pears' => ['shelf' => 5, 'store' => 0], ]);} catch (Core\Test\Failure $failure) { echo $failure->message, "\n";}
// When the arrays have a different number of entries, the message says so.try { Core\Test::assertEqualsDeep(['a', 'b'], ['a', 'b', 'c']);} catch (Core\Test\Failure $failure) { echo $failure->message, "\n";}Core\Test::assertEqualsDeep failed: the two differ at `$actual["pears"]["shelf"]`: `$actual` is 3, `$expected` is 5Core\Test::assertEqualsDeep failed: the two differ at the value itself: `$actual` is an array of 2, `$expected` is an array of 3Test an order built from a cart
<?nvs// Shows how to test an object that contains other objects, such as an order and its lines.
class Line{ public function constructor(public string $sku, public int $quantity) {}}
class Order{ public function constructor(public string $customer, public array<Line> $lines) {}}
class Checkout{ // Adds up the quantities of each product in the cart. public static function order(string $customer, array<string> $cart): Order { array<int> $counts = []; foreach ($cart as string $sku) { // `??+=` starts a missing key at 0, then adds 1. $counts[$sku] ??+= 1; } array<Line> $lines = []; foreach ($counts as string $sku => int $quantity) { $lines[] = new Line($sku, $quantity); } return new Order($customer, $lines); }}
Order $order = Checkout::order('customer-7', ['tea', 'cup', 'tea']);
// The objects have no `compareTo` method. This check compares the class// and every property, inside the lines as well.Core\Test::assertEqualsDeep($order, new Order('customer-7', [new Line('tea', 2), new Line('cup', 1)]));echo "the order has 2 tea and 1 cup\n";
// A wrong quantity gives the path to the property that is different.try { Core\Test::assertEqualsDeep($order, new Order('customer-7', [new Line('tea', 1), new Line('cup', 1)]));} catch (Core\Test\Failure $failure) { echo $failure->message, "\n";}the order has 2 tea and 1 cupCore\Test::assertEqualsDeep failed: the two differ at `$actual->lines["0"]->quantity`: `$actual` is 2, `$expected` is 1