Skip to content
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
58 changes: 58 additions & 0 deletions tests/Mooc/Courses/Application/Find/CourseFinderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

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;
use CodelyTv\Mooc\Courses\Domain\Course;

class CourseFinderTest extends CoursesModuleUnitTestCase
{
private CourseFinder $courseFinder;

protected function setUp(): void
{
parent::setUp();

$this->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);
}
}
10 changes: 10 additions & 0 deletions tests/Mooc/Courses/CoursesModuleUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}