add query parser tests

This commit is contained in:
Robin Appelman 2017-02-01 15:13:29 +01:00
commit 5f6b358839
5 changed files with 107 additions and 2 deletions

View file

@ -26,9 +26,23 @@ use Sabre\Xml\Reader;
use Sabre\Xml\XmlDeserializable; use Sabre\Xml\XmlDeserializable;
class Literal implements XmlDeserializable { class Literal implements XmlDeserializable {
/** @var string|boolean|\DateTime|integer */ /**
* @var string|boolean|\DateTime|integer
*
* The value of the literal
*/
public $value; public $value;
/**
* Literal constructor.
*
* @param bool|\DateTime|int|string $value
*/
public function __construct($value = '') {
$this->value = $value;
}
static function xmlDeserialize(Reader $reader) { static function xmlDeserialize(Reader $reader) {
$literal = new self(); $literal = new self();

View file

@ -58,6 +58,18 @@ class Operator implements XmlDeserializable {
*/ */
public $arguments; public $arguments;
/**
* Operator constructor.
*
* @param string $type
* @param array $arguments
*/
public function __construct($type = '', array $arguments = []) {
$this->type = $type;
$this->arguments = $arguments;
}
static function xmlDeserialize(Reader $reader) { static function xmlDeserialize(Reader $reader) {
$operator = new self(); $operator = new self();

View file

@ -42,6 +42,17 @@ class Order implements XmlDeserializable {
*/ */
public $order; public $order;
/**
* Order constructor.
*
* @param string $property
* @param string $order
*/
public function __construct($property = '', $order = self::ASC) {
$this->property = $property;
$this->order = $order;
}
static function xmlDeserialize(Reader $reader) { static function xmlDeserialize(Reader $reader) {
$order = new self(); $order = new self();

View file

@ -29,7 +29,8 @@ class Scope implements XmlDeserializable {
/** /**
* @var string * @var string
* *
* The absolute url of the search scope * The scope of the search, either as absolute uri or as a path relative to the
* search arbiter.
*/ */
public $href; public $href;
@ -42,6 +43,15 @@ class Scope implements XmlDeserializable {
*/ */
public $depth; public $depth;
/**
* @param string $href
* @param int|string $depth
*/
public function __construct($href = '', $depth = 1) {
$this->href = $href;
$this->depth = $depth;
}
static function xmlDeserialize(Reader $reader) { static function xmlDeserialize(Reader $reader) {
$scope = new self(); $scope = new self();

58
tests/QueryParserTest.php Normal file
View file

@ -0,0 +1,58 @@
<?php
/**
* @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace SearchDAV\Test;
use SearchDAV\DAV\QueryParser;
use SearchDAV\XML\BasicSearch;
use SearchDAV\XML\Literal;
use SearchDAV\XML\Operator;
use SearchDAV\XML\Order;
use SearchDAV\XML\Scope;
class QueryParserTest extends \PHPUnit_Framework_TestCase {
public function testParseBasicQuery() {
$query = file_get_contents(__DIR__ . '/basicquery.xml');
$parser = new QueryParser();
$xml = $parser->parse($query, null, $rootElementName);
$this->assertEquals('{DAV:}searchrequest', $rootElementName);
$this->assertArrayHasKey('{DAV:}basicsearch', $xml);
/** @var BasicSearch $search */
$search = $xml['{DAV:}basicsearch'];
$this->assertInstanceOf(BasicSearch::class, $search);
$this->assertEquals(['{DAV:}getcontentlength'], $search->select);
$this->assertEquals([
new Scope('/container1/', 'infinity')
], $search->from);
$this->assertEquals(new Operator(Operator::OPERATION_GREATER_THAN, [
'{DAV:}getcontentlength',
new Literal(10000)
]), $search->where);
$this->assertEquals([
new Order('{DAV:}getcontentlength', Order::ASC)
], $search->orderBy);
}
}