Skip to content

Commit b3f4824

Browse files
committed
re-plumb
1 parent d269759 commit b3f4824

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

tests/autoload.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+

tests/class-test-base.php

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
spl_autoload_register(function ($name) {
8+
if ($name == "xml_file_test_utils") require_once(__DIR__ . "/class-xml_file_test_utils.php");
9+
});
10+
11+
class test_base extends TestCase
12+
{
13+
public $files;
14+
15+
public function tearDown(): void
16+
{
17+
if (is_array($this->files))
18+
foreach ($this->files as $tmp) @unlink($tmp);
19+
}
20+
21+
public function tmpFile()
22+
{
23+
if ($this->files == null) $files = array();
24+
if (file_exists("C:\WINDOWS\Temp") && is_dir("C:\WINDOWS\Temp"))
25+
$this->files[] = $tmp = tempnam('C:\WINDOWS\Temp', 'test_');
26+
else
27+
$this->files[] = $tmp = tempnam('/tmp', 'test_');
28+
unlink($tmp);
29+
return $tmp;
30+
}
31+
32+
function createTestXML()
33+
{
34+
$tmp = $this->tmpFile();
35+
$s = "";
36+
$s .= "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n";
37+
$s .= "<items>" . "\n";
38+
$s .= " <item id='1'>" . "\n";
39+
$s .= " <name>Name #1</name>" . "\n";
40+
$s .= " <size>Small</size>" . "\n";
41+
$s .= " </item>" . "\n";
42+
$s .= " <item id='2'>" . "\n";
43+
$s .= " <name>Name #2</name>" . "\n";
44+
$s .= " <size>Medium</size>" . "\n";
45+
$s .= " </item>" . "\n";
46+
$s .= " <item id='3'>" . "\n";
47+
$s .= " <name>Name #3</name>" . "\n";
48+
$s .= " <size>Large</size>" . "\n";
49+
$s .= " </item>" . "\n";
50+
$s .= "</items>";
51+
file_put_contents($tmp, $s);
52+
return $tmp;
53+
}
54+
55+
function createTestXSL()
56+
{
57+
$n = "\n";
58+
59+
$s = "";
60+
// $s .= $n . '<?xml version="1.0" encoding="UTF-8" ?' . '>';
61+
$s .= $n . '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">';
62+
$s .= $n;
63+
$s .= $n . ' <xsl:template match="/">';
64+
$s .= $n . ' <xsl:for-each select="items/item/name"><xsl:value-of select="." />,</xsl:for-each>';
65+
$s .= $n . ' </xsl:template>';
66+
$s .= $n . '</xsl:stylesheet>';
67+
68+
$tmp = $this->tmpFile();
69+
file_put_contents($tmp, $s);
70+
return $tmp;
71+
}
72+
73+
function createInvalidXML()
74+
{
75+
$tmp = $this->tmpFile();
76+
$s = "";
77+
$s .= "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n";
78+
$s .= "<items>" . "\n";
79+
$s .= " <item id='1'>" . "\n";
80+
$s .= "</items>";
81+
file_put_contents($tmp, $s);
82+
return $tmp;
83+
}
84+
85+
public function testCreateXMLFile(): void
86+
{
87+
$subject = new xml_file();
88+
$this->assertNotNull($subject);
89+
}
90+
91+
public function testTempFile(): void
92+
{
93+
$testText = "12345";
94+
$fname = $this->tmpFile();
95+
file_put_contents($fname, $testText);
96+
$result = file_get_contents($fname);
97+
$this->assertEquals($testText, $result);
98+
}
99+
100+
public function testLoadAFile(): void
101+
{
102+
$subject = new xml_file($fname = $this->createTestXML());
103+
$this->assertNotEquals("", $subject->gid);
104+
$this->assertEquals($fname, $subject->filename);
105+
$this->assertTrue($subject->loaded);
106+
$this->assertEquals("", $subject->err);
107+
}
108+
109+
public function testClearObject(): void
110+
{
111+
$subject = new xml_file($fname = $this->createTestXML(), "readonly,notidy");
112+
$this->assertTrue($subject->loaded);
113+
$this->assertTrue($subject->readonly);
114+
$this->assertTrue($subject->notidy);
115+
$gid = $subject->gid;
116+
117+
$subject->clear();
118+
$this->assertFalse($subject->loaded);
119+
$this->assertEquals("", $subject->filename);
120+
$this->assertEquals($gid, $subject->gid);
121+
$this->assertFalse($subject->readonly);
122+
$this->assertFalse($subject->notidy);
123+
}
124+
125+
public function testLoadNonExistentFile(): void
126+
{
127+
$subject = new xml_file();
128+
$result = $subject->load($this->tmpFile());
129+
$this->assertFalse($subject->loaded);
130+
$this->assertFalse($result);
131+
}
132+
133+
public function testInvalidXML(): void
134+
{
135+
$subject = new xml_file();
136+
$result = $subject->load($this->createInvalidXML());
137+
138+
$this->assertFalse($result);
139+
$this->assertNotNull($subject->err);
140+
}
141+
}

0 commit comments

Comments
 (0)