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