-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IBX-366: Implemented DateMetadataCriterion to REST API (#3100)
- Loading branch information
1 parent
b473d13
commit 2de5aa8
Showing
3 changed files
with
240 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
eZ/Publish/Core/REST/Server/Tests/Input/Parser/Criterion/DateMetadataTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |