diff --git a/lib/Service.php b/lib/Service.php index 9a2c477..596c93c 100644 --- a/lib/Service.php +++ b/lib/Service.php @@ -115,12 +115,13 @@ public function parse($input, string $contextUri = null, string &$rootElementNam // Unfortunately the XMLReader doesn't support streams. When it // does, we can optimize this. $input = (string) stream_get_contents($input); + } - // If input is an empty string, then its safe to throw exception - if ('' === $input) { - throw new ParseException('The input element to parse is empty. Do not attempt to parse'); - } + // If input is empty, then its safe to throw exception + if (empty($input)) { + throw new ParseException('The input element to parse is empty. Do not attempt to parse'); } + $r = $this->getReader(); $r->contextUri = $contextUri; $r->XML($input, null, $this->options); @@ -158,12 +159,13 @@ public function expect($rootElementName, $input, string $contextUri = null) // Unfortunately the XMLReader doesn't support streams. When it // does, we can optimize this. $input = (string) stream_get_contents($input); + } - // If input is empty string, then its safe to throw exception - if ('' === $input) { - throw new ParseException('The input element to parse is empty. Do not attempt to parse'); - } + // If input is empty, then its safe to throw exception + if (empty($input)) { + throw new ParseException('The input element to parse is empty. Do not attempt to parse'); } + $r = $this->getReader(); $r->contextUri = $contextUri; $r->XML($input, null, $this->options); diff --git a/tests/Sabre/Xml/ServiceTest.php b/tests/Sabre/Xml/ServiceTest.php index 3521e80..379952f 100644 --- a/tests/Sabre/Xml/ServiceTest.php +++ b/tests/Sabre/Xml/ServiceTest.php @@ -36,13 +36,18 @@ public function testGetWriter() $this->assertEquals($ns, $writer->namespaceMap); } - public function testEmptyInputParse() + /** + * @dataProvider providesEmptyInput + * + * @param string|resource $input + */ + public function testEmptyInputParse($input) { - $resource = fopen('php://input', 'r'); - $util = new Service(); $this->expectException('\Sabre\Xml\ParseException'); $this->expectExceptionMessage('The input element to parse is empty. Do not attempt to parse'); - $util->parse($resource, '/sabre.io/ns'); + + $util = new Service(); + $util->parse($input, '/sabre.io/ns'); } /** @@ -105,14 +110,18 @@ public function testParseStream() ); } - public function testEmptyInputExpect() + /** + * @dataProvider providesEmptyInput + * + * @param string|resource $input + */ + public function testEmptyInputExpect($input) { - //$resource = \fopen('') - $resource = fopen('php://input', 'r'); - $util = new Service(); $this->expectException('\Sabre\Xml\ParseException'); $this->expectExceptionMessage('The input element to parse is empty. Do not attempt to parse'); - $util->expect('foo', $resource, '/sabre.io/ns'); + + $util = new Service(); + $util->expect('foo', $input, '/sabre.io/ns'); } /** @@ -350,6 +359,15 @@ public function testParseClarkNotationFail() Service::parseClarkNotation('http://sabredav.org/ns}elem'); } + public function providesEmptyInput() + { + $emptyResource = fopen('php://input', 'r'); + $data[] = [$emptyResource]; + $data[] = ['']; + + return $data; + } + public function providesEmptyPropfinds() { return [