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