Skip to content

Commit

Permalink
IBX-6827: Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs committed Dec 8, 2023
1 parent fcc30f7 commit ca2d9b6
Show file tree
Hide file tree
Showing 7 changed files with 331 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ final class DateTimeStepRangesGenerator implements RangesGeneratorInterface

private bool $isRightOpen = true;


public function __construct(DateTimeInterface $start, DateTimeInterface $end)
{
$this->start = $start;
Expand Down Expand Up @@ -97,6 +96,12 @@ public function setRightOpen(bool $isRightOpen): self
*/
public function generate(): array
{
if ($this->start == $this->end && $this->isLeftOpen && $this->isRightOpen) {
return [
new Range(Range::INF, Range::INF),
];
}

$ranges = [];

if ($this->isLeftOpen) {
Expand All @@ -109,7 +114,7 @@ public function generate(): array
$current = DateTimeImmutable::createFromMutable($current);
}

while ($current <= $this->end) {
while ($current < $this->end) {
$next = $current->add($this->step);
$ranges[] = Range::ofDateTime($current, $next);
$current = $next;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ public function isLeftOpen(): bool
return $this->isLeftOpen;
}

public function setLeftOpen(bool $isLeftOpen): void
public function setLeftOpen(bool $isLeftOpen): self
{
$this->isLeftOpen = $isLeftOpen;

return $this;
}

public function isRightOpen(): bool
Expand All @@ -91,6 +93,12 @@ public function setRightOpen(bool $isRightOpen): self
*/
public function generate(): array
{
if ($this->start === $this->end && $this->isLeftOpen && $this->isRightOpen) {
return [
new Range(Range::INF, Range::INF),
];
}

$ranges = [];

if ($this->isLeftOpen) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\Ranges;

use Generator;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\Range;

final class IntegerStepRangesGenerator implements RangesGeneratorInterface
Expand Down Expand Up @@ -69,9 +70,11 @@ public function isLeftOpen(): bool
return $this->isLeftOpen;
}

public function setLeftOpen(bool $isLeftOpen): void
public function setLeftOpen(bool $isLeftOpen): self
{
$this->isLeftOpen = $isLeftOpen;

return $this;
}

public function isRightOpen(): bool
Expand All @@ -89,8 +92,14 @@ public function setRightOpen(bool $isRightOpen): self
/**
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\Range[]
*/
public function generate(): \Generator
public function generate(): Generator
{
if ($this->start === $this->end && $this->isLeftOpen && $this->isRightOpen) {
yield new Range(Range::INF, Range::INF);

return;
}

if ($this->isLeftOpen) {
yield Range::ofInt(Range::INF, $this->start);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\Ranges;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Integration\Core\Repository\Values\Content\Query\Aggregation\Ranges;

use DateInterval;
use DateTimeImmutable;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\Range;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\Ranges\DateTimeStepRangesGenerator;
use PHPUnit\Framework\TestCase;

final class DateTimeStepRangesGeneratorTest extends TestCase
{
public function testGenerateOpenRangesSequence(): void
{
self::assertGeneratorResults(
[
$this->createRange(Range::INF, '01-01-2023'),
$this->createRange('01-01-2023', '02-01-2023'),
$this->createRange('02-01-2023', '03-01-2023'),
$this->createRange('03-01-2023', Range::INF),
],
new DateTimeStepRangesGenerator(
new DateTimeImmutable('01-01-2023 00:00:00'),
new DateTimeImmutable('03-01-2023 00:00:00')
)
);
}

public function testGenerateCloseRangesSequence(): void
{
$generator = new DateTimeStepRangesGenerator(
new DateTimeImmutable('01-01-2023 00:00:00'),
new DateTimeImmutable('03-01-2023 00:00:00')
);
$generator->setLeftOpen(false);
$generator->setRightOpen(false);

self::assertGeneratorResults(
[
$this->createRange('01-01-2023', '02-01-2023'),
$this->createRange('02-01-2023', '03-01-2023'),
],
$generator
);
}

public function testGenerateRangesWithCustomStep(): void
{
$generator = new DateTimeStepRangesGenerator(
new DateTimeImmutable('01-01-2023 00:00:00'),
new DateTimeImmutable('05-01-2023 00:00:00')
);
$generator->setStep(new DateInterval('P2D'));

self::assertGeneratorResults(
[
$this->createRange(Range::INF, '01-01-2023'),
$this->createRange('01-01-2023', '03-01-2023'),
$this->createRange('03-01-2023', '05-01-2023'),
$this->createRange('05-01-2023', Range::INF),
],
$generator
);
}

public function testGenerateInfRangesSequence(): void
{
$generator = new DateTimeStepRangesGenerator(
new DateTimeImmutable('01-01-1970 00:00:00'),
new DateTimeImmutable('01-01-1970 00:00:00'),
);

self::assertGeneratorResults(
[
Range::ofDateTime(Range::INF, Range::INF),
],
$generator
);
}

public function testGenerateEmptyRangesSequence(): void
{
$generator = new DateTimeStepRangesGenerator(
new DateTimeImmutable('01-01-1970 00:00:00'),
new DateTimeImmutable('01-01-1970 00:00:00'),
);
$generator->setLeftOpen(false);
$generator->setRightOpen(false);

self::assertGeneratorResults([], $generator);
}

private function createRange(?string $start, ?string $end): Range
{
return Range::ofDateTime(
$start !== null ? new DateTimeImmutable($start . ' 00:00:00') : null,
$end !== null ? new DateTimeImmutable($end . ' 00:00:00') : null
);
}

/**
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\Range[] $expectedResult
*/
private static function assertGeneratorResults(array $expectedResult, DateTimeStepRangesGenerator $generator): void
{
self::assertEquals($expectedResult, $generator->generate());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Integration\Core\Repository\Values\Content\Query\Aggregation\Ranges;

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\Range;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\Ranges\FloatStepRangesGenerator;
use PHPUnit\Framework\TestCase;

final class FloatStepRangesGeneratorTest extends TestCase
{
public function testGenerateOpenRangesSequence(): void
{
self::assertGeneratorResults(
[
Range::ofFloat(Range::INF, 1.0),
Range::ofFloat(1.0, 2.0),
Range::ofFloat(2.0, 3.0),
Range::ofFloat(3.0, Range::INF),
],
new FloatStepRangesGenerator(1.0, 3.0)
);
}

public function testGenerateCloseRangesSequence(): void
{
$generator = new FloatStepRangesGenerator(1.0, 3.0);
$generator->setLeftOpen(false);
$generator->setRightOpen(false);

self::assertGeneratorResults(
[
Range::ofFloat(1.0, 2.0),
Range::ofFloat(2.0, 3.0),
],
$generator
);
}

public function testGenerateRangesWithCustomStep(): void
{
$generator = new FloatStepRangesGenerator(1.0, 10.0);
$generator->setStep(2.0);

self::assertGeneratorResults(
[
Range::ofFloat(Range::INF, 1.0),
Range::ofFloat(1.0, 3.0),
Range::ofFloat(3.0, 5.0),
Range::ofFloat(5.0, 7.0),
Range::ofFloat(7.0, 9.0),
Range::ofFloat(10.0, Range::INF),
],
$generator
);
}

public function testGenerateInfRangesSequence(): void
{
$generator = new FloatStepRangesGenerator(0.0, 0.0);

self::assertGeneratorResults(
[
Range::ofFloat(Range::INF, Range::INF),
],
$generator
);
}

public function testGenerateEmptyRangesSequence(): void
{
$generator = new FloatStepRangesGenerator(0.0, 0.0);
$generator->setLeftOpen(false);
$generator->setRightOpen(false);

self::assertGeneratorResults([], $generator);
}

/**
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\Range[] $expectedResult
*/
private static function assertGeneratorResults(array $expectedResult, FloatStepRangesGenerator $generator): void
{
self::assertEquals($expectedResult, $generator->generate());
}
}
Loading

0 comments on commit ca2d9b6

Please sign in to comment.