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 \Exception;
14use \RuntimeException;
15
16/**
17 * @coversDefaultClass SebastianBergmann\Comparator\ExceptionComparator
18 *
19 */
20class ExceptionComparatorTest extends \PHPUnit_Framework_TestCase
21{
22    private $comparator;
23
24    protected function setUp()
25    {
26        $this->comparator = new ExceptionComparator;
27        $this->comparator->setFactory(new Factory);
28    }
29
30    public function acceptsSucceedsProvider()
31    {
32        return array(
33          array(new Exception, new Exception),
34          array(new RuntimeException, new RuntimeException),
35          array(new Exception, new RuntimeException)
36        );
37    }
38
39    public function acceptsFailsProvider()
40    {
41        return array(
42          array(new Exception, null),
43          array(null, new Exception),
44          array(null, null)
45        );
46    }
47
48    public function assertEqualsSucceedsProvider()
49    {
50        $exception1 = new Exception;
51        $exception2 = new Exception;
52
53        $exception3 = new RunTimeException('Error', 100);
54        $exception4 = new RunTimeException('Error', 100);
55
56        return array(
57          array($exception1, $exception1),
58          array($exception1, $exception2),
59          array($exception3, $exception3),
60          array($exception3, $exception4)
61        );
62    }
63
64    public function assertEqualsFailsProvider()
65    {
66        $typeMessage = 'not instance of expected class';
67        $equalMessage = 'Failed asserting that two objects are equal.';
68
69        $exception1 = new Exception('Error', 100);
70        $exception2 = new Exception('Error', 101);
71        $exception3 = new Exception('Errors', 101);
72
73        $exception4 = new RunTimeException('Error', 100);
74        $exception5 = new RunTimeException('Error', 101);
75
76        return array(
77          array($exception1, $exception2, $equalMessage),
78          array($exception1, $exception3, $equalMessage),
79          array($exception1, $exception4, $typeMessage),
80          array($exception2, $exception3, $equalMessage),
81          array($exception4, $exception5, $equalMessage)
82        );
83    }
84
85    /**
86     * @covers       ::accepts
87     * @dataProvider acceptsSucceedsProvider
88     */
89    public function testAcceptsSucceeds($expected, $actual)
90    {
91        $this->assertTrue(
92          $this->comparator->accepts($expected, $actual)
93        );
94    }
95
96    /**
97     * @covers       ::accepts
98     * @dataProvider acceptsFailsProvider
99     */
100    public function testAcceptsFails($expected, $actual)
101    {
102        $this->assertFalse(
103          $this->comparator->accepts($expected, $actual)
104        );
105    }
106
107    /**
108     * @covers       ::assertEquals
109     * @dataProvider assertEqualsSucceedsProvider
110     */
111    public function testAssertEqualsSucceeds($expected, $actual)
112    {
113        $exception = null;
114
115        try {
116            $this->comparator->assertEquals($expected, $actual);
117        }
118
119        catch (ComparisonFailure $exception) {
120        }
121
122        $this->assertNull($exception, 'Unexpected ComparisonFailure');
123    }
124
125    /**
126     * @covers       ::assertEquals
127     * @dataProvider assertEqualsFailsProvider
128     */
129    public function testAssertEqualsFails($expected, $actual, $message)
130    {
131        $this->setExpectedException(
132          'SebastianBergmann\\Comparator\\ComparisonFailure', $message
133        );
134        $this->comparator->assertEquals($expected, $actual);
135    }
136}
137