Skip to content

Commit ef1c3cb

Browse files
committed
Merge pull request #4 from clue-labs/tests
Improve test suite
2 parents 79e2c83 + e4e241c commit ef1c3cb

13 files changed

+721
-19
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
"php": ">=5.3",
1818
"react/event-loop": "~0.4.0|~0.3.0",
1919
"clue/buzz-react": "~0.2.0",
20-
"ext-simplexml": "*",
21-
"ext-tidy": "*"
20+
"ext-simplexml": "*"
2221
},
2322
"require-dev": {
2423
"clue/block-react": "~0.1.0"

src/Client.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,22 @@
99
use Clue\React\Buzz\Browser;
1010
use Clue\React\Buzz\Message\Response;
1111
use React\Promise\Deferred;
12+
use Clue\React\ViewVcApi\Io\Parser;
13+
use Clue\React\ViewVcApi\Io\Loader;
1214

1315
class Client
1416
{
1517
private $url;
1618
private $brower;
1719

18-
public function __construct($url, Browser $browser, Parser $parser = null)
20+
public function __construct($url, Browser $browser, Parser $parser = null, Loader $loader = null)
1921
{
2022
if ($parser === null) {
2123
$parser = new Parser();
2224
}
25+
if ($loader === null) {
26+
$loader = new Loader();
27+
}
2328

2429
// TODO: do not follow redirects
2530
// $browser = $this->browser->withOptions(array(
@@ -29,6 +34,7 @@ public function __construct($url, Browser $browser, Parser $parser = null)
2934
$this->url = $url;
3035
$this->browser = $browser;
3136
$this->parser = $parser;
37+
$this->loader = $loader;
3238
}
3339

3440
public function fetchFile($path, $revision = null)
@@ -105,18 +111,7 @@ private function fetchLogXml($path)
105111

106112
private function fetchXml($url)
107113
{
108-
return $this->fetch($url)->then(function ($html) {
109-
// clean up HTML to safe XML
110-
$html = tidy_repair_string($html, array(
111-
'output-xml' => true,
112-
'input-xml' => true,
113-
));
114-
115-
// clean up namespace declaration
116-
$html = str_replace('xmlns=', 'ns=', $html);
117-
118-
return new SimpleXMLElement($html);
119-
});
114+
return $this->fetch($url)->then(array($this->loader, 'loadXmlString'));
120115
}
121116

122117
private function fetch($url)

src/Io/Loader.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Clue\React\ViewVcApi\Io;
4+
5+
use SimpleXMLElement;
6+
7+
class Loader
8+
{
9+
public function loadXmlFile($path)
10+
{
11+
return $this->loadXmlString(file_get_contents($path));
12+
}
13+
14+
public function loadXmlString($html)
15+
{
16+
// fix invalid markup of help link in footer of outdated ViewVC versions
17+
$html = str_replace('Help</strong></td>', 'Help</a></strong></td>', $html);
18+
19+
// replace unneeded HTML entities
20+
$html = str_replace('&nbsp;', ' ', $html);
21+
22+
// clean up namespace declaration
23+
$html = str_replace('xmlns="', 'ns="', $html);
24+
25+
return new SimpleXMLElement($html);
26+
}
27+
}

src/Parser.php renamed to src/Io/Parser.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
22

3-
namespace Clue\React\ViewVcApi;
3+
namespace Clue\React\ViewVcApi\Io;
4+
5+
use SimpleXMLElement;
46

57
class Parser
68
{
7-
public function parseDirectoryListing($xml)
9+
public function parseDirectoryListing(SimpleXMLElement $xml)
810
{
911
$files = array();
1012

@@ -24,7 +26,7 @@ public function parseDirectoryListing($xml)
2426
return $files;
2527
}
2628

27-
public function parseLogRevisions($xml)
29+
public function parseLogRevisions(SimpleXMLElement $xml)
2830
{
2931
$revisions = array();
3032

tests/FunctionalClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class FunctionalClientTest extends TestCase
1414

1515
public function setUp()
1616
{
17-
$url = 'https://svn.apache.org/viewvc/';
17+
$url = 'http://svn.apache.org/viewvc/';
1818

1919
$this->loop = LoopFactory::create();
2020
$this->blocker = new Blocker($this->loop);

tests/Io/LoaderTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Clue\React\ViewVcApi\Io\Loader;
4+
5+
class LoaderTest extends TestCase
6+
{
7+
private $loader;
8+
9+
public function setUp()
10+
{
11+
$this->loader = new Loader();
12+
}
13+
14+
/**
15+
* @dataProvider xmlFiles
16+
* @param string $path
17+
*/
18+
public function testValidXmlFixtures($path)
19+
{
20+
$xml = $this->loader->loadXmlFile(__DIR__ . '/../fixtures/' . $path);
21+
}
22+
23+
public function xmlFiles()
24+
{
25+
return array_filter(array_map(
26+
function ($path) {
27+
return (substr($path, -5) === '.html') ? array($path) : null;
28+
},
29+
scandir(__DIR__ . '/../fixtures/')
30+
));
31+
}
32+
}

tests/Io/ParserTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use Clue\React\ViewVcApi\Io\Parser;
4+
use Clue\React\ViewVcApi\Io\Loader;
5+
6+
class ParserTest extends TestCase
7+
{
8+
private $loader;
9+
private $parser;
10+
11+
public function setUp()
12+
{
13+
$this->loader = new Loader();
14+
$this->parser = new Parser();
15+
}
16+
17+
public function testLogRevisions()
18+
{
19+
$xml = $this->loadXml('is-a-file.html');
20+
21+
$revisions = $this->parser->parseLogRevisions($xml);
22+
23+
$this->assertEquals(array('168703' => '168695', '168695' => '168694'), $revisions);
24+
}
25+
26+
public function testDirectoryListing()
27+
{
28+
$xml = $this->loadXml('listing.html');
29+
30+
$files = $this->parser->parseDirectoryListing($xml);
31+
32+
$this->assertEquals(array('images/', 'stylesheets/', 'index.xml', 'velocity.properties'), $files);
33+
}
34+
35+
private function loadXml($file)
36+
{
37+
return $this->loader->loadXmlFile(__DIR__ . '/../fixtures/' . $file);
38+
}
39+
}

0 commit comments

Comments
 (0)