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\Factory
15 *
16 */
17class FactoryTest extends \PHPUnit_Framework_TestCase
18{
19    public function instanceProvider()
20    {
21        $tmpfile = tmpfile();
22
23        return array(
24            array(NULL, NULL, 'SebastianBergmann\\Comparator\\ScalarComparator'),
25            array(NULL, TRUE, 'SebastianBergmann\\Comparator\\ScalarComparator'),
26            array(TRUE, NULL, 'SebastianBergmann\\Comparator\\ScalarComparator'),
27            array(TRUE, TRUE, 'SebastianBergmann\\Comparator\\ScalarComparator'),
28            array(FALSE, FALSE, 'SebastianBergmann\\Comparator\\ScalarComparator'),
29            array(TRUE, FALSE, 'SebastianBergmann\\Comparator\\ScalarComparator'),
30            array(FALSE, TRUE, 'SebastianBergmann\\Comparator\\ScalarComparator'),
31            array('', '', 'SebastianBergmann\\Comparator\\ScalarComparator'),
32            array('0', '0', 'SebastianBergmann\\Comparator\\ScalarComparator'),
33            array('0', 0, 'SebastianBergmann\\Comparator\\NumericComparator'),
34            array(0, '0', 'SebastianBergmann\\Comparator\\NumericComparator'),
35            array(0, 0, 'SebastianBergmann\\Comparator\\NumericComparator'),
36            array(1.0, 0, 'SebastianBergmann\\Comparator\\DoubleComparator'),
37            array(0, 1.0, 'SebastianBergmann\\Comparator\\DoubleComparator'),
38            array(1.0, 1.0, 'SebastianBergmann\\Comparator\\DoubleComparator'),
39            array(array(1), array(1), 'SebastianBergmann\\Comparator\\ArrayComparator'),
40            array($tmpfile, $tmpfile, 'SebastianBergmann\\Comparator\\ResourceComparator'),
41            array(new \stdClass, new \stdClass, 'SebastianBergmann\\Comparator\\ObjectComparator'),
42            array(new \DateTime, new \DateTime, 'SebastianBergmann\\Comparator\\DateTimeComparator'),
43            array(new \SplObjectStorage, new \SplObjectStorage, 'SebastianBergmann\\Comparator\\SplObjectStorageComparator'),
44            array(new \Exception, new \Exception, 'SebastianBergmann\\Comparator\\ExceptionComparator'),
45            array(new \DOMDocument, new \DOMDocument, 'SebastianBergmann\\Comparator\\DOMNodeComparator'),
46            // mixed types
47            array($tmpfile, array(1), 'SebastianBergmann\\Comparator\\TypeComparator'),
48            array(array(1), $tmpfile, 'SebastianBergmann\\Comparator\\TypeComparator'),
49            array($tmpfile, '1', 'SebastianBergmann\\Comparator\\TypeComparator'),
50            array('1', $tmpfile, 'SebastianBergmann\\Comparator\\TypeComparator'),
51            array($tmpfile, new \stdClass, 'SebastianBergmann\\Comparator\\TypeComparator'),
52            array(new \stdClass, $tmpfile, 'SebastianBergmann\\Comparator\\TypeComparator'),
53            array(new \stdClass, array(1), 'SebastianBergmann\\Comparator\\TypeComparator'),
54            array(array(1), new \stdClass, 'SebastianBergmann\\Comparator\\TypeComparator'),
55            array(new \stdClass, '1', 'SebastianBergmann\\Comparator\\TypeComparator'),
56            array('1', new \stdClass, 'SebastianBergmann\\Comparator\\TypeComparator'),
57            array(new ClassWithToString, '1', 'SebastianBergmann\\Comparator\\ScalarComparator'),
58            array('1', new ClassWithToString, 'SebastianBergmann\\Comparator\\ScalarComparator'),
59            array(1.0, new \stdClass, 'SebastianBergmann\\Comparator\\TypeComparator'),
60            array(new \stdClass, 1.0, 'SebastianBergmann\\Comparator\\TypeComparator'),
61            array(1.0, array(1), 'SebastianBergmann\\Comparator\\TypeComparator'),
62            array(array(1), 1.0, 'SebastianBergmann\\Comparator\\TypeComparator'),
63        );
64    }
65
66    /**
67     * @dataProvider instanceProvider
68     * @covers       ::getComparatorFor
69     * @covers       ::__construct
70     */
71    public function testGetComparatorFor($a, $b, $expected)
72    {
73        $factory = new Factory;
74        $actual = $factory->getComparatorFor($a, $b);
75        $this->assertInstanceOf($expected, $actual);
76    }
77
78    /**
79     * @covers ::register
80     */
81    public function testRegister()
82    {
83        $comparator = new TestClassComparator;
84
85        $factory = new Factory;
86        $factory->register($comparator);
87
88        $a = new TestClass;
89        $b = new TestClass;
90        $expected = 'SebastianBergmann\\Comparator\\TestClassComparator';
91        $actual = $factory->getComparatorFor($a, $b);
92
93        $factory->unregister($comparator);
94        $this->assertInstanceOf($expected, $actual);
95    }
96
97    /**
98     * @covers ::unregister
99     */
100    public function testUnregister()
101    {
102        $comparator = new TestClassComparator;
103
104        $factory = new Factory;
105        $factory->register($comparator);
106        $factory->unregister($comparator);
107
108        $a = new TestClass;
109        $b = new TestClass;
110        $expected = 'SebastianBergmann\\Comparator\\ObjectComparator';
111        $actual = $factory->getComparatorFor($a, $b);
112
113        $this->assertInstanceOf($expected, $actual);
114    }
115}
116