DomTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 102
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testXmlString() 0 6 1
A testXmlFile() 0 11 1
A testXmlUri() 0 6 1
A testHtml5String() 0 8 1
A testHtml4StringNamespaces() 0 15 1
A testHtmlSvgFile() 0 12 1
A testHtmlMathMLFile() 0 14 1
1
<?php
2
3
/**
4
 * dom-factory
5
 *
6
 * @category   Jkphl
7
 * @package    Jkphl\Domfactory
8
 * @subpackage Jkphl\Domfactory\Tests
9
 * @author     Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright  Copyright © 2020 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license    http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2020 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Jkphl\Domfactory\Tests\Ports;
38
39
use DOMDocument;
40
use DOMXPath;
41
use Jkphl\Domfactory\Ports\Dom;
42
use Jkphl\Domfactory\Ports\InvalidArgumentException;
43
use Jkphl\Domfactory\Tests\AbstractTestBase;
44
45
/**
46
 * DOM factory tests
47
 *
48
 * @package    Jkphl\Domfactory
49
 * @subpackage Jkphl\Domfactory\Tests
50
 */
51
class DomTest extends AbstractTestBase
52
{
53
    /**
54
     * Test parsing an XML string
55
     */
56
    public function testXmlString()
57
    {
58
        $dom = Dom::createFromString(file_get_contents(self::$fixture.'books.xml'));
59
        $this->assertInstanceOf(DOMDocument::class, $dom);
60
        $this->assertEquals('catalog', $dom->documentElement->localName);
61
    }
62
63
    /**
64
     * Test parsing an XML file
65
     */
66
    public function testXmlFile()
67
    {
68
        $this->expectException(InvalidArgumentException::class);
69
        $this->expectExceptionCode(1495569580);
70
71
        $dom = Dom::createFromFile(self::$fixture.'books.xml');
72
        $this->assertInstanceOf(DOMDocument::class, $dom);
73
        $this->assertEquals('catalog', $dom->documentElement->localName);
74
75
        Dom::createFromFile('invalid');
76
    }
77
78
    /**
79
     * Test parsing an XML string via HTTP
80
     */
81
    public function testXmlUri()
82
    {
83
        $dom = Dom::createFromUri('http://localhost:1349/books.xml');
84
        $this->assertInstanceOf(DOMDocument::class, $dom);
85
        $this->assertEquals('catalog', $dom->documentElement->localName);
86
    }
87
88
    /**
89
     * Test parsing an HTML5 string
90
     */
91
    public function testHtml5String()
92
    {
93
        $dom = Dom::createFromString(file_get_contents(self::$fixture.'html5.html'));
94
        $this->assertInstanceOf(DOMDocument::class, $dom);
95
        $this->assertEquals('html', $dom->documentElement->localName);
96
        $this->assertEquals('http://www.w3.org/1999/xhtml', $dom->documentElement->namespaceURI);
97
        $this->assertTrue($dom->documentElement->isDefaultNamespace('http://www.w3.org/1999/xhtml'));
98
    }
99
100
    /**
101
     * Test parsing an HTML4 string with additional namespaces
102
     */
103
    public function testHtml4StringNamespaces()
104
    {
105
        $dom = Dom::createFromString(file_get_contents(self::$fixture.'html4.html'));
106
        $this->assertInstanceOf(DOMDocument::class, $dom);
107
        $this->assertEquals('html', $dom->documentElement->localName);
108
        $this->assertEquals('http://www.w3.org/1999/xhtml', $dom->documentElement->namespaceURI);
109
        $this->assertTrue($dom->documentElement->isDefaultNamespace('http://www.w3.org/1999/xhtml'));
110
111
        $xpath = new DOMXPath($dom);
112
        $xpath->registerNamespace('html', 'http://www.w3.org/1999/xhtml');
113
        $xpath->registerNamespace('test', 'http://example.com/test-ns');
114
        $htmlElement = $xpath->query('/html:html');
115
        $this->assertEquals(1, count($htmlElement));
116
        $this->assertEquals(3, count($xpath->query('namespace::*', $htmlElement->item(0))));
117
    }
118
119
    /**
120
     * Test parsing an HTML file with inline SVG
121
     */
122
    public function testHtmlSvgFile()
123
    {
124
        $dom = Dom::createFromFile(self::$fixture.'html+svg.html');
125
        $this->assertInstanceOf(DOMDocument::class, $dom);
126
        $this->assertEquals('html', $dom->documentElement->localName);
127
128
        $xpath = new DOMXPath($dom);
129
        $xpath->registerNamespace('svg', 'http://www.w3.org/2000/svg');
130
        $this->assertEquals(1, count($xpath->query('/html')));
131
        $this->assertEquals(1, count($xpath->query('//svg')));
132
        $this->assertEquals(1, count($xpath->query('//svg:svg')));
133
    }
134
135
    /**
136
     * Test parsing an HTML file with inline MathML
137
     */
138
    public function testHtmlMathMLFile()
139
    {
140
        $dom = Dom::createFromFile(self::$fixture.'html+mathml.html');
141
        $this->assertInstanceOf(DOMDocument::class, $dom);
142
        $this->assertEquals('html', $dom->documentElement->localName);
143
        $this->assertEquals('http://www.w3.org/1999/xhtml', $dom->documentElement->namespaceURI);
144
        $this->assertTrue($dom->documentElement->isDefaultNamespace('http://www.w3.org/1999/xhtml'));
145
146
        $xpath = new DOMXPath($dom);
147
        $xpath->registerNamespace('html', 'http://www.w3.org/1999/xhtml');
148
        $xpath->registerNamespace('mathml', 'http://www.w3.org/1998/Math/MathML');
149
        $this->assertEquals(1, count($xpath->query('/html:html')));
150
        $this->assertEquals(1, count($xpath->query('//mathml:math')));
151
    }
152
}
153