1<?php
2/*
3 * This file is part of the Comparator package.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11namespace SebastianBergmann\Comparator;
12
13/**
14 * Compares \SplObjectStorage instances for equality.
15 */
16class SplObjectStorageComparator extends Comparator
17{
18    /**
19     * Returns whether the comparator can compare two values.
20     *
21     * @param  mixed $expected The first value to compare
22     * @param  mixed $actual   The second value to compare
23     * @return bool
24     */
25    public function accepts($expected, $actual)
26    {
27        return $expected instanceof \SplObjectStorage && $actual instanceof \SplObjectStorage;
28    }
29
30    /**
31     * Asserts that two values are equal.
32     *
33     * @param mixed $expected     First value to compare
34     * @param mixed $actual       Second value to compare
35     * @param float $delta        Allowed numerical distance between two values to consider them equal
36     * @param bool  $canonicalize Arrays are sorted before comparison when set to true
37     * @param bool  $ignoreCase   Case is ignored when set to true
38     *
39     * @throws ComparisonFailure
40     */
41    public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)
42    {
43        foreach ($actual as $object) {
44            if (!$expected->contains($object)) {
45                throw new ComparisonFailure(
46                    $expected,
47                    $actual,
48                    $this->exporter->export($expected),
49                    $this->exporter->export($actual),
50                    false,
51                    'Failed asserting that two objects are equal.'
52                );
53            }
54        }
55
56        foreach ($expected as $object) {
57            if (!$actual->contains($object)) {
58                throw new ComparisonFailure(
59                    $expected,
60                    $actual,
61                    $this->exporter->export($expected),
62                    $this->exporter->export($actual),
63                    false,
64                    'Failed asserting that two objects are equal.'
65                );
66            }
67        }
68    }
69}
70