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
13use stdClass;
14
15/**
16 * @coversDefaultClass SebastianBergmann\Comparator\ObjectComparator
17 *
18 */
19class ObjectComparatorTest extends \PHPUnit_Framework_TestCase
20{
21    private $comparator;
22
23    protected function setUp()
24    {
25        $this->comparator = new ObjectComparator;
26        $this->comparator->setFactory(new Factory);
27    }
28
29    public function acceptsSucceedsProvider()
30    {
31        return array(
32          array(new TestClass, new TestClass),
33          array(new stdClass, new stdClass),
34          array(new stdClass, new TestClass)
35        );
36    }
37
38    public function acceptsFailsProvider()
39    {
40        return array(
41          array(new stdClass, null),
42          array(null, new stdClass),
43          array(null, null)
44        );
45    }
46
47    public function assertEqualsSucceedsProvider()
48    {
49        // cyclic dependencies
50        $book1 = new Book;
51        $book1->author = new Author('Terry Pratchett');
52        $book1->author->books[] = $book1;
53        $book2 = new Book;
54        $book2->author = new Author('Terry Pratchett');
55        $book2->author->books[] = $book2;
56
57        $object1 = new SampleClass(4, 8, 15);
58        $object2 = new SampleClass(4, 8, 15);
59
60        return array(
61          array($object1, $object1),
62          array($object1, $object2),
63          array($book1, $book1),
64          array($book1, $book2),
65          array(new Struct(2.3), new Struct(2.5), 0.5)
66        );
67    }
68
69    public function assertEqualsFailsProvider()
70    {
71        $typeMessage = 'is not instance of expected class';
72        $equalMessage = 'Failed asserting that two objects are equal.';
73
74        // cyclic dependencies
75        $book1 = new Book;
76        $book1->author = new Author('Terry Pratchett');
77        $book1->author->books[] = $book1;
78        $book2 = new Book;
79        $book2->author = new Author('Terry Pratch');
80        $book2->author->books[] = $book2;
81
82        $book3 = new Book;
83        $book3->author = 'Terry Pratchett';
84        $book4 = new stdClass;
85        $book4->author = 'Terry Pratchett';
86
87        $object1 = new SampleClass( 4,  8, 15);
88        $object2 = new SampleClass(16, 23, 42);
89
90        return array(
91          array(new SampleClass(4, 8, 15), new SampleClass(16, 23, 42), $equalMessage),
92          array($object1, $object2, $equalMessage),
93          array($book1, $book2, $equalMessage),
94          array($book3, $book4, $typeMessage),
95          array(new Struct(2.3), new Struct(4.2), $equalMessage, 0.5)
96        );
97    }
98
99    /**
100     * @covers       ::accepts
101     * @dataProvider acceptsSucceedsProvider
102     */
103    public function testAcceptsSucceeds($expected, $actual)
104    {
105        $this->assertTrue(
106          $this->comparator->accepts($expected, $actual)
107        );
108    }
109
110    /**
111     * @covers       ::accepts
112     * @dataProvider acceptsFailsProvider
113     */
114    public function testAcceptsFails($expected, $actual)
115    {
116        $this->assertFalse(
117          $this->comparator->accepts($expected, $actual)
118        );
119    }
120
121    /**
122     * @covers       ::assertEquals
123     * @dataProvider assertEqualsSucceedsProvider
124     */
125    public function testAssertEqualsSucceeds($expected, $actual, $delta = 0.0)
126    {
127        $exception = null;
128
129        try {
130            $this->comparator->assertEquals($expected, $actual, $delta);
131        }
132
133        catch (ComparisonFailure $exception) {
134        }
135
136        $this->assertNull($exception, 'Unexpected ComparisonFailure');
137    }
138
139    /**
140     * @covers       ::assertEquals
141     * @dataProvider assertEqualsFailsProvider
142     */
143    public function testAssertEqualsFails($expected, $actual, $message, $delta = 0.0)
144    {
145        $this->setExpectedException(
146          'SebastianBergmann\\Comparator\\ComparisonFailure', $message
147        );
148        $this->comparator->assertEquals($expected, $actual, $delta);
149    }
150}
151