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

Added useCase to find the last video #317

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions src/Mooc/Videos/Application/FindLast/FindLastQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare (strict_types=1);

namespace CodelyTv\Mooc\Videos\Application\FindLast;

use Elasticsearch\Endpoints\Sql\Query;

final class FindLastQuery extends Query
{

}
30 changes: 30 additions & 0 deletions src/Mooc/Videos/Application/FindLast/FindLastQueryHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare (strict_types=1);

namespace CodelyTv\Mooc\Videos\Application\FindLast;

use CodelyTv\Mooc\Videos\Domain\Video;
use CodelyTv\Mooc\Videos\Domain\VideoRepository;
use CodelyTv\Shared\Domain\Bus\Query\QueryHandler;
use CodelyTv\Shared\Domain\Criteria\Criteria;
use CodelyTv\Shared\Domain\Criteria\Filters;
use CodelyTv\Shared\Domain\Criteria\Order;
use CodelyTv\Shared\Domain\Criteria\OrderBy;
use CodelyTv\Shared\Domain\Criteria\OrderType;

final class FindLastQueryHandler implements QueryHandler
{

public function __construct(private VideoRepository $videoRepository)
{
}

public function __invoke(FindLastQuery $query): ?Video
{
$order = new Order(new OrderBy('created'), OrderType::desc());
$criteria = new Criteria(filters: new Filters([]), order: $order, offset: null, limit: 1);

return $this->videoRepository->searchByCriteria($criteria)->first();
}
}
4 changes: 4 additions & 0 deletions src/Mooc/Videos/Domain/Video.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
use CodelyTv\Mooc\Shared\Domain\Courses\CourseId;
use CodelyTv\Mooc\Shared\Domain\Videos\VideoUrl;
use CodelyTv\Shared\Domain\Aggregate\AggregateRoot;
use DateTimeImmutable;

final class Video extends AggregateRoot
{
private DateTimeImmutable $created;

public function __construct(
private readonly VideoId $id,
private readonly VideoType $type,
private VideoTitle $title,
private readonly VideoUrl $url,
private readonly CourseId $courseId
) {
$this->created = new DateTimeImmutable();
}

public static function create(
Expand Down
9 changes: 9 additions & 0 deletions src/Mooc/Videos/Domain/Videos.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@ protected function type(): string
{
return Video::class;
}

public function first(): ?Video
{
if (0 === $this->count()) {
return null;
}

return $this->items()[0];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare (strict_types=1);

namespace CodelyTv\Tests\Mooc\Videos\Application\FindLast;

use CodelyTv\Mooc\Shared\Domain\Courses\CourseId;
use CodelyTv\Mooc\Shared\Domain\Videos\VideoUrl;
use CodelyTv\Mooc\Videos\Application\FindLast\FindLastQuery;
use CodelyTv\Mooc\Videos\Application\FindLast\FindLastQueryHandler;
use CodelyTv\Mooc\Videos\Domain\Video;
use CodelyTv\Mooc\Videos\Domain\VideoId;
use CodelyTv\Mooc\Videos\Domain\VideoRepository;
use CodelyTv\Mooc\Videos\Domain\Videos;
use CodelyTv\Mooc\Videos\Domain\VideoTitle;
use CodelyTv\Mooc\Videos\Domain\VideoType;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class FindLastQueryHandlerTest extends TestCase
{
private MockObject|VideoRepository $videoRepository;
private FindLastQueryHandler $queryHandler;

protected function setUp(): void
{
$this->videoRepository = $this->createMock(VideoRepository::class);
$this->queryHandler = new FindLastQueryHandler($this->videoRepository);
}

public function testMustReturnNullGivenRepositoryWithoutVideos(): void
{
$this->videoRepository->expects(self::once())->method('searchByCriteria')->willReturn(new Videos([]));

$video = $this->queryHandler->__invoke(new FindLastQuery());

self::assertNull($video);
}

public function testMustReturnLastVideoGivenRepositoryWithVideos(): void
{
$video1 = Video::create(
VideoId::random(),
VideoType::interview(),
new VideoTitle('my video1 title'),
new VideoUrl('https://myvdeo1url.com'),
CourseId::random()
);
$video2 = Video::create(
VideoId::random(),
VideoType::interview(),
new VideoTitle('my video2 title'),
new VideoUrl('https://myvdeo2url.com'),
CourseId::random()
);
$this->videoRepository->expects(self::once())->method('searchByCriteria')->willReturn(new Videos([$video1, $video2]));

$video = $this->queryHandler->__invoke(new FindLastQuery());
self::assertSame($video, $video1);
}


}