diff --git a/tests/Mooc/Courses/Application/Find/CourseFinderTest.php b/tests/Mooc/Courses/Application/Find/CourseFinderTest.php new file mode 100644 index 000000000..8742ceddd --- /dev/null +++ b/tests/Mooc/Courses/Application/Find/CourseFinderTest.php @@ -0,0 +1,58 @@ +courseFinder = new CourseFinder($this->repository()); + } + + public function testCourseFinderShouldFindAnExistingCourse() + { + $course = $this->givenACourse(); + $actual = $this->whenFinderIsInvoked($course->id(), $course); + $this->thenTheCourseShouldBeTheExpected($course, $actual); + } + + public function testCourseFinderShouldThrowExceptionWhenCourseNotFound() + { + $course = $this->givenACourse(); + $this->whenFinderIsInvokedThenShouldThrowCourseNotFoundException($course->id()); + } + + private function givenACourse(): Course + { + return CourseMother::create(); + } + + private function whenFinderIsInvoked(CourseId $courseId, Course $course): Course + { + $this->shouldSearch($courseId, $course); + return $this->courseFinder->__invoke($course->id()); + } + + private function thenTheCourseShouldBeTheExpected(Course $course, Course $actual): void + { + $this->assertSimilar($course, $actual); + } + + private function whenFinderIsInvokedThenShouldThrowCourseNotFoundException(CourseId $courseId) + { + $this->expectException(CourseNotExist::class); + $this->searchShouldThrowException($courseId, new CourseNotExist($courseId)); + $this->courseFinder->__invoke($courseId); + } +} diff --git a/tests/Mooc/Courses/CoursesModuleUnitTestCase.php b/tests/Mooc/Courses/CoursesModuleUnitTestCase.php index 99aec4245..c4a02f0d5 100644 --- a/tests/Mooc/Courses/CoursesModuleUnitTestCase.php +++ b/tests/Mooc/Courses/CoursesModuleUnitTestCase.php @@ -7,6 +7,7 @@ use CodelyTv\Mooc\Courses\Domain\Course; use CodelyTv\Mooc\Courses\Domain\CourseRepository; use CodelyTv\Mooc\Shared\Domain\Courses\CourseId; +use CodelyTv\Shared\Domain\DomainError; use CodelyTv\Tests\Shared\Infrastructure\PhpUnit\UnitTestCase; use Mockery\MockInterface; @@ -36,4 +37,13 @@ protected function repository(): CourseRepository|MockInterface { return $this->repository = $this->repository ?? $this->mock(CourseRepository::class); } + + protected function searchShouldThrowException(CourseId $id, DomainError $exception) + { + $this->repository() + ->shouldReceive('search') + ->with($this->similarTo($id)) + ->once() + ->andThrow($exception); + } }