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 * @coversDefaultClass SebastianBergmann\Comparator\ArrayComparator
15 *
16 */
17class ArrayComparatorTest extends \PHPUnit_Framework_TestCase
18{
19    private $comparator;
20
21    protected function setUp()
22    {
23        $this->comparator = new ArrayComparator;
24        $this->comparator->setFactory(new Factory);
25    }
26
27    public function acceptsFailsProvider()
28    {
29        return array(
30          array(array(), null),
31          array(null, array()),
32          array(null, null)
33        );
34    }
35
36    public function assertEqualsSucceedsProvider()
37    {
38        return array(
39          array(
40            array('a' => 1, 'b' => 2),
41            array('b' => 2, 'a' => 1)
42          ),
43          array(
44            array(1),
45            array('1')
46          ),
47          array(
48            array(3, 2, 1),
49            array(2, 3, 1),
50            0,
51            true
52          ),
53          array(
54            array(2.3),
55            array(2.5),
56            0.5
57          ),
58          array(
59            array(array(2.3)),
60            array(array(2.5)),
61            0.5
62          ),
63          array(
64            array(new Struct(2.3)),
65            array(new Struct(2.5)),
66            0.5
67          ),
68        );
69    }
70
71    public function assertEqualsFailsProvider()
72    {
73        return array(
74          array(
75            array(),
76            array(0 => 1)
77          ),
78          array(
79            array(0 => 1),
80            array()
81          ),
82          array(
83            array(0 => null),
84            array()
85          ),
86          array(
87            array(0 => 1, 1 => 2),
88            array(0 => 1, 1 => 3)
89          ),
90          array(
91            array('a', 'b' => array(1, 2)),
92            array('a', 'b' => array(2, 1))
93          ),
94          array(
95            array(2.3),
96            array(4.2),
97            0.5
98          ),
99          array(
100            array(array(2.3)),
101            array(array(4.2)),
102            0.5
103          ),
104          array(
105            array(new Struct(2.3)),
106            array(new Struct(4.2)),
107            0.5
108          )
109        );
110    }
111
112    /**
113     * @covers  ::accepts
114     */
115    public function testAcceptsSucceeds()
116    {
117        $this->assertTrue(
118          $this->comparator->accepts(array(), array())
119        );
120    }
121
122    /**
123     * @covers       ::accepts
124     * @dataProvider acceptsFailsProvider
125     */
126    public function testAcceptsFails($expected, $actual)
127    {
128        $this->assertFalse(
129          $this->comparator->accepts($expected, $actual)
130        );
131    }
132
133    /**
134     * @covers       ::assertEquals
135     * @dataProvider assertEqualsSucceedsProvider
136     */
137    public function testAssertEqualsSucceeds($expected, $actual, $delta = 0.0, $canonicalize = false)
138    {
139        $exception = null;
140
141        try {
142            $this->comparator->assertEquals($expected, $actual, $delta, $canonicalize);
143        }
144
145        catch (ComparisonFailure $exception) {
146        }
147
148        $this->assertNull($exception, 'Unexpected ComparisonFailure');
149    }
150
151    /**
152     * @covers       ::assertEquals
153     * @dataProvider assertEqualsFailsProvider
154     */
155    public function testAssertEqualsFails($expected, $actual,$delta = 0.0, $canonicalize = false)
156    {
157        $this->setExpectedException(
158          'SebastianBergmann\\Comparator\\ComparisonFailure',
159          'Failed asserting that two arrays are equal'
160        );
161        $this->comparator->assertEquals($expected, $actual, $delta, $canonicalize);
162    }
163}
164