Skip to content

Commit

Permalink
IBX-366: Implemented DateMetadataCriterion to REST API (#3100)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszdebinski committed May 21, 2021
1 parent b473d13 commit 2de5aa8
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class DateMetadata extends Criterion
/**
* DateMetadata target: modification date.
*/
const MODIFIED = 'modified';
public const MODIFIED = 'modified';

/**
* DateMetadata target: creation date.
*/
const CREATED = 'created';
public const CREATED = 'created';

/**
* Creates a new DateMetadata criterion on $metadata.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,96 @@
*/
namespace eZ\Publish\Core\REST\Server\Input\Parser\Criterion;

use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator;
use eZ\Publish\Core\REST\Common\Input\BaseParser;
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\DateMetadata as DateMetadataCriterion;
use eZ\Publish\Core\REST\Common\Exceptions;

/**
* Parser for ViewInput Criterion.
*/
class DateMetadata extends BaseParser
{
private const OPERATORS = [
'IN' => Operator::IN,
'EQ' => Operator::EQ,
'GT' => Operator::GT,
'GTE' => Operator::GTE,
'LT' => Operator::LT,
'LTE' => Operator::LTE,
'BETWEEN' => Operator::BETWEEN,
];

private const TARGETS = [
DateMetadataCriterion::MODIFIED,
DateMetadataCriterion::CREATED,
];

/**
* Parses input structure to a Criterion object.
*
* @param array $data
* @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
*
* @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser
*/
public function parse(array $data, ParsingDispatcher $parsingDispatcher): DateMetadataCriterion
{
if (!isset($data['DateMetadataCriterion'])) {
throw new Exceptions\Parser('Invalid <DateMetaDataCriterion> format');
}

$dateMetadata = $data['DateMetadataCriterion'];

if (!isset($dateMetadata['Target'])) {
throw new Exceptions\Parser('Invalid <Target> format');
}

$target = strtolower($dateMetadata['Target']);

if (!in_array($target, self::TARGETS, true)) {
throw new Exceptions\Parser('Invalid <Target> format');
}

if (!isset($dateMetadata['Value'])) {
throw new Exceptions\Parser('Invalid <Value> format');
}

if (!in_array(gettype($dateMetadata['Value']), ['integer', 'array'], true)) {
throw new Exceptions\Parser('Invalid <Value> format');
}

$value = $dateMetadata['Value'];

if (!isset($dateMetadata['Operator'])) {
throw new Exceptions\Parser('Invalid <Operator> format');
}

$operator = $this->getOperator($dateMetadata['Operator']);

return new DateMetadataCriterion($target, $operator, $value);
}

/**
* Get operator for the given literal name.
*
* @return \eZ\Publish\API\Repository\Values\Content\Query\Criterion\DateMetadata
* For the full list of supported operators:
*
* @see \eZ\Publish\Core\REST\Server\Input\Parser\Criterion\DateMetadata::OPERATORS
*/
public function parse(array $data, ParsingDispatcher $parsingDispatcher)
private function getOperator(string $operatorName): string
{
throw new \Exception('@todo implement');
$operatorName = strtoupper($operatorName);
if (!isset(self::OPERATORS[$operatorName])) {
throw new Exceptions\Parser(
sprintf(
'Unexpected DateMetadata operator. Expected one of: %s',
implode(', ', array_keys(self::OPERATORS))
)
);
}

return self::OPERATORS[$operatorName];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\Core\REST\Server\Tests\Input\Parser\Criterion;

use eZ\Publish\API\Repository\Values\Content\Query\Criterion\DateMetadata as DateMetadataCriterion;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator;
use eZ\Publish\Core\REST\Server\Input\Parser\Criterion\DateMetadata;
use eZ\Publish\Core\REST\Server\Tests\Input\Parser\BaseTest;
use eZ\Publish\Core\REST\Common\Exceptions;

final class DateMetadataTest extends BaseTest
{
public function testParseProvider(): iterable
{
return [
[
['DateMetadataCriterion' => ['Target' => 'modified', 'Value' => [14, 1620739489], 'Operator' => 'BETWEEN']],
new DateMetadataCriterion('modified', Operator::BETWEEN, [14, 1620739489]),
],
[
['DateMetadataCriterion' => ['Target' => 'modified', 'Value' => 14, 'Operator' => 'GT']],
new DateMetadataCriterion('modified', Operator::GT, 14),
],
[
['DateMetadataCriterion' => ['Target' => 'created', 'Value' => 14, 'Operator' => 'GTE']],
new DateMetadataCriterion('created', Operator::GTE, 14),
],
[
['DateMetadataCriterion' => ['Target' => 'created', 'Value' => 14, 'Operator' => 'EQ']],
new DateMetadataCriterion('created', Operator::EQ, 14),
],
[
['DateMetadataCriterion' => ['Target' => 'created', 'Value' => 1620739489, 'Operator' => 'LT']],
new DateMetadataCriterion('created', Operator::LT, 1620739489),
],
[
['DateMetadataCriterion' => ['Target' => 'created', 'Value' => 1620739489, 'Operator' => 'LTE']],
new DateMetadataCriterion('created', Operator::LTE, 1620739489),
],
[
['DateMetadataCriterion' => ['Target' => 'created', 'Value' => [14, 58, 167, 165245, 1620739489], 'Operator' => 'IN']],
new DateMetadataCriterion('created', Operator::IN, [14, 58, 167, 165245, 1620739489]),
],
];
}

/**
* Tests the DateMetaData parser.
*
* @param string[] $data
* @dataProvider testParseProvider
*/
public function testParse(array $data, DateMetadataCriterion $expected): void
{
$dateMetadata = $this->getParser();
$result = $dateMetadata->parse($data, $this->getParsingDispatcherMock());

$this->assertEquals(
$expected,
$result,
'DateMetadata parser not created correctly.'
);
}

public function testParseExceptionOnInvalidCriterionFormat(): void
{
$this->expectExceptionMessage('Invalid <DateMetaDataCriterion> format');
$this->expectException(Exceptions\Parser::class);
$inputArray = [
'foo' => 'Michael learns to mock',
];

$dataKeyValueObjectClass = $this->getParser();
$dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock());
}

public function testParseExceptionOnInvalidTargetFormat(): void
{
$this->expectExceptionMessage('Invalid <Target> format');
$this->expectException(Exceptions\Parser::class);

$inputArray = [
'DateMetadataCriterion' => [
'foo' => 'Mock around the clock',
'Value' => 42,
],
];

$dataKeyValueObjectClass = $this->getParser();
$dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock());
}

public function testParseExceptionOnWrongTargetType(): void
{
$this->expectExceptionMessage('Invalid <Target> format');
$this->expectException(Exceptions\Parser::class);

$inputArray = [
'DateMetadataCriterion' => [
'Target' => 'Mock around the clock',
'Value' => 42,
],
];

$dataKeyValueObjectClass = $this->getParser();
$dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock());
}

public function testParseExceptionOnInvalidValueFormat(): void
{
$this->expectExceptionMessage('Invalid <Value> format');
$this->expectException(Exceptions\Parser::class);

$inputArray = [
'DateMetadataCriterion' => [
'Target' => 'modified',
'foo' => 42,
],
];

$dataKeyValueObjectClass = $this->getParser();
$dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock());
}

public function testParseExceptionOnWrongValueType(): void
{
$this->expectExceptionMessage('Invalid <Value> format');
$this->expectException(Exceptions\Parser::class);

$inputArray = [
'DateMetadataCriterion' => [
'Target' => 'modified',
'Value' => new \stdClass(),
],
];

$dataKeyValueObjectClass = $this->getParser();
$dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock());
}

public function testParseExceptionOnInvalidOperatorFormat(): void
{
$this->expectExceptionMessage('Invalid <Operator> format');
$this->expectException(Exceptions\Parser::class);

$inputArray = [
'DateMetadataCriterion' => [
'Target' => 'modified',
'Value' => 42,
],
];

$dataKeyValueObjectClass = $this->getParser();
$dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock());
}

protected function internalGetParser(): DateMetadata
{
return new DateMetadata();
}
}

0 comments on commit 2de5aa8

Please sign in to comment.