Skip to content

Commit

Permalink
feat: Date range Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Apr 26, 2024
1 parent 35529ba commit 05313ad
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/Dto/Search/Query/Filter/RelativeDateRangeFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace Atoolo\Search\Dto\Search\Query\Filter;

use DateInterval;
use InvalidArgumentException;

class RelativeDateRangeFilter extends Filter
{
public function __construct(
private readonly ?DateInterval $from,
private readonly ?DateInterval $to,
?string $key = null
) {
parent::__construct(
$key,
$key !== null ? [$key] : []
);
}

public function getQuery(): string
{
return 'sp_date_list:' . $this->toSolrDateRage();
}

private function toSolrDateRage(): string
{
if ($this->from === null) {
$from = "NOW/DAY";
} else {
$from = $this->toSolrIntervalSyntax($this->from);
}

if ($this->to === null) {
$to = "NOW/DAY+1DAY-1SECOND";
} else {
$to = $this->toSolrIntervalSyntax($this->to);
}

return '[' . $from . ' TO ' . $to . ']';
}

private function toSolrIntervalSyntax(DateInterval $value): string
{
$interval = 'NOW';
if ($value->y > 0) {
$interval = $interval . '-' . $value->y . 'YEARS';
}
if ($value->m > 0) {
$interval = $interval . '-' . $value->m . 'MONTHS';
}
if ($value->d > 0) {
$interval = $interval . '-' . $value->d . 'DAYS';
}
if ($value->h > 0) {
throw new InvalidArgumentException(
'Hours are not supported for the RelativeDateRangeFilter'
);
}
if ($value->i > 0) {
throw new InvalidArgumentException(
'Minutes are not supported for the RelativeDateRangeFilter'
);
}
if ($value->s > 0) {
throw new InvalidArgumentException(
'Seconds are not supported for the RelativeDateRangeFilter'
);
}

return $interval . '/DAY';
}
}
44 changes: 44 additions & 0 deletions test/Dto/Search/Query/Filter/AbsoluteDateRangeFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Atoolo\Search\Test\Dto\Search\Query\Filter;

use Atoolo\Search\Dto\Search\Query\Filter\AbsoluteDateRangeFilter;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(AbsoluteDateRangeFilter::class)]
class AbsoluteDateRangeFilterTest extends TestCase
{
public function testGetQueryWithFromAndTo(): void
{
$from = new \DateTime('2021-01-01 00:00:00');
$to = new \DateTime('2021-01-02 00:00:00');
$filter = new AbsoluteDateRangeFilter($from, $to, 'sp_date_list');
$this->assertEquals(
'sp_date_list:[2021-01-01T00:00:00Z TO 2021-01-02T00:00:00Z]',
$filter->getQuery()
);
}

public function testGetQueryWithFrom(): void
{
$from = new \DateTime('2021-01-01 00:00:00');
$filter = new AbsoluteDateRangeFilter($from, null, 'sp_date_list');
$this->assertEquals(
'sp_date_list:[2021-01-01T00:00:00Z TO *]',
$filter->getQuery()
);
}

public function testGetQueryWithTo(): void
{
$to = new \DateTime('2021-01-02 00:00:00');
$filter = new AbsoluteDateRangeFilter(null, $to, 'sp_date_list');
$this->assertEquals(
'sp_date_list:[* TO 2021-01-02T00:00:00Z]',
$filter->getQuery()
);
}
}
143 changes: 143 additions & 0 deletions test/Dto/Search/Query/Filter/RelativeDateRangeFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

declare(strict_types=1);

namespace Atoolo\Search\Test\Dto\Search\Query\Filter;

use Atoolo\Search\Dto\Search\Query\Filter\RelativeDateRangeFilter;
use DateInterval;
use Exception;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

#[CoversClass(RelativeDateRangeFilter::class)]
class RelativeDateRangeFilterTest extends TestCase
{
/**
* @return array<array{string, string}>
*/
public static function additionProviderForFromIntervals(): array
{
return [
['P1D', 'sp_date_list:[NOW-1DAYS/DAY TO NOW/DAY+1DAY-1SECOND]'],
['P1W', 'sp_date_list:[NOW-7DAYS/DAY TO NOW/DAY+1DAY-1SECOND]'],
['P2M', 'sp_date_list:[NOW-2MONTHS/DAY TO NOW/DAY+1DAY-1SECOND]'],
['P3Y', 'sp_date_list:[NOW-3YEARS/DAY TO NOW/DAY+1DAY-1SECOND]'],
];
}

/**
* @return array<array{string, string}>
*/
public static function additionProviderForToIntervals(): array
{
return [
['P1D', 'sp_date_list:[NOW/DAY TO NOW-1DAYS/DAY]'],
['P1W', 'sp_date_list:[NOW/DAY TO NOW-7DAYS/DAY]'],
['P2M', 'sp_date_list:[NOW/DAY TO NOW-2MONTHS/DAY]'],
['P3Y', 'sp_date_list:[NOW/DAY TO NOW-3YEARS/DAY]'],
];
}

/**
* @return array<array{string, string, string}>
*/
public static function additionProviderForFromAndToIntervals(): array
{
return [
['P1D', 'P1D', 'sp_date_list:[NOW-1DAYS/DAY TO NOW-1DAYS/DAY]'],
['P1W', 'P2M', 'sp_date_list:[NOW-7DAYS/DAY TO NOW-2MONTHS/DAY]'],
];
}

/**
* @return array<array{string}>
*/
public static function additionProviderForInvalidIntervals(): array
{
return [
['PT1H'],
['PT1M'],
['PT1S'],
];
}

/**
* @throws Exception
*/
#[DataProvider('additionProviderForFromIntervals')]
public function testGetQueryWithFrom(
string $from,
string $expected
): void {
$filter = new RelativeDateRangeFilter(
new DateInterval($from),
null,
);

$this->assertEquals(
$expected,
$filter->getQuery(),
'unexpected query'
);
}

/**
* @throws Exception
*/
#[DataProvider('additionProviderForToIntervals')]
public function testGetQueryWithTo(
string $to,
string $expected
): void {
$filter = new RelativeDateRangeFilter(
null,
new DateInterval($to),
);

$this->assertEquals(
$expected,
$filter->getQuery(),
'unexpected query'
);
}

/**
* @throws Exception
*/
#[DataProvider('additionProviderForFromAndToIntervals')]
public function testGetQueryWithFromAndTo(
string $from,
string $to,
string $expected
): void {
$filter = new RelativeDateRangeFilter(
new DateInterval($from),
new DateInterval($to),
);

$this->assertEquals(
$expected,
$filter->getQuery(),
'unexpected query'
);
}


/**
* @throws Exception
*/
#[DataProvider('additionProviderForInvalidIntervals')]
public function testGetQueryWithInvalidIntervals(
string $interval
): void {
$filter = new RelativeDateRangeFilter(
null,
new DateInterval($interval),
);
$this->expectException(InvalidArgumentException::class);
$filter->getQuery();
}
}
14 changes: 14 additions & 0 deletions test/Service/Indexer/InternalResourceIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,20 @@ public function testIndexAllWithChunks(): void
$this->indexer->index();
}

public function testIndexAllWithEmptyList(): void
{
$this->finder->method('findAll')
->willReturn([
]);
$this->indexerProgressHandler->expects($this->once())
->method('start')
->with(0);
$this->indexerProgressHandler->expects($this->once())
->method('finish');

$this->indexer->index();
}

public function testIndexSkipResource(): void
{
$this->finder->method('findAll')
Expand Down

0 comments on commit 05313ad

Please sign in to comment.