Skip to content

Commit 4491d48

Browse files
committed
Added unit tests
- Added PHPUnit dependency - Created phpunit.xml configuration - Created UXML test
1 parent 987665a commit 4491d48

File tree

3 files changed

+130
-1
lines changed

3 files changed

+130
-1
lines changed

composer.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
"UXML\\": "src/"
1717
}
1818
},
19+
"autoload-dev": {
20+
"psr-4": {
21+
"Tests\\": "tests/"
22+
}
23+
},
1924
"require": {
2025
"php": ">=7.1",
2126
"lib-libxml": "*"
2227
},
2328
"require-dev": {
24-
"phan/phan": "^3.0"
29+
"phpunit/phpunit": "^9",
30+
"phan/phan": "^4"
2531
}
2632
}

phpunit.xml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0"?>
2+
<phpunit cacheResult="false" colors="true" bootstrap="vendor/autoload.php" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage processUncoveredFiles="true">
4+
<include>
5+
<directory suffix=".php">src</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="Tests">
10+
<directory>tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

tests/UXMLTest.php

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
namespace Tests;
3+
4+
use DOMElement;
5+
use UXML\UXML;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class UXMLTest extends TestCase {
9+
/**
10+
* Element list to text
11+
* @param UXML[] $elements Array of elements
12+
* @return string Text content of elements separated by commas
13+
*/
14+
private function listToText(array $elements): string {
15+
return implode(',', array_map(function(UXML $elem) {
16+
return $elem->asText();
17+
}, $elements));
18+
}
19+
20+
public function testCanCreateElements(): void {
21+
$xml = UXML::newInstance('RootTagName');
22+
$this->assertEquals('<RootTagName/>', $xml);
23+
24+
$xml = new UXML(new DOMElement('TagName'));
25+
$this->assertEquals('<TagName/>', $xml);
26+
}
27+
28+
public function testCanLoadXml(): void {
29+
$source = "<fruits>";
30+
$source .= "<fruit>Banana</fruit>";
31+
$source .= "<fruit>Apple</fruit>";
32+
$source .= "<fruit>Orange</fruit>";
33+
$source .= "<optional>";
34+
$source .= "<fruit>Tomato</fruit>";
35+
$source .= "</optional>";
36+
$source .= "</fruits>";
37+
$xml = UXML::load($source);
38+
$this->assertEquals($source, $xml);
39+
}
40+
41+
public function testCanAddElements(): void {
42+
$xml = UXML::newInstance('Parent');
43+
$childA = $xml->add('ChildA', 'Name');
44+
$childB = $xml->add('Wrapper')->add('ChildB');
45+
$this->assertEquals('<ChildA>Name</ChildA>', $childA);
46+
$this->assertEquals('<ChildB/>', $childB);
47+
$this->assertEquals('<Parent><ChildA>Name</ChildA><Wrapper><ChildB/></Wrapper></Parent>', $xml);
48+
}
49+
50+
public function testCanHandleAttributes(): void {
51+
$feed = UXML::newInstance('feed', null, [
52+
'xmlns' => 'urn:atom',
53+
'xmlns:a' => 'urn:testns'
54+
]);
55+
$feed->add('link', 'Wow!', ['a:href' => 'urn']);
56+
$this->assertEquals('<feed xmlns="urn:atom" xmlns:a="urn:testns"><link a:href="urn">Wow!</link></feed>', $feed);
57+
}
58+
59+
public function testCanGetSingleElement(): void {
60+
$source = <<<XML
61+
<movie>
62+
<name lang="en-US">Inception</name>
63+
<year>2010</year>
64+
<director>
65+
<name>Christopher</name>
66+
<surname>Nolan</surname>
67+
<year>1970</year>
68+
</director>
69+
</movie>
70+
XML;
71+
$xml = UXML::load($source);
72+
73+
$this->assertEquals('<year>1970</year>', $xml->get('director/year'));
74+
$this->assertEquals('<year>1970</year>', $xml->get('director')->get('year'));
75+
$this->assertEquals('<year>2010</year>', $xml->get('year'));
76+
$this->assertEquals('<year>2010</year>', $xml->get('director')->get('//year'));
77+
$this->assertEquals('<name lang="en-US">Inception</name>', $xml->get('*[@lang]'));
78+
$this->assertNull($xml->get('genre'));
79+
}
80+
81+
public function testCanGetAllElements(): void {
82+
$source = <<<XML
83+
<root>
84+
<a>
85+
<b>1</b>
86+
<b>2</b>
87+
<c>-1</c>
88+
<b>3</b>
89+
<c>-2</c>
90+
<d>Inf</d>
91+
</a>
92+
<b>4</b>
93+
<c>-3</c>
94+
</root>
95+
XML;
96+
$xml = UXML::load($source);
97+
98+
$this->assertEquals('1,2,3', $this->listToText($xml->getAll('a/b')));
99+
$this->assertEquals('1,2', $this->listToText($xml->getAll('a/b', 2)));
100+
$this->assertEquals('-3', $this->listToText($xml->getAll('c')));
101+
$this->assertEquals('-1,-2,-3', $this->listToText($xml->getAll('//c')));
102+
$this->assertEmpty($xml->getAll('d'));
103+
}
104+
105+
public function testCanHandleClarkNotation(): void {
106+
$xml = UXML::load('<a xmlns:ns="urn:abc"><ns:b /><ns:c /></a>');
107+
$this->assertEquals('<ns:b xmlns:ns="urn:abc"/>', $xml->get('{urn:abc}b'));
108+
$this->assertSame($xml->get('{urn:abc}b'), $xml->get('ns:b'));
109+
}
110+
}

0 commit comments

Comments
 (0)