{"record":{"id":"ec1c09cf573b9e7b","repo":"sebastianbergmann/comparator","slug":"failed-asserting-that-two-arrays-are-equal","errorCode":null,"errorMessage":"Failed asserting that two arrays are equal.","messagePattern":"Failed asserting that two arrays are equal\\.","errorType":"exception","errorClass":"ComparisonFailure","httpStatus":null,"severity":"error","filePath":"src/ArrayComparator.php","lineNumber":177,"sourceCode":"                    \"    %s\\n\",\n                    $exporter->shortenedExport($value),\n                );\n            } else {\n                $actualAsString .= sprintf(\n                    \"    %s => %s\\n\",\n                    $exporter->export($key),\n                    $exporter->shortenedExport($value),\n                );\n            }\n\n            $equal = false;\n        }\n\n        $expectedAsString .= ')';\n        $actualAsString .= ')';\n\n        if (!$equal) {\n            throw new ComparisonFailure(\n                $expected,\n                $actual,\n                $expectedAsString,\n                $actualAsString,\n                'Failed asserting that two arrays are equal.',\n                $this->contextLines(),\n            );\n        }\n    }\n\n    private function indent(string $lines): string\n    {\n        return trim(str_replace(\"\\n\", \"\\n    \", $lines));\n    }\n\n    private function compare(mixed $a, mixed $b): int\n    {\n        $typeOrderA = $this->typeOrder($a);","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/sebastianbergmann/comparator/blob/00837a9d227b4e123b0b42a91736960f6b0b3583/src/ArrayComparator.php#L159-L195","documentation":"ComparisonFailure thrown by ArrayComparator::assertEquals when two arrays do not contain the same key-value pairs: a key exists in only one array, or a value at a shared key differs (recursively via child comparators). The exception carries exported expected/actual array strings for diffing. This is the standard assertion-failure signal of sebastian/comparator, normally surfaced by PHPUnit assertions like assertEquals.","triggerScenarios":"Calling Factory::getComparatorFor(...)->assertEquals($expectedArray, $actualArray) (or PHPUnit assertEquals with two arrays) when: a key exists in $expected but not $actual, a key exists in $actual but not $expected, or a nested value fails its own comparator check. Nested failures indent the child ComparisonFailure output.","commonSituations":"PHPUnit assertion failures in tests comparing fixtures vs. actual data; API response arrays missing/containing extra keys; ordering differences in lists when canonicalize=false; float precision or case differences inside nested arrays; config arrays drifting from expected defaults.","solutions":["Read the exported 'Array (...)' strings in the exception (expected vs actual) to find the first differing or missing key.","Fix the actual data or the expected fixture so key sets and values match.","For order-insensitive list comparison, pass $canonicalize=true so list elements are sorted before comparison.","For float/datetime elements, pass an appropriate $delta or normalize values (rounding, casing) before comparing.","Use assertArraySubset-style or targeted key assertions to narrow which key mismatches when arrays are large."],"exampleFix":"// before\n$factory->getComparatorFor($expected, $actual)->assertEquals($expected, $actual); // throws on order/extra keys\n// after\n$factory->getComparatorFor($expected, $actual)->assertEquals($expected, $actual, 0.0, true); // canonicalize: sort lists & keys","handlingStrategy":"try-catch","validationCode":"// before comparing\nif (!is_array($expected) || !is_array($actual)) return;\n$missing = array_diff_key($expected, $actual);\n$extra = array_diff_key($actual, $expected);\nif ($missing || $extra) { /* reconcile key sets first */ }","typeGuard":null,"tryCatchPattern":"try {\n    $factory->getComparatorFor($expected, $actual)->assertEquals($expected, $actual, 0.0, true);\n} catch (ComparisonFailure $e) {\n    // inspect $e->getExpectedAsString() / $e->getActualAsString() for the diff\n}","preventionTips":["Build expected fixtures from the same normalization code path as the actual data.","Use canonicalize=true for order-insensitive list comparison.","Round/normalize floats and casing before comparison.","Keep fixtures small and key-targeted so diffs are readable."],"tags":["phpunit","arrays","assertion","comparison"],"backgroundTag":"schema-validation-failed","analyzedSha":"00837a9d227b4e123b0b42a91736960f6b0b3583","analyzedAt":"2026-09-15T03:36:55.154Z","contentChangedAt":"2026-09-15T03:36:55.154Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}