From 410c3fac1edf6493c1bdf8e49ee83fc09010285a Mon Sep 17 00:00:00 2001 From: Eduardo Bravo Date: Sat, 9 Oct 2021 16:41:41 -0300 Subject: [PATCH 1/2] Update video command --- .../Update/UpdateVideoTitleCommand.php | 28 +++++++++++++++++++ .../Update/UpdateVideoTitleCommandHandler.php | 26 +++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/Mooc/Videos/Application/Update/UpdateVideoTitleCommand.php create mode 100644 src/Mooc/Videos/Application/Update/UpdateVideoTitleCommandHandler.php diff --git a/src/Mooc/Videos/Application/Update/UpdateVideoTitleCommand.php b/src/Mooc/Videos/Application/Update/UpdateVideoTitleCommand.php new file mode 100644 index 00000000..e8b48e04 --- /dev/null +++ b/src/Mooc/Videos/Application/Update/UpdateVideoTitleCommand.php @@ -0,0 +1,28 @@ +id; + } + + public function title(): string + { + return $this->title; + } + + +} diff --git a/src/Mooc/Videos/Application/Update/UpdateVideoTitleCommandHandler.php b/src/Mooc/Videos/Application/Update/UpdateVideoTitleCommandHandler.php new file mode 100644 index 00000000..69b54def --- /dev/null +++ b/src/Mooc/Videos/Application/Update/UpdateVideoTitleCommandHandler.php @@ -0,0 +1,26 @@ +titleUpdater = $titleUpdater; + } + + public function __invoke(UpdateVideoTitleCommand $command) + { + $id = new VideoId($command->id()); + $title = new VideoTitle($command->title()); + + $this->titleUpdater->__invoke($id, $title); + } +} From 12a5f80a142165d330fd5a693ef690605cb5a79e Mon Sep 17 00:00:00 2001 From: Eduardo Bravo Date: Sat, 9 Oct 2021 16:42:46 -0300 Subject: [PATCH 2/2] Update video command tests --- .../Update/UpdateVideoTitleCommandMother.php | 34 ++++++++++++++ .../Application/Update/VideoFinderTest.php | 36 +++++++++++++++ .../Update/VideoRenamerCommandTest.php | 44 ++++++++++++++++++ tests/Mooc/Videos/Domain/VideoIdMother.php | 17 +++++++ tests/Mooc/Videos/Domain/VideoMother.php | 46 +++++++++++++++++++ tests/Mooc/Videos/Domain/VideoTitleMother.php | 14 ++++++ tests/Mooc/Videos/Domain/VideoTypeMother.php | 13 ++++++ tests/Mooc/Videos/Domain/VideoUrlMother.php | 14 ++++++ .../Mooc/Videos/VideosModuleUnitTestCase.php | 42 +++++++++++++++++ tests/Shared/Domain/UrlMother.php | 11 +++++ 10 files changed, 271 insertions(+) create mode 100644 tests/Mooc/Videos/Application/Update/UpdateVideoTitleCommandMother.php create mode 100644 tests/Mooc/Videos/Application/Update/VideoFinderTest.php create mode 100644 tests/Mooc/Videos/Application/Update/VideoRenamerCommandTest.php create mode 100644 tests/Mooc/Videos/Domain/VideoIdMother.php create mode 100644 tests/Mooc/Videos/Domain/VideoMother.php create mode 100644 tests/Mooc/Videos/Domain/VideoTitleMother.php create mode 100644 tests/Mooc/Videos/Domain/VideoTypeMother.php create mode 100644 tests/Mooc/Videos/Domain/VideoUrlMother.php create mode 100644 tests/Mooc/Videos/VideosModuleUnitTestCase.php create mode 100644 tests/Shared/Domain/UrlMother.php diff --git a/tests/Mooc/Videos/Application/Update/UpdateVideoTitleCommandMother.php b/tests/Mooc/Videos/Application/Update/UpdateVideoTitleCommandMother.php new file mode 100644 index 00000000..e7248da6 --- /dev/null +++ b/tests/Mooc/Videos/Application/Update/UpdateVideoTitleCommandMother.php @@ -0,0 +1,34 @@ +value() ?? VideoIdMother::create()->value(), + $title->value() ?? VideoTitleMother::create()->value() + ); + } + + public static function withId(VideoId $id): UpdateVideoTitleCommand + { + return self::create($id, VideoTitleMother::create()); + } + + public static function withIdAndTitle(VideoId $id, VideoTitle $title): UpdateVideoTitleCommand + { + return self::create($id, $title); + } +} diff --git a/tests/Mooc/Videos/Application/Update/VideoFinderTest.php b/tests/Mooc/Videos/Application/Update/VideoFinderTest.php new file mode 100644 index 00000000..e41401c0 --- /dev/null +++ b/tests/Mooc/Videos/Application/Update/VideoFinderTest.php @@ -0,0 +1,36 @@ +handler = new UpdateVideoTitleCommandHandler(new VideoTitleUpdater($this->repository())); + } + + /** @test */ + public function it_should_update_video_title_when_video_exists(): void + { + $video = VideoMother::create(); + $newTitle = VideoTitleMother::create(); + $command = UpdateVideoTitleCommandMother::withIdAndTitle($video->id(), $newTitle); + $renamedVideo = DuplicatorMother::with($video, ['title' => $newTitle]); + + $this->shouldSearch($video->id(), $video); + $this->shouldSave($renamedVideo); + + $this->handler->__invoke($command); + } +} diff --git a/tests/Mooc/Videos/Application/Update/VideoRenamerCommandTest.php b/tests/Mooc/Videos/Application/Update/VideoRenamerCommandTest.php new file mode 100644 index 00000000..ae6b91dd --- /dev/null +++ b/tests/Mooc/Videos/Application/Update/VideoRenamerCommandTest.php @@ -0,0 +1,44 @@ +finder = new VideoFinder($this->repository()); + } + + /** @test */ + public function it_should_throw_an_exception_when_the_video_not_exist() + { + $this->expectException(VideoNotFound::class); + + $id = VideoIdMother::create(); + + $this->shouldSearch($id, null); + + $this->finder->__invoke($id); + } + + /** @test */ + public function it_should_find_an_existing_video(): void + { + $video = VideoMother::create(); + + $this->shouldSearch($video->id(), $video); + + $videoFromRepository = $this->finder->__invoke($video->id()); + + $this->assertEquals($videoFromRepository, $video); + } +} diff --git a/tests/Mooc/Videos/Domain/VideoIdMother.php b/tests/Mooc/Videos/Domain/VideoIdMother.php new file mode 100644 index 00000000..1f272cce --- /dev/null +++ b/tests/Mooc/Videos/Domain/VideoIdMother.php @@ -0,0 +1,17 @@ +id()), + VideoTypeMother::create($request->type()), + VideoTitleMother::create($request->title()), + VideoUrlMother::create($request->url()), + CourseIdMother::create($request->courseId()), + ); + } +} diff --git a/tests/Mooc/Videos/Domain/VideoTitleMother.php b/tests/Mooc/Videos/Domain/VideoTitleMother.php new file mode 100644 index 00000000..b7320b89 --- /dev/null +++ b/tests/Mooc/Videos/Domain/VideoTitleMother.php @@ -0,0 +1,14 @@ +value()); + } +} diff --git a/tests/Mooc/Videos/Domain/VideoUrlMother.php b/tests/Mooc/Videos/Domain/VideoUrlMother.php new file mode 100644 index 00000000..409f0303 --- /dev/null +++ b/tests/Mooc/Videos/Domain/VideoUrlMother.php @@ -0,0 +1,14 @@ +repository() + ->shouldReceive('save') + ->with($this->similarTo($video)) + ->once() + ->andReturnNull(); + } + + protected function shouldSearch(VideoId $id, ?Video $video): void + { + $this->repository() + ->shouldReceive('search') + ->with($this->similarTo($id)) + ->once() + ->andReturn($video); + } + + protected function repository(): VideoRepository|MockInterface + { + return $this->repository = $this->repository ?? $this->mock(VideoRepository::class); + } +} diff --git a/tests/Shared/Domain/UrlMother.php b/tests/Shared/Domain/UrlMother.php new file mode 100644 index 00000000..38b59e7d --- /dev/null +++ b/tests/Shared/Domain/UrlMother.php @@ -0,0 +1,11 @@ +url; + } +}