Skip to content

Commit

Permalink
Simplify all ObjectMothers
Browse files Browse the repository at this point in the history
  • Loading branch information
rgomezcasas committed Nov 13, 2020
1 parent f3eb51d commit e0b66b1
Show file tree
Hide file tree
Showing 37 changed files with 141 additions and 210 deletions.
2 changes: 1 addition & 1 deletion src/Shared/Domain/ValueObject/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static function randomValue()
return self::values()[array_rand(self::values())];
}

public static function random(): self
public static function random(): static
{
return new static(self::randomValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected function setUp(): void
/** @test */
public function it_should_authenticate_a_valid_user(): void
{
$command = AuthenticateUserCommandMother::random();
$command = AuthenticateUserCommandMother::create();
$authUser = AuthUserMother::fromCommand($command);

$this->shouldSearch($authUser->username(), $authUser);
Expand All @@ -39,7 +39,7 @@ public function it_should_throw_an_exception_when_the_user_does_not_exist(): voi
{
$this->expectException(InvalidAuthUsername::class);

$command = AuthenticateUserCommandMother::random();
$command = AuthenticateUserCommandMother::create();
$username = AuthUsernameMother::create($command->username());

$this->shouldSearch($username);
Expand All @@ -52,8 +52,8 @@ public function it_should_throw_an_exception_when_the_password_does_not_math():
{
$this->expectException(InvalidAuthCredentials::class);

$command = AuthenticateUserCommandMother::random();
$authUser = AuthUserMother::withUsername(AuthUsernameMother::create($command->username()));
$command = AuthenticateUserCommandMother::create();
$authUser = AuthUserMother::create(username: AuthUsernameMother::create($command->username()));

$this->shouldSearch($authUser->username(), $authUser);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

final class AuthenticateUserCommandMother
{
public static function create(AuthUsername $username, AuthPassword $password): AuthenticateUserCommand
{
return new AuthenticateUserCommand($username->value(), $password->value());
}

public static function random(): AuthenticateUserCommand
{
return self::create(AuthUsernameMother::random(), AuthPasswordMother::random());
public static function create(
?AuthUsername $username = null,
?AuthPassword $password = null
): AuthenticateUserCommand {
return new AuthenticateUserCommand(
$username?->value() ?? AuthUsernameMother::create()->value(),
$password?->value() ?? AuthPasswordMother::create()->value()
);
}
}
9 changes: 2 additions & 7 deletions tests/Backoffice/Auth/Domain/AuthPasswordMother.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@

final class AuthPasswordMother
{
public static function create(string $value): AuthPassword
public static function create(?string $value = null): AuthPassword
{
return new AuthPassword($value);
}

public static function random(): AuthPassword
{
return self::create(UuidMother::random());
return new AuthPassword($value ?? UuidMother::create());
}
}
9 changes: 2 additions & 7 deletions tests/Backoffice/Auth/Domain/AuthUserMother.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

final class AuthUserMother
{
public static function create(AuthUsername $username, AuthPassword $password): AuthUser
public static function create(?AuthUsername $username = null, ?AuthPassword $password = null): AuthUser
{
return new AuthUser($username, $password);
return new AuthUser($username ?? AuthUsernameMother::create(), $password ?? AuthPasswordMother::create());
}

public static function fromCommand(AuthenticateUserCommand $command): AuthUser
Expand All @@ -23,9 +23,4 @@ public static function fromCommand(AuthenticateUserCommand $command): AuthUser
AuthPasswordMother::create($command->password())
);
}

public static function withUsername(AuthUsername $username): AuthUser
{
return self::create($username, AuthPasswordMother::random());
}
}
9 changes: 2 additions & 7 deletions tests/Backoffice/Auth/Domain/AuthUsernameMother.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@

final class AuthUsernameMother
{
public static function create(string $value): AuthUsername
public static function create(?string $value = null): AuthUsername
{
return new AuthUsername($value);
}

public static function random(): AuthUsername
{
return self::create(WordMother::random());
return new AuthUsername($value ?? WordMother::create());
}
}
15 changes: 3 additions & 12 deletions tests/Backoffice/Courses/Domain/BackofficeCourseMother.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,9 @@ final class BackofficeCourseMother
public static function create(?string $id = null, ?string $name = null, ?string $duration = null): BackofficeCourse
{
return new BackofficeCourse(
$id ?? CourseIdMother::random()->value(),
$name ?? CourseNameMother::random()->value(),
$duration ?? CourseDurationMother::random()->value()
);
}

public static function random(): BackofficeCourse
{
return self::create(
CourseIdMother::random()->value(),
CourseNameMother::random()->value(),
CourseDurationMother::random()->value()
$id ?? CourseIdMother::create()->value(),
$name ?? CourseNameMother::create()->value(),
$duration ?? CourseDurationMother::create()->value()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ final class ElasticsearchBackofficeCourseRepositoryTest extends BackofficeCourse
/** @test */
public function it_should_save_a_valid_course(): void
{
$this->elasticRepository()->save(BackofficeCourseMother::random());
$this->elasticRepository()->save(BackofficeCourseMother::create());
}

/** @test */
public function it_should_search_all_existing_courses(): void
{
$existingCourse = BackofficeCourseMother::random();
$anotherExistingCourse = BackofficeCourseMother::random();
$existingCourse = BackofficeCourseMother::create();
$anotherExistingCourse = BackofficeCourseMother::create();
$existingCourses = [$existingCourse, $anotherExistingCourse];

$this->elasticRepository()->save($existingCourse);
Expand All @@ -33,8 +33,8 @@ public function it_should_search_all_existing_courses(): void
/** @test */
public function it_should_search_all_existing_courses_with_an_empty_criteria(): void
{
$existingCourse = BackofficeCourseMother::random();
$anotherExistingCourse = BackofficeCourseMother::random();
$existingCourse = BackofficeCourseMother::create();
$anotherExistingCourse = BackofficeCourseMother::create();
$existingCourses = [$existingCourse, $anotherExistingCourse];

$this->elasticRepository()->save($existingCourse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ final class MySqlBackofficeCourseRepositoryTest extends BackofficeCoursesModuleI
/** @test */
public function it_should_save_a_valid_course(): void
{
$this->mySqlRepository()->save(BackofficeCourseMother::random());
$this->mySqlRepository()->save(BackofficeCourseMother::create());
}

/** @test */
public function it_should_search_all_existing_courses(): void
{
$existingCourse = BackofficeCourseMother::random();
$anotherExistingCourse = BackofficeCourseMother::random();
$existingCourse = BackofficeCourseMother::create();
$anotherExistingCourse = BackofficeCourseMother::create();
$existingCourses = [$existingCourse, $anotherExistingCourse];

$this->mySqlRepository()->save($existingCourse);
Expand All @@ -33,8 +33,8 @@ public function it_should_search_all_existing_courses(): void
/** @test */
public function it_should_search_all_existing_courses_with_an_empty_criteria(): void
{
$existingCourse = BackofficeCourseMother::random();
$anotherExistingCourse = BackofficeCourseMother::random();
$existingCourse = BackofficeCourseMother::create();
$anotherExistingCourse = BackofficeCourseMother::create();
$existingCourses = [$existingCourse, $anotherExistingCourse];

$this->mySqlRepository()->save($existingCourse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function setUp(): void
/** @test */
public function it_should_create_a_valid_course(): void
{
$command = CreateCourseCommandMother::random();
$command = CreateCourseCommandMother::create();

$course = CourseMother::fromRequest($command);
$domainEvent = CourseCreatedDomainEventMother::fromCourse($course);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

final class CreateCourseCommandMother
{
public static function create(CourseId $id, CourseName $name, CourseDuration $duration): CreateCourseCommand
{
return new CreateCourseCommand($id->value(), $name->value(), $duration->value());
}

public static function random(): CreateCourseCommand
{
return self::create(CourseIdMother::random(), CourseNameMother::random(), CourseDurationMother::random());
public static function create(
?CourseId $id = null,
?CourseName $name = null,
?CourseDuration $duration = null
): CreateCourseCommand {
return new CreateCourseCommand(
$id?->value() ?? CourseIdMother::create()->value(),
$name?->value() ?? CourseNameMother::create()->value(),
$duration?->value() ?? CourseDurationMother::create()->value()
);
}
}
8 changes: 4 additions & 4 deletions tests/Mooc/Courses/Application/Update/CourseRenamerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ protected function setUp(): void
/** @test */
public function it_should_rename_an_existing_course(): void
{
$course = CourseMother::random();
$newName = CourseNameMother::random();
$course = CourseMother::create();
$newName = CourseNameMother::create();
$renamedCourse = DuplicatorMother::with($course, ['name' => $newName]);

$this->shouldSearch($course->id(), $course);
Expand All @@ -42,10 +42,10 @@ public function it_should_throw_an_exception_when_the_course_not_exist(): void
{
$this->expectException(CourseNotExist::class);

$id = CourseIdMother::random();
$id = CourseIdMother::create();

$this->shouldSearch($id, null);

$this->renamer->__invoke($id, CourseNameMother::random());
$this->renamer->__invoke($id, CourseNameMother::create());
}
}
18 changes: 10 additions & 8 deletions tests/Mooc/Courses/Domain/CourseCreatedDomainEventMother.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@

final class CourseCreatedDomainEventMother
{
public static function create(CourseId $id, CourseName $name, CourseDuration $duration): CourseCreatedDomainEvent
{
return new CourseCreatedDomainEvent($id->value(), $name->value(), $duration->value());
public static function create(
?CourseId $id = null,
?CourseName $name = null,
?CourseDuration $duration = null
): CourseCreatedDomainEvent {
return new CourseCreatedDomainEvent(
$id?->value() ?? CourseIdMother::create()->value(),
$name?->value() ?? CourseNameMother::create()->value(),
$duration?->value() ?? CourseDurationMother::create()->value()
);
}

public static function fromCourse(Course $course): CourseCreatedDomainEvent
{
return self::create($course->id(), $course->name(), $course->duration());
}

public static function random(): CourseCreatedDomainEvent
{
return self::create(CourseIdMother::random(), CourseNameMother::random(), CourseDurationMother::random());
}
}
16 changes: 7 additions & 9 deletions tests/Mooc/Courses/Domain/CourseDurationMother.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@

final class CourseDurationMother
{
public static function create(string $value): CourseDuration
public static function create(?string $value = null): CourseDuration
{
return new CourseDuration($value);
return new CourseDuration($value ?? self::random());
}

public static function random(): CourseDuration
private static function random(): string
{
return self::create(
sprintf(
'%s %s',
IntegerMother::lessThan(100),
RandomElementPicker::from('months', 'years', 'days', 'hours', 'minutes', 'seconds')
)
return sprintf(
'%s %s',
IntegerMother::lessThan(100),
RandomElementPicker::from('months', 'years', 'days', 'hours', 'minutes', 'seconds')
);
}
}
14 changes: 2 additions & 12 deletions tests/Mooc/Courses/Domain/CourseIdMother.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,8 @@

final class CourseIdMother
{
public static function create(string $value): CourseId
public static function create(?string $value = null): CourseId
{
return new CourseId($value);
}

public static function creator(): callable
{
return static fn() => self::random();
}

public static function random(): CourseId
{
return self::create(UuidMother::random());
return new CourseId($value ?? UuidMother::create());
}
}
18 changes: 10 additions & 8 deletions tests/Mooc/Courses/Domain/CourseMother.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@

final class CourseMother
{
public static function create(CourseId $id, CourseName $name, CourseDuration $duration): Course
{
return new Course($id, $name, $duration);
public static function create(
?CourseId $id = null,
?CourseName $name = null,
?CourseDuration $duration = null
): Course {
return new Course(
$id ?? CourseIdMother::create(),
$name ?? CourseNameMother::create(),
$duration ?? CourseDurationMother::create()
);
}

public static function fromRequest(CreateCourseCommand $request): Course
Expand All @@ -25,9 +32,4 @@ public static function fromRequest(CreateCourseCommand $request): Course
CourseDurationMother::create($request->duration())
);
}

public static function random(): Course
{
return self::create(CourseIdMother::random(), CourseNameMother::random(), CourseDurationMother::random());
}
}
9 changes: 2 additions & 7 deletions tests/Mooc/Courses/Domain/CourseNameMother.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@

final class CourseNameMother
{
public static function create(string $value): CourseName
public static function create(?string $value = null): CourseName
{
return new CourseName($value);
}

public static function random(): CourseName
{
return self::create(WordMother::random());
return new CourseName($value ?? WordMother::create());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ final class CourseRepositoryTest extends CoursesModuleInfrastructureTestCase
/** @test */
public function it_should_save_a_course(): void
{
$course = CourseMother::random();
$course = CourseMother::create();

$this->repository()->save($course);
}

/** @test */
public function it_should_return_an_existing_course(): void
{
$course = CourseMother::random();
$course = CourseMother::create();

$this->repository()->save($course);

Expand All @@ -31,6 +31,6 @@ public function it_should_return_an_existing_course(): void
/** @test */
public function it_should_not_return_a_non_existing_course(): void
{
$this->assertNull($this->repository()->search(CourseIdMother::random()));
$this->assertNull($this->repository()->search(CourseIdMother::create()));
}
}
Loading

0 comments on commit e0b66b1

Please sign in to comment.