Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-366: Implemented DateMetadataCriterion to REST API #3100

Merged
merged 9 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,108 @@
*/
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
{
const OPERATORS = [
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
'IN' => Operator::IN,
'EQ' => Operator::EQ,
'GT' => Operator::GT,
'GTE' => Operator::GTE,
'LT' => Operator::LT,
'LTE' => Operator::LTE,
'BETWEEN' => Operator::BETWEEN,
];

/**
* DateMetadata target: modification date.
*/
const MODIFIED = 'modified';
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved

/**
* DateMetadata target: creation date.
*/
const CREATED = 'created';
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved

public const TARGETS = [
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
self::MODIFIED,
self::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)) {
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
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'])) {
throw new Exceptions\Parser('Invalid <Value> format');
}

$value = is_array($dateMetadata['Value'])
? $dateMetadata['Value']
: explode(',', $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.
*
* For the full list of supported operators:
*
* @return \eZ\Publish\API\Repository\Values\Content\Query\Criterion\DateMetadata
* @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,211 @@
<?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;

class DateMetadataTest extends BaseTest
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
{
public function testParseProvider()
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
{
return [
[
['DateMetadataCriterion' => ['Target' => 'modified', 'Value' => '14,1620739489', 'Operator' => 'BETWEEN']],
new DateMetadataCriterion('modified', Operator::BETWEEN, [14, 1620739489]),
],
[
['DateMetadataCriterion' => ['Target' => 'created', 'Value' => [14, 1620739489], 'Operator' => 'BETWEEN']],
new DateMetadataCriterion('created', 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.
*
* @dataProvider testParseProvider
*/
public function testParse($data, $expected)
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
{
$dateMetadata = $this->getParser();
$result = $dateMetadata->parse($data, $this->getParsingDispatcherMock());

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

/**
* Test DateMetaData parser throwing exception on invalid UserMetadataCriterion format.
*
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
* @expectedExceptionMessage Invalid <DateMetaDataCriterion> format
*/
public function testParseExceptionOnInvalidCriterionFormat()
{
$inputArray = [
'foo' => 'Michael learns to mock',
];

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

/**
* Test DateMetaData parser throwing exception on invalid target format.
*
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
* @expectedExceptionMessage Invalid <Target> format
*/
public function testParseExceptionOnInvalidTargetFormat()
{
$inputArray = [
'DateMetaDataCriterion' => [
'foo' => 'Mock around the clock',
'Value' => 42,
],
];

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

/**
* Test DateMetaData parser throwing exception on wrong target format.
*
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
* @expectedExceptionMessage Invalid <Target> format
*/
public function testParseExceptionOnWrongTargetType()
{
$inputArray = [
'DateMetaDataCriterion' => [
'Target' => 'Mock around the clock',
'Value' => 42,
],
];

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

/**
* Test DateMetaData parser throwing exception on invalid value format.
*
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
* @expectedExceptionMessage Invalid <Value> format
*/
public function testParseExceptionOnInvalidValueFormat()
{
$inputArray = [
'DateMetaDataCriterion' => [
'Target' => 'Moxette',
'foo' => 42,
],
];

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

/**
* Test DateMetaData parser throwing exception on wrong type of value format.
*
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
* @expectedExceptionMessage Invalid <Value> format
*/
public function testParseExceptionOnWrongValueType()
{
$inputArray = [
'DateMetaDataCriterion' => [
'Target' => 'We will mock you',
'Value' => new \stdClass(),
],
];

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

/**
* Test DateMetaData parser throwing exception on invalid value format.
*
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
* @expectedExceptionMessage Invalid <Operator> format
*/
public function testParseExceptionOnInvalidOperatorFormat()
{
$inputArray = [
'DateMetaDataCriterion' => [
'Target' => 'modified',
'foo' => 42,
],
];

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

/**
* Test DateMetaData parser throwing exception on wrong type of value format.
*
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
* @expectedExceptionMessage Invalid <Operator> format
*/
public function testParseExceptionOnWrongOperatorType()
{
$inputArray = [
'DateMetaDataCriterion' => [
'Target' => 'modified',
'Value' => 42,
'Operator' => 'GTE',
],
];

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

/**
* Returns the DateMetaData criterion parser.
*
* @return \eZ\Publish\Core\REST\Server\Input\Parser\Criterion\DateMetadata
*/
protected function internalGetParser()
{
return new DateMetadata();
}
}