Skip to content

Commit

Permalink
Fix missing sorting in BufferedMatchDayLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusklocke committed Jan 9, 2022
1 parent e4ee5a3 commit ca3ffcf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
41 changes: 35 additions & 6 deletions src/Infrastructure/API/GraphQL/Loader/BufferedMatchDayLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use HexagonalPlayground\Infrastructure\Persistence\Read\Criteria\EqualityFilter;
use HexagonalPlayground\Infrastructure\Persistence\Read\Criteria\Filter;
use HexagonalPlayground\Infrastructure\Persistence\Read\Criteria\Sorting;
use HexagonalPlayground\Infrastructure\Persistence\Read\MatchDayRepository;

class BufferedMatchDayLoader
Expand Down Expand Up @@ -51,9 +52,23 @@ public function getBySeason(string $seasonId): array
$seasonIds = array_keys($this->bySeasonId, null, true);

if (count($seasonIds)) {
$filter = new EqualityFilter($this->matchDayRepository->getField('season_id'), Filter::MODE_INCLUDE, $seasonIds);

$matchDays = $this->matchDayRepository->findMany([$filter], [], null, 'season_id');
$filter = new EqualityFilter(
$this->matchDayRepository->getField('season_id'),
Filter::MODE_INCLUDE,
$seasonIds
);

$sorting = new Sorting(
$this->matchDayRepository->getField('number'),
Sorting::DIRECTION_ASCENDING
);

$matchDays = $this->matchDayRepository->findMany(
[$filter],
[$sorting],
null,
'season_id'
);

foreach ($seasonIds as $id) {
$this->bySeasonId[$id] = $matchDays[$id] ?? [];
Expand All @@ -72,9 +87,23 @@ public function getByTournament(string $tournamentId): array
$tournamentIds = array_keys($this->byTournamentId, null ,true);

if (count($tournamentIds)) {
$filter = new EqualityFilter($this->matchDayRepository->getField('tournament_id'), Filter::MODE_INCLUDE, $tournamentIds);

$matchDays = $this->matchDayRepository->findMany([$filter], [], null, 'tournament_id');
$filter = new EqualityFilter(
$this->matchDayRepository->getField('tournament_id'),
Filter::MODE_INCLUDE,
$tournamentIds
);

$sorting = new Sorting(
$this->matchDayRepository->getField('number'),
Sorting::DIRECTION_ASCENDING
);

$matchDays = $this->matchDayRepository->findMany(
[$filter],
[$sorting],
null,
'tournament_id'
);

foreach ($tournamentIds as $id) {
$this->byTournamentId[$id] = $matchDays[$id] ?? [];
Expand Down
2 changes: 2 additions & 0 deletions src/Infrastructure/API/GraphQL/SeasonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function __construct()
/** @var BufferedTeamLoader $loader */
$loader = $context->getContainer()->get(BufferedTeamLoader::class);
$loader->addSeason($root['id']);

return new Deferred(function () use ($loader, $root) {
return $loader->getBySeason($root['id']);
});
Expand All @@ -52,6 +53,7 @@ public function __construct()
/** @var BufferedMatchDayLoader $loader */
$loader = $context->getContainer()->get(BufferedMatchDayLoader::class);
$loader->addSeason($root['id']);

return new Deferred(function () use ($loader, $root) {
return $loader->getBySeason($root['id']);
});
Expand Down
8 changes: 8 additions & 0 deletions tests/GraphQL/SeasonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,17 @@ public function testSeasonCanBeStarted(string $seasonId): string
self::assertSame(count($dates), count($season->match_days));

$matchCount = 0;
$previousMatchDay = null;
foreach ($season->match_days as $matchDay) {
$matchCount += count($matchDay->matches);

if ($previousMatchDay !== null) {
self::assertGreaterThan($previousMatchDay->number, $matchDay->number);
}

$previousMatchDay = $matchDay;
}

self::assertSame(count($dates) * count(self::$teamIds) / 2, $matchCount);

return $season->id;
Expand Down

0 comments on commit ca3ffcf

Please sign in to comment.