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

Fixed EventRepository trait issue with datetime string conversion + t… #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
Fixed EventRepository trait issue with datetime string conversion + t…
…wig extension service
jpottier committed Sep 10, 2021
commit b216287265d687b6500cf24e70217af01dd71437
32 changes: 9 additions & 23 deletions src/Bridge/Doctrine/ORM/EventRepository.php
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@

use CalendR\Event\EventInterface;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;

/**
@@ -28,31 +27,18 @@ public function getEventsQueryBuilder(\DateTimeInterface $begin, \DateTimeInterf
{
$qb = $this->createQueryBuilderForGetEvent($options);

$beginField = $this->getBeginFieldName();
$endField = $this->getEndFieldName();

return $qb
->andWhere(
$qb->expr()->orX(
// Period in event
$qb->expr()->andX(
$qb->expr()->lte($this->getBeginFieldName(), $begin),
$qb->expr()->gte($this->getEndFieldName(), $end)
),
// Event in period
$qb->expr()->andX(
$qb->expr()->gte($this->getBeginFieldName(), $begin),
$qb->expr()->lt($this->getEndFieldName(), $end)
),
// Event begins during period
$qb->expr()->andX(
$qb->expr()->lt($this->getBeginFieldName(), $end),
$qb->expr()->gte($this->getBeginFieldName(), $begin)
),
// Event ends during period
$qb->expr()->andX(
$qb->expr()->gte($this->getEndFieldName(), $begin),
$qb->expr()->lt($this->getEndFieldName(), $end)
)
$qb->expr()->andX(
"${beginField} < :end",
"${endField} > :begin"
)
);
)
->setParameter(':begin', $begin)
->setParameter(':end', $end);
}

public function getEventsQuery(\DateTimeInterface $begin, \DateTimeInterface $end, array $options = []): AbstractQuery
8 changes: 3 additions & 5 deletions src/Bridge/Symfony/Bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
@@ -6,11 +6,9 @@ services:
CalendR\Event\Manager:
class: 'CalendR\Event\Manager'

CalendR\Extension\Twig\CalendRExtension:
class: 'CalendR\Extension\Twig\CalendRExtension'
arguments: [ '@calendr' ]
tags:
- { name: twig.extension }
CalendR\Bridge\Twig\CalendRExtension:
autowire: true
autoconfigure: true

calendr:
alias: 'CalendR\Calendar'
6 changes: 3 additions & 3 deletions tests/Bridge/Doctrine/ORM/EventRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -62,9 +62,9 @@ public function testGetEvents($begin, $end, array $providedEvents): void
$this->qb->expects($this->once())->method('from')->willReturn($this->qb);
$this->qb->expects($this->once())->method('andWhere')->willReturn($this->qb);
$this->qb->expects($this->once())->method('getQuery')->willReturn($query);
$this->qb->expects($this->atLeastOnce())->method('expr')->willReturn($expr);
$expr->expects($this->once())->method('orX');
$expr->expects($this->exactly(4))->method('andX');
$this->qb->expects($this->exactly(2))->method('setParameter')->willReturn($this->qb);
$this->qb->expects($this->once())->method('expr')->willReturn($expr);
$expr->expects($this->once())->method('andX');

$events = $this->repo->getEvents(new \DateTimeImmutable($begin), new \DateTimeImmutable($end));
$this->assertSame($providedEvents, $events);