Skip to content

Commit

Permalink
Illegal paths check, more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dotfry committed Sep 20, 2018
1 parent c040bc2 commit 2b5933a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
18 changes: 12 additions & 6 deletions sixdreams/StreamReader/XmlStreamReader.zep
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class XmlStreamReader implements StreamReaderInterface
protected collecting;

protected collected;

protected collectedRef;

public function parse(resource data, int buffer = 1024) -> bool
Expand Down Expand Up @@ -61,11 +60,18 @@ class XmlStreamReader implements StreamReaderInterface
return true;
}

public function registerCallback(string! collectPath, string extractPath, callable callback) -> <StreamReaderInterface>
public function registerCallback(string collectPath, string! extractPath, callable callback) -> <StreamReaderInterface>
{
let this->extractPath = strtolower(extractPath);
let this->collectPath = strtolower(collectPath);
let this->callback = callback;
if (collectPath === "") {
let this->collectPath = this->extractPath;
} else {
let this->collectPath = strtolower(collectPath);
}
let this->callback = callback;
if strpos(this->extractPath, this->collectPath) !== 0 {
throw new \Exception("Path must extractPath must contain collectPath!");
}

return this;
}
Expand All @@ -82,7 +88,7 @@ class XmlStreamReader implements StreamReaderInterface
let this->currentPath = this->currentPath . "/" . strtolower(name);
this->checkPath();

if this->collecting {
if this->collecting || this->extracting {
if this->extracting && !this->isExtract() {
this->addData(this->buildElement([name, attributes, ""]));
return;
Expand Down Expand Up @@ -114,8 +120,8 @@ class XmlStreamReader implements StreamReaderInterface
if this->extracting && !extract {
this->addData(this->closeElement(name));
} else {
unset(this->collected[this->collectedRef - 1]);
let this->collectedRef = this->collectedRef - 1;
unset(this->collected[this->collectedRef]);
}
}

Expand Down
35 changes: 28 additions & 7 deletions tests/XmlStreamReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public function testBase(?string $collect, string $xtract, array $excepted): voi
}
}

/**
* Tests invalid path.
*/
public function testInvalidPath(): void
{
$this->expectExceptionMessage('Path must extractPath must contain collectPath!');
$this->requestData('/a/b/c', '/h/c/d/e');
}

/**
* Data provider for test.
*
Expand All @@ -39,6 +48,16 @@ public function dataProvider(): array
'<sport name="football" id="2"><league name="FBL02"><game team1="g1t1" team2="g1t2"><![CDATA[raw]]></game></league></sport>',
'<sport name="football" id="2"><LEAGUE name="supsuckers"><game team2="test_team_name"><var><![CDATA[test]]></var></game></LEAGUE></sport>',
'<sport name="snooker" id="1"><league name="SNK01"><game team1="t1" team2="t34"></game></league></sport>'
]],
[null, '/xml/sport/league/game', [
'<game team1="g1t1" team2="g1t2"><![CDATA[raw]]></game>',
'<game team2="test_team_name"><var><![CDATA[test]]></var></game>',
'<game team1="t1" team2="t34"></game>'
]],
['/xml/sport/league/game', '/xml/sport/league/game', [
'<game team1="g1t1" team2="g1t2"><![CDATA[raw]]></game>',
'<game team2="test_team_name"><var><![CDATA[test]]></var></game>',
'<game team1="t1" team2="t34"></game>'
]]
];
}
Expand All @@ -56,13 +75,15 @@ protected function requestData(?string $collect, string $extract): array
$handle = \fopen(__DIR__ . '/sample.xml', 'rb');
$collected = [];

(new StreamReader\XmlStreamReader())
->registerCallback($collect, $extract, function (string $data) use (&$collected) {
$collected[] = $data;
})
->parse($handle);

\fclose($handle);
try {
(new StreamReader\XmlStreamReader())
->registerCallback($collect, $extract, function (string $data) use (&$collected) {
$collected[] = $data;
})
->parse($handle);
} finally {
\fclose($handle);
}

return $collected;
}
Expand Down

0 comments on commit 2b5933a

Please sign in to comment.