From 79f2198d74110be9cee1d8b6cb039f2b2855f51b Mon Sep 17 00:00:00 2001 From: isra Date: Thu, 28 Jul 2022 08:39:24 +0200 Subject: [PATCH] Added useCase to find the last video --- .../Application/FindLast/FindLastQuery.php | 12 ++++ .../FindLast/FindLastQueryHandler.php | 30 +++++++++ src/Mooc/Videos/Domain/Video.php | 4 ++ src/Mooc/Videos/Domain/Videos.php | 9 +++ .../FindLast/FindLastQueryHandlerTest.php | 63 +++++++++++++++++++ 5 files changed, 118 insertions(+) create mode 100644 src/Mooc/Videos/Application/FindLast/FindLastQuery.php create mode 100644 src/Mooc/Videos/Application/FindLast/FindLastQueryHandler.php create mode 100644 tests/Mooc/Videos/Application/FindLast/FindLastQueryHandlerTest.php diff --git a/src/Mooc/Videos/Application/FindLast/FindLastQuery.php b/src/Mooc/Videos/Application/FindLast/FindLastQuery.php new file mode 100644 index 000000000..4d03da17b --- /dev/null +++ b/src/Mooc/Videos/Application/FindLast/FindLastQuery.php @@ -0,0 +1,12 @@ +videoRepository->searchByCriteria($criteria)->first(); + } +} \ No newline at end of file diff --git a/src/Mooc/Videos/Domain/Video.php b/src/Mooc/Videos/Domain/Video.php index 6028dc104..24e030daa 100644 --- a/src/Mooc/Videos/Domain/Video.php +++ b/src/Mooc/Videos/Domain/Video.php @@ -7,9 +7,12 @@ 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, @@ -17,6 +20,7 @@ public function __construct( private readonly VideoUrl $url, private readonly CourseId $courseId ) { + $this->created = new DateTimeImmutable(); } public static function create( diff --git a/src/Mooc/Videos/Domain/Videos.php b/src/Mooc/Videos/Domain/Videos.php index e749b0726..3a046780c 100644 --- a/src/Mooc/Videos/Domain/Videos.php +++ b/src/Mooc/Videos/Domain/Videos.php @@ -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]; + } } diff --git a/tests/Mooc/Videos/Application/FindLast/FindLastQueryHandlerTest.php b/tests/Mooc/Videos/Application/FindLast/FindLastQueryHandlerTest.php new file mode 100644 index 000000000..206cdd28d --- /dev/null +++ b/tests/Mooc/Videos/Application/FindLast/FindLastQueryHandlerTest.php @@ -0,0 +1,63 @@ +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); + } + + +}