An assertion compares the expected value with the actual one and stops the test with a readable message when they differ. Prefer the strictest one that fits:
| Assertion | Passes when |
|---|---|
| assertSame | ===: same type and value, same instance |
| assertEquals | ==: equal value objects, but also '10' and 10 |
| assertArraysAreIdenticalIgnoringOrder | same pairs in any order (new in 13) |
| assertJsonStringEqualsJsonString | two JSON documents are equivalent |
Here is a deliberately wrong expectation, to show what a failure looks like:
<?php
use PHPUnit\Framework\TestCase;
use Shop\{Money, Order, Product};
final class QuantitiesTest extends TestCase {
public function testQuantitiesAfterTwoAdds(): void {
$order = new Order();
$order->add(new Product('BK-PHP-01', 'Modern PHP in Practice', new Money(3990), 25), 2);
$order->add(new Product('AC-MUG-01', 'Semicolon Coffee Mug', new Money(1250), 60), 1);
$this->assertSame(['BK-PHP-01' => 2, 'AC-MUG-01' => 2], $order->quantities());
}
}Output
...
1) QuantitiesTest::testQuantitiesAfterTwoAdds
Failed asserting that two arrays are identical.
--- Expected
+++ Actual
@@ @@
Array &0 [
'BK-PHP-01' => 2,
- 'AC-MUG-01' => 2,
+ 'AC-MUG-01' => 1,
]
/home/dev/bookshop/tests/Unit/QuantitiesTest.php:10
...The diff marks the expected line - and the actual +, then gives the failing line. Correct the expectation to 1 and the test passes.