From 62ffad515ba0a53a19386c9887fcfa04bc380154 Mon Sep 17 00:00:00 2001 From: Adrian Gonzalez Date: Thu, 1 Apr 2021 12:04:52 +0200 Subject: [PATCH 1/2] Add CourseFinderTest with semantic given when then test for happy path --- .../Application/Find/CourseFinderTest.php | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tests/Mooc/Courses/Application/Find/CourseFinderTest.php diff --git a/tests/Mooc/Courses/Application/Find/CourseFinderTest.php b/tests/Mooc/Courses/Application/Find/CourseFinderTest.php new file mode 100644 index 00000000..0594ac0b --- /dev/null +++ b/tests/Mooc/Courses/Application/Find/CourseFinderTest.php @@ -0,0 +1,44 @@ +courseFinder = new CourseFinder($this->repository()); + } + + public function testCourseFinderShouldFindAnExistingCourse() + { + $course = $this->givenAnExistingCourse(); + $actual = $this->whenFinderIsInvoked($course->id(), $course); + $this->thenTheCourseShouldBeTheExpected($course, $actual); + } + + private function givenAnExistingCourse(): 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); + } +} From 1e3baf86cd34db2cb049745ee8f0e70343b3522f Mon Sep 17 00:00:00 2001 From: Adrian Gonzalez Date: Thu, 1 Apr 2021 12:54:04 +0200 Subject: [PATCH 2/2] Add test to check CourseNotFoundException in semantic way --- .../Application/Find/CourseFinderTest.php | 18 ++++++++++++++++-- .../Mooc/Courses/CoursesModuleUnitTestCase.php | 10 ++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/tests/Mooc/Courses/Application/Find/CourseFinderTest.php b/tests/Mooc/Courses/Application/Find/CourseFinderTest.php index 0594ac0b..8742cedd 100644 --- a/tests/Mooc/Courses/Application/Find/CourseFinderTest.php +++ b/tests/Mooc/Courses/Application/Find/CourseFinderTest.php @@ -3,6 +3,7 @@ namespace CodelyTv\Tests\Mooc\Courses\Application\Find; use CodelyTv\Mooc\Courses\Application\Find\CourseFinder; +use CodelyTv\Mooc\Courses\Domain\CourseNotExist; use CodelyTv\Mooc\Shared\Domain\Courses\CourseId; use CodelyTv\Tests\Mooc\Courses\CoursesModuleUnitTestCase; use CodelyTv\Tests\Mooc\Courses\Domain\CourseMother; @@ -21,12 +22,18 @@ protected function setUp(): void public function testCourseFinderShouldFindAnExistingCourse() { - $course = $this->givenAnExistingCourse(); + $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 givenAnExistingCourse(): Course + private function givenACourse(): Course { return CourseMother::create(); } @@ -41,4 +48,11 @@ private function thenTheCourseShouldBeTheExpected(Course $course, Course $actual { $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 99aec424..c4a02f0d 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); + } }