Skip to content

Commit

Permalink
Refactor repeated preg_replace in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-davis committed Nov 16, 2021
1 parent 196a5be commit 09bf553
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
10 changes: 10 additions & 0 deletions tests/Sabre/DAV/AbstractServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ protected function getRootNode()
return new FS\Directory(SABRE_TEMPDIR);
}

protected function getSanitizedBody()
{
return preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", 'xmlns\\1="urn:DAV"', $this->response->getBodyAsString());
}

protected function getSanitizedBodyAsXml()
{
return simplexml_load_string($this->getSanitizedBody());
}

private function deleteTree($path, $deleteRoot = true)
{
foreach (scandir($path) as $node) {
Expand Down
6 changes: 2 additions & 4 deletions tests/Sabre/DAV/Locks/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ public function testLock()

$this->assertEquals(200, $this->response->status, 'Got an incorrect status back. Response body: '.$this->response->getBodyAsString());

$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", 'xmlns\\1="urn:DAV"', $this->response->getBodyAsString());
$xml = simplexml_load_string($body);
$xml = $this->getSanitizedBodyAsXml();
$xml->registerXPathNamespace('d', 'urn:DAV');

$elements = [
Expand Down Expand Up @@ -128,8 +127,7 @@ public function testLockWithContext()

$this->assertEquals(200, $this->response->status, 'Got an incorrect status back. Response body: '.$this->response->getBodyAsString());

$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", 'xmlns\\1="urn:DAV"', $this->response->getBodyAsString());
$xml = simplexml_load_string($body);
$xml = $this->getSanitizedBodyAsXml();
$xml->registerXPathNamespace('d', 'urn:DAV');

$lockRoot = $xml->xpath('/d:prop/d:lockdiscovery/d:activelock/d:lockroot/d:href');
Expand Down
7 changes: 3 additions & 4 deletions tests/Sabre/DAV/ServerPropsInfiniteDepthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ public function testLockDiscovery()

$this->sendRequest($xml);

$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", 'xmlns\\1="urn:DAV"', $this->response->getBodyAsString());
$xml = simplexml_load_string($body);
$xml = $this->getSanitizedBodyAsXml();
$xml->registerXPathNamespace('d', 'urn:DAV');

$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:lockdiscovery');
Expand All @@ -138,7 +137,7 @@ public function testUnknownProperty()
</d:propfind>';

$this->sendRequest($xml);
$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", 'xmlns\\1="urn:DAV"', $this->response->getBodyAsString());
$body = $this->getSanitizedBody();
$xml = simplexml_load_string($body);
$xml->registerXPathNamespace('d', 'urn:DAV');
$pathTests = [
Expand Down Expand Up @@ -168,7 +167,7 @@ public function testFilesThatAreSiblingsOfDirectoriesShouldBeReportedAsFiles()
</d:propfind>';

$this->sendRequest($xml);
$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", 'xmlns\\1="urn:DAV"', $this->response->getBodyAsString());
$body = $this->getSanitizedBody();
$xml = simplexml_load_string($body);
$xml->registerXPathNamespace('d', 'urn:DAV');
$pathTests = [
Expand Down
17 changes: 6 additions & 11 deletions tests/Sabre/DAV/ServerPropsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public function testPropFindEmptyBody()
$this->response->getHeaders()
);

$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", 'xmlns\\1="urn:DAV"', $this->response->getBodyAsString());
$xml = simplexml_load_string($body);
$xml = $this->getSanitizedBodyAsXml();
$xml->registerXPathNamespace('d', 'urn:DAV');

list($data) = $xml->xpath('/d:multistatus/d:response/d:href');
Expand Down Expand Up @@ -85,8 +84,7 @@ public function testPropFindEmptyBodyDepth1Custom()
$this->response->getHeaders()
);

$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", 'xmlns\\1="urn:DAV"', $this->response->getBodyAsString());
$xml = simplexml_load_string($body);
$xml = $this->getSanitizedBodyAsXml();
$xml->registerXPathNamespace('d', 'urn:DAV');

$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:ishidden');
Expand All @@ -111,8 +109,7 @@ public function testPropFindEmptyBodyFile()
$this->response->getHeaders()
);

$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", 'xmlns\\1="urn:DAV"', $this->response->getBodyAsString());
$xml = simplexml_load_string($body);
$xml = $this->getSanitizedBodyAsXml();
$xml->registerXPathNamespace('d', 'urn:DAV');

list($data) = $xml->xpath('/d:multistatus/d:response/d:href');
Expand All @@ -133,8 +130,7 @@ public function testSupportedLocks()

$this->sendRequest($xml);

$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", 'xmlns\\1="urn:DAV"', $this->response->getBodyAsString());
$xml = simplexml_load_string($body);
$xml = $this->getSanitizedBodyAsXml();
$xml->registerXPathNamespace('d', 'urn:DAV');

$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:supportedlock/d:lockentry');
Expand Down Expand Up @@ -167,8 +163,7 @@ public function testLockDiscovery()

$this->sendRequest($xml);

$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", 'xmlns\\1="urn:DAV"', $this->response->getBodyAsString());
$xml = simplexml_load_string($body);
$xml = $this->getSanitizedBodyAsXml();
$xml->registerXPathNamespace('d', 'urn:DAV');

$data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop/d:lockdiscovery');
Expand All @@ -185,7 +180,7 @@ public function testUnknownProperty()
</d:propfind>';

$this->sendRequest($xml);
$body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/", 'xmlns\\1="urn:DAV"', $this->response->getBodyAsString());
$body = $this->getSanitizedBody();
$xml = simplexml_load_string($body);
$xml->registerXPathNamespace('d', 'urn:DAV');
$pathTests = [
Expand Down

0 comments on commit 09bf553

Please sign in to comment.