diff --git a/src/Shared/Domain/ValueObject/Enum.php b/src/Shared/Domain/ValueObject/Enum.php index d3a754c02..18afa4d3f 100644 --- a/src/Shared/Domain/ValueObject/Enum.php +++ b/src/Shared/Domain/ValueObject/Enum.php @@ -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()); } diff --git a/tests/Backoffice/Auth/Application/Authenticate/AuthenticateUserCommandHandlerTest.php b/tests/Backoffice/Auth/Application/Authenticate/AuthenticateUserCommandHandlerTest.php index 8555267f0..0d83c9376 100644 --- a/tests/Backoffice/Auth/Application/Authenticate/AuthenticateUserCommandHandlerTest.php +++ b/tests/Backoffice/Auth/Application/Authenticate/AuthenticateUserCommandHandlerTest.php @@ -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); @@ -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); @@ -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); diff --git a/tests/Backoffice/Auth/Application/Authenticate/AuthenticateUserCommandMother.php b/tests/Backoffice/Auth/Application/Authenticate/AuthenticateUserCommandMother.php index c58631ea2..d42c37d95 100644 --- a/tests/Backoffice/Auth/Application/Authenticate/AuthenticateUserCommandMother.php +++ b/tests/Backoffice/Auth/Application/Authenticate/AuthenticateUserCommandMother.php @@ -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() + ); } } diff --git a/tests/Backoffice/Auth/Domain/AuthPasswordMother.php b/tests/Backoffice/Auth/Domain/AuthPasswordMother.php index d88d293d0..6b344f818 100644 --- a/tests/Backoffice/Auth/Domain/AuthPasswordMother.php +++ b/tests/Backoffice/Auth/Domain/AuthPasswordMother.php @@ -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()); } } diff --git a/tests/Backoffice/Auth/Domain/AuthUserMother.php b/tests/Backoffice/Auth/Domain/AuthUserMother.php index 3238ac542..0776c4838 100644 --- a/tests/Backoffice/Auth/Domain/AuthUserMother.php +++ b/tests/Backoffice/Auth/Domain/AuthUserMother.php @@ -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 @@ -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()); - } } diff --git a/tests/Backoffice/Auth/Domain/AuthUsernameMother.php b/tests/Backoffice/Auth/Domain/AuthUsernameMother.php index fc87619b0..128f15382 100644 --- a/tests/Backoffice/Auth/Domain/AuthUsernameMother.php +++ b/tests/Backoffice/Auth/Domain/AuthUsernameMother.php @@ -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()); } } diff --git a/tests/Backoffice/Courses/Domain/BackofficeCourseMother.php b/tests/Backoffice/Courses/Domain/BackofficeCourseMother.php index ccb71e7a0..a28a35032 100644 --- a/tests/Backoffice/Courses/Domain/BackofficeCourseMother.php +++ b/tests/Backoffice/Courses/Domain/BackofficeCourseMother.php @@ -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() ); } } diff --git a/tests/Backoffice/Courses/Infrastructure/Persistence/ElasticsearchBackofficeCourseRepositoryTest.php b/tests/Backoffice/Courses/Infrastructure/Persistence/ElasticsearchBackofficeCourseRepositoryTest.php index dc6b59133..3a54c1407 100644 --- a/tests/Backoffice/Courses/Infrastructure/Persistence/ElasticsearchBackofficeCourseRepositoryTest.php +++ b/tests/Backoffice/Courses/Infrastructure/Persistence/ElasticsearchBackofficeCourseRepositoryTest.php @@ -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); @@ -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); diff --git a/tests/Backoffice/Courses/Infrastructure/Persistence/MySqlBackofficeCourseRepositoryTest.php b/tests/Backoffice/Courses/Infrastructure/Persistence/MySqlBackofficeCourseRepositoryTest.php index af4f67abb..bb35da789 100644 --- a/tests/Backoffice/Courses/Infrastructure/Persistence/MySqlBackofficeCourseRepositoryTest.php +++ b/tests/Backoffice/Courses/Infrastructure/Persistence/MySqlBackofficeCourseRepositoryTest.php @@ -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); @@ -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); diff --git a/tests/Mooc/Courses/Application/Create/CreateCourseCommandHandlerTest.php b/tests/Mooc/Courses/Application/Create/CreateCourseCommandHandlerTest.php index ba6db6151..6a85c1f1a 100644 --- a/tests/Mooc/Courses/Application/Create/CreateCourseCommandHandlerTest.php +++ b/tests/Mooc/Courses/Application/Create/CreateCourseCommandHandlerTest.php @@ -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); diff --git a/tests/Mooc/Courses/Application/Create/CreateCourseCommandMother.php b/tests/Mooc/Courses/Application/Create/CreateCourseCommandMother.php index ed22076ae..a1ba53fe2 100644 --- a/tests/Mooc/Courses/Application/Create/CreateCourseCommandMother.php +++ b/tests/Mooc/Courses/Application/Create/CreateCourseCommandMother.php @@ -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() + ); } } diff --git a/tests/Mooc/Courses/Application/Update/CourseRenamerTest.php b/tests/Mooc/Courses/Application/Update/CourseRenamerTest.php index ef68cd5f2..2815aa7fe 100644 --- a/tests/Mooc/Courses/Application/Update/CourseRenamerTest.php +++ b/tests/Mooc/Courses/Application/Update/CourseRenamerTest.php @@ -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); @@ -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()); } } diff --git a/tests/Mooc/Courses/Domain/CourseCreatedDomainEventMother.php b/tests/Mooc/Courses/Domain/CourseCreatedDomainEventMother.php index 33776136f..00044f5fe 100644 --- a/tests/Mooc/Courses/Domain/CourseCreatedDomainEventMother.php +++ b/tests/Mooc/Courses/Domain/CourseCreatedDomainEventMother.php @@ -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()); - } } diff --git a/tests/Mooc/Courses/Domain/CourseDurationMother.php b/tests/Mooc/Courses/Domain/CourseDurationMother.php index 14c279a7c..a3b78ae84 100644 --- a/tests/Mooc/Courses/Domain/CourseDurationMother.php +++ b/tests/Mooc/Courses/Domain/CourseDurationMother.php @@ -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') ); } } diff --git a/tests/Mooc/Courses/Domain/CourseIdMother.php b/tests/Mooc/Courses/Domain/CourseIdMother.php index a84fee05a..f80119695 100644 --- a/tests/Mooc/Courses/Domain/CourseIdMother.php +++ b/tests/Mooc/Courses/Domain/CourseIdMother.php @@ -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()); } } diff --git a/tests/Mooc/Courses/Domain/CourseMother.php b/tests/Mooc/Courses/Domain/CourseMother.php index f56953dfa..31a622989 100644 --- a/tests/Mooc/Courses/Domain/CourseMother.php +++ b/tests/Mooc/Courses/Domain/CourseMother.php @@ -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 @@ -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()); - } } diff --git a/tests/Mooc/Courses/Domain/CourseNameMother.php b/tests/Mooc/Courses/Domain/CourseNameMother.php index 6a01e95aa..210496b49 100644 --- a/tests/Mooc/Courses/Domain/CourseNameMother.php +++ b/tests/Mooc/Courses/Domain/CourseNameMother.php @@ -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()); } } diff --git a/tests/Mooc/Courses/Infrastructure/Persistence/CourseRepositoryTest.php b/tests/Mooc/Courses/Infrastructure/Persistence/CourseRepositoryTest.php index f15356a87..25b23b6b9 100644 --- a/tests/Mooc/Courses/Infrastructure/Persistence/CourseRepositoryTest.php +++ b/tests/Mooc/Courses/Infrastructure/Persistence/CourseRepositoryTest.php @@ -13,7 +13,7 @@ final class CourseRepositoryTest extends CoursesModuleInfrastructureTestCase /** @test */ public function it_should_save_a_course(): void { - $course = CourseMother::random(); + $course = CourseMother::create(); $this->repository()->save($course); } @@ -21,7 +21,7 @@ public function it_should_save_a_course(): void /** @test */ public function it_should_return_an_existing_course(): void { - $course = CourseMother::random(); + $course = CourseMother::create(); $this->repository()->save($course); @@ -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())); } } diff --git a/tests/Mooc/CoursesCounter/Application/Find/CoursesCounterResponseMother.php b/tests/Mooc/CoursesCounter/Application/Find/CoursesCounterResponseMother.php index f3b3c8b2b..7abaed048 100644 --- a/tests/Mooc/CoursesCounter/Application/Find/CoursesCounterResponseMother.php +++ b/tests/Mooc/CoursesCounter/Application/Find/CoursesCounterResponseMother.php @@ -6,11 +6,12 @@ use CodelyTv\Mooc\CoursesCounter\Application\Find\CoursesCounterResponse; use CodelyTv\Mooc\CoursesCounter\Domain\CoursesCounterTotal; +use CodelyTv\Tests\Mooc\CoursesCounter\Domain\CoursesCounterTotalMother; final class CoursesCounterResponseMother { - public static function create(CoursesCounterTotal $total): CoursesCounterResponse + public static function create(?CoursesCounterTotal $total = null): CoursesCounterResponse { - return new CoursesCounterResponse($total->value()); + return new CoursesCounterResponse($total?->value() ?? CoursesCounterTotalMother::create()->value()); } } diff --git a/tests/Mooc/CoursesCounter/Application/Find/FindCoursesCounterQueryHandlerTest.php b/tests/Mooc/CoursesCounter/Application/Find/FindCoursesCounterQueryHandlerTest.php index 6b5501fd0..630137874 100644 --- a/tests/Mooc/CoursesCounter/Application/Find/FindCoursesCounterQueryHandlerTest.php +++ b/tests/Mooc/CoursesCounter/Application/Find/FindCoursesCounterQueryHandlerTest.php @@ -25,7 +25,7 @@ protected function setUp(): void /** @test */ public function it_should_find_an_existing_courses_counter(): void { - $counter = CoursesCounterMother::random(); + $counter = CoursesCounterMother::create(); $query = new FindCoursesCounterQuery(); $response = CoursesCounterResponseMother::create($counter->total()); diff --git a/tests/Mooc/CoursesCounter/Application/Increment/IncrementCoursesCounterOnCourseCreatedTest.php b/tests/Mooc/CoursesCounter/Application/Increment/IncrementCoursesCounterOnCourseCreatedTest.php index ed01dac2c..9074bb70d 100644 --- a/tests/Mooc/CoursesCounter/Application/Increment/IncrementCoursesCounterOnCourseCreatedTest.php +++ b/tests/Mooc/CoursesCounter/Application/Increment/IncrementCoursesCounterOnCourseCreatedTest.php @@ -32,7 +32,7 @@ protected function setUp(): void /** @test */ public function it_should_initialize_a_new_counter(): void { - $event = CourseCreatedDomainEventMother::random(); + $event = CourseCreatedDomainEventMother::create(); $courseId = CourseIdMother::create($event->aggregateId()); $newCounter = CoursesCounterMother::withOne($courseId); @@ -49,10 +49,10 @@ public function it_should_initialize_a_new_counter(): void /** @test */ public function it_should_increment_an_existing_counter(): void { - $event = CourseCreatedDomainEventMother::random(); + $event = CourseCreatedDomainEventMother::create(); $courseId = CourseIdMother::create($event->aggregateId()); - $existingCounter = CoursesCounterMother::random(); + $existingCounter = CoursesCounterMother::create(); $incrementedCounter = CoursesCounterMother::incrementing($existingCounter, $courseId); $domainEvent = CoursesCounterIncrementedDomainEventMother::fromCounter($incrementedCounter); @@ -66,7 +66,7 @@ public function it_should_increment_an_existing_counter(): void /** @test */ public function it_should_not_increment_an_already_incremented_course(): void { - $event = CourseCreatedDomainEventMother::random(); + $event = CourseCreatedDomainEventMother::create(); $courseId = CourseIdMother::create($event->aggregateId()); $existingCounter = CoursesCounterMother::withOne($courseId); diff --git a/tests/Mooc/CoursesCounter/Domain/CoursesCounterIdMother.php b/tests/Mooc/CoursesCounter/Domain/CoursesCounterIdMother.php index ce3089cc6..e0dfc9c57 100644 --- a/tests/Mooc/CoursesCounter/Domain/CoursesCounterIdMother.php +++ b/tests/Mooc/CoursesCounter/Domain/CoursesCounterIdMother.php @@ -9,13 +9,8 @@ final class CoursesCounterIdMother { - public static function create(string $value): CoursesCounterId + public static function create(?string $value = null): CoursesCounterId { - return new CoursesCounterId($value); - } - - public static function random(): CoursesCounterId - { - return self::create(UuidMother::random()); + return new CoursesCounterId($value ?? UuidMother::create()); } } diff --git a/tests/Mooc/CoursesCounter/Domain/CoursesCounterIncrementedDomainEventMother.php b/tests/Mooc/CoursesCounter/Domain/CoursesCounterIncrementedDomainEventMother.php index 8a3c60bba..f3fc1e4d3 100644 --- a/tests/Mooc/CoursesCounter/Domain/CoursesCounterIncrementedDomainEventMother.php +++ b/tests/Mooc/CoursesCounter/Domain/CoursesCounterIncrementedDomainEventMother.php @@ -12,19 +12,17 @@ final class CoursesCounterIncrementedDomainEventMother { public static function create( - CoursesCounterId $id, - CoursesCounterTotal $total + ?CoursesCounterId $id = null, + ?CoursesCounterTotal $total = null ): CoursesCounterIncrementedDomainEvent { - return new CoursesCounterIncrementedDomainEvent($id->value(), $total->value()); + return new CoursesCounterIncrementedDomainEvent( + $id?->value() ?? CoursesCounterIdMother::create()->value(), + $total?->value() ?? CoursesCounterTotalMother::create()->value() + ); } public static function fromCounter(CoursesCounter $counter): CoursesCounterIncrementedDomainEvent { return self::create($counter->id(), $counter->total()); } - - public static function random(): CoursesCounterIncrementedDomainEvent - { - return self::create(CoursesCounterIdMother::random(), CoursesCounterTotalMother::random()); - } } diff --git a/tests/Mooc/CoursesCounter/Domain/CoursesCounterMother.php b/tests/Mooc/CoursesCounter/Domain/CoursesCounterMother.php index b042dcb96..d05637331 100644 --- a/tests/Mooc/CoursesCounter/Domain/CoursesCounterMother.php +++ b/tests/Mooc/CoursesCounter/Domain/CoursesCounterMother.php @@ -14,16 +14,20 @@ final class CoursesCounterMother { public static function create( - CoursesCounterId $id, - CoursesCounterTotal $total, + ?CoursesCounterId $id = null, + ?CoursesCounterTotal $total = null, CourseId ...$existingCourses ): CoursesCounter { - return new CoursesCounter($id, $total, ...$existingCourses); + return new CoursesCounter( + $id ?? CoursesCounterIdMother::create(), + $total ?? CoursesCounterTotalMother::create(), + ...count($existingCourses) ? $existingCourses : Repeater::random(fn() => CourseIdMother::create()) + ); } public static function withOne(CourseId $courseId): CoursesCounter { - return self::create(CoursesCounterIdMother::random(), CoursesCounterTotalMother::one(), $courseId); + return self::create(CoursesCounterIdMother::create(), CoursesCounterTotalMother::one(), $courseId); } public static function incrementing(CoursesCounter $existingCounter, CourseId $courseId): CoursesCounter @@ -34,13 +38,4 @@ public static function incrementing(CoursesCounter $existingCounter, CourseId $c ...array_merge($existingCounter->existingCourses(), [$courseId]) ); } - - public static function random(): CoursesCounter - { - return self::create( - CoursesCounterIdMother::random(), - CoursesCounterTotalMother::random(), - ...Repeater::random(CourseIdMother::creator()) - ); - } } diff --git a/tests/Mooc/CoursesCounter/Domain/CoursesCounterTotalMother.php b/tests/Mooc/CoursesCounter/Domain/CoursesCounterTotalMother.php index f151d5855..49115ea90 100644 --- a/tests/Mooc/CoursesCounter/Domain/CoursesCounterTotalMother.php +++ b/tests/Mooc/CoursesCounter/Domain/CoursesCounterTotalMother.php @@ -9,9 +9,9 @@ final class CoursesCounterTotalMother { - public static function create(int $value): CoursesCounterTotal + public static function create(?int $value = null): CoursesCounterTotal { - return new CoursesCounterTotal($value); + return new CoursesCounterTotal($value ?? IntegerMother::create()); } public static function one(): CoursesCounterTotal @@ -21,6 +21,6 @@ public static function one(): CoursesCounterTotal public static function random(): CoursesCounterTotal { - return self::create(IntegerMother::random()); + return self::create(IntegerMother::create()); } } diff --git a/tests/Shared/Domain/Criteria/FilterFieldMother.php b/tests/Shared/Domain/Criteria/FilterFieldMother.php index 90403d21f..01f35645e 100644 --- a/tests/Shared/Domain/Criteria/FilterFieldMother.php +++ b/tests/Shared/Domain/Criteria/FilterFieldMother.php @@ -9,13 +9,8 @@ final class FilterFieldMother { - public static function create(string $fieldName): FilterField + public static function create(?string $fieldName = null): FilterField { - return new FilterField($fieldName); - } - - public static function random(): FilterField - { - return self::create(WordMother::random()); + return new FilterField($fieldName ?? WordMother::create()); } } diff --git a/tests/Shared/Domain/Criteria/FilterMother.php b/tests/Shared/Domain/Criteria/FilterMother.php index 22d687383..9f3a6fcc5 100644 --- a/tests/Shared/Domain/Criteria/FilterMother.php +++ b/tests/Shared/Domain/Criteria/FilterMother.php @@ -11,9 +11,16 @@ final class FilterMother { - public static function create(FilterField $field, FilterOperator $operator, FilterValue $value): Filter - { - return new Filter($field, $operator, $value); + public static function create( + ?FilterField $field = null, + ?FilterOperator $operator = null, + ?FilterValue $value = null + ): Filter { + return new Filter( + $field ?? FilterFieldMother::create(), + $operator ?? FilterOperator::random(), + $value ?? FilterValueMother::create() + ); } /** @param string[] $values */ @@ -21,9 +28,4 @@ public static function fromValues(array $values): Filter { return Filter::fromValues($values); } - - public static function random(): Filter - { - return self::create(FilterFieldMother::random(), FilterOperator::random(), FilterValueMother::random()); - } } diff --git a/tests/Shared/Domain/Criteria/FilterValueMother.php b/tests/Shared/Domain/Criteria/FilterValueMother.php index 701f5a86b..aa78e74a0 100644 --- a/tests/Shared/Domain/Criteria/FilterValueMother.php +++ b/tests/Shared/Domain/Criteria/FilterValueMother.php @@ -9,13 +9,8 @@ final class FilterValueMother { - public static function create(string $value): FilterValue + public static function create(?string $value = null): FilterValue { - return new FilterValue($value); - } - - public static function random(): FilterValue - { - return self::create(WordMother::random()); + return new FilterValue($value ?? WordMother::create()); } } diff --git a/tests/Shared/Domain/Criteria/FiltersMother.php b/tests/Shared/Domain/Criteria/FiltersMother.php index 8d4b6de4e..fe0009f5f 100644 --- a/tests/Shared/Domain/Criteria/FiltersMother.php +++ b/tests/Shared/Domain/Criteria/FiltersMother.php @@ -24,9 +24,4 @@ public static function blank(): Filters { return self::create([]); } - - public static function random(): Filters - { - return self::create([FilterMother::random()]); - } } diff --git a/tests/Shared/Domain/Criteria/OrderByMother.php b/tests/Shared/Domain/Criteria/OrderByMother.php index 552e72b3f..457e35bf6 100644 --- a/tests/Shared/Domain/Criteria/OrderByMother.php +++ b/tests/Shared/Domain/Criteria/OrderByMother.php @@ -9,13 +9,8 @@ final class OrderByMother { - public static function create(string $fieldName): OrderBy + public static function create(?string $fieldName = null): OrderBy { - return new OrderBy($fieldName); - } - - public static function random(): OrderBy - { - return self::create(WordMother::random()); + return new OrderBy($fieldName ?? WordMother::create()); } } diff --git a/tests/Shared/Domain/Criteria/OrderMother.php b/tests/Shared/Domain/Criteria/OrderMother.php index 0b1f94a27..10e791440 100644 --- a/tests/Shared/Domain/Criteria/OrderMother.php +++ b/tests/Shared/Domain/Criteria/OrderMother.php @@ -10,18 +10,13 @@ final class OrderMother { - public static function create(OrderBy $orderBy, OrderType $orderType): Order + public static function create(?OrderBy $orderBy = null, ?OrderType $orderType = null): Order { - return new Order($orderBy, $orderType); + return new Order($orderBy ?? OrderByMother::create(), $orderType ?? OrderType::random()); } public static function none(): Order { return Order::none(); } - - public static function random(): Order - { - return self::create(OrderByMother::random(), OrderType::random()); - } } diff --git a/tests/Shared/Domain/IntegerMother.php b/tests/Shared/Domain/IntegerMother.php index faa83a9b6..ac6259f3b 100644 --- a/tests/Shared/Domain/IntegerMother.php +++ b/tests/Shared/Domain/IntegerMother.php @@ -6,6 +6,11 @@ final class IntegerMother { + public static function create(): int + { + return self::between(1); + } + public static function between(int $min, $max = PHP_INT_MAX): int { return MotherCreator::random()->numberBetween($min, $max); @@ -15,9 +20,4 @@ public static function lessThan(int $max): int { return self::between(1, $max); } - - public static function random(): int - { - return self::between(1); - } } diff --git a/tests/Shared/Domain/MotherCreator.php b/tests/Shared/Domain/MotherCreator.php index 26c4228fc..41eab9050 100644 --- a/tests/Shared/Domain/MotherCreator.php +++ b/tests/Shared/Domain/MotherCreator.php @@ -9,10 +9,10 @@ final class MotherCreator { - private static $faker; + private static ?Generator $faker; public static function random(): Generator { - return self::$faker = self::$faker ?: Factory::create(); + return self::$faker = self::$faker ?? Factory::create(); } } diff --git a/tests/Shared/Domain/UuidMother.php b/tests/Shared/Domain/UuidMother.php index 3ec353506..524e2f72e 100644 --- a/tests/Shared/Domain/UuidMother.php +++ b/tests/Shared/Domain/UuidMother.php @@ -6,7 +6,7 @@ final class UuidMother { - public static function random(): string + public static function create(): string { return MotherCreator::random()->unique()->uuid; } diff --git a/tests/Shared/Domain/WordMother.php b/tests/Shared/Domain/WordMother.php index 5814eb85c..424fe1774 100644 --- a/tests/Shared/Domain/WordMother.php +++ b/tests/Shared/Domain/WordMother.php @@ -6,7 +6,7 @@ final class WordMother { - public static function random(): string + public static function create(): string { return MotherCreator::random()->word; } diff --git a/tests/Shared/Infrastructure/Bus/Event/MySql/MySqlDoctrineEventBusTest.php b/tests/Shared/Infrastructure/Bus/Event/MySql/MySqlDoctrineEventBusTest.php index a8721d14a..5cf3ff4ab 100644 --- a/tests/Shared/Infrastructure/Bus/Event/MySql/MySqlDoctrineEventBusTest.php +++ b/tests/Shared/Infrastructure/Bus/Event/MySql/MySqlDoctrineEventBusTest.php @@ -33,8 +33,8 @@ protected function setUp(): void /** @test */ public function it_should_publish_and_consume_domain_events_from_msql(): void { - $domainEvent = CourseCreatedDomainEventMother::random(); - $anotherDomainEvent = CoursesCounterIncrementedDomainEventMother::random(); + $domainEvent = CourseCreatedDomainEventMother::create(); + $anotherDomainEvent = CoursesCounterIncrementedDomainEventMother::create(); $this->bus->publish($domainEvent, $anotherDomainEvent); diff --git a/tests/Shared/Infrastructure/Bus/Event/RabbitMq/RabbitMqEventBusTest.php b/tests/Shared/Infrastructure/Bus/Event/RabbitMq/RabbitMqEventBusTest.php index a8cab73b1..0934f42bc 100644 --- a/tests/Shared/Infrastructure/Bus/Event/RabbitMq/RabbitMqEventBusTest.php +++ b/tests/Shared/Infrastructure/Bus/Event/RabbitMq/RabbitMqEventBusTest.php @@ -57,7 +57,7 @@ protected function setUp(): void /** @test */ public function it_should_publish_and_consume_domain_events_from_rabbitmq(): void { - $domainEvent = CourseCreatedDomainEventMother::random(); + $domainEvent = CourseCreatedDomainEventMother::create(); $this->configurer->configure($this->exchangeName, $this->fakeSubscriber); @@ -76,7 +76,7 @@ public function it_should_throw_an_exception_consuming_non_existing_domain_event { $this->expectException(RuntimeException::class); - $domainEvent = CoursesCounterIncrementedDomainEventMother::random(); + $domainEvent = CoursesCounterIncrementedDomainEventMother::create(); $this->configurer->configure($this->exchangeName, $this->fakeSubscriber); @@ -93,7 +93,7 @@ public function it_should_throw_an_exception_consuming_non_existing_domain_event /** @test */ public function it_should_retry_failed_domain_events(): void { - $domainEvent = CourseCreatedDomainEventMother::random(); + $domainEvent = CourseCreatedDomainEventMother::create(); $this->configurer->configure($this->exchangeName, $this->fakeSubscriber); @@ -114,7 +114,7 @@ public function it_should_retry_failed_domain_events(): void /** @test */ public function it_should_send_events_to_dead_letter_after_retry_failed_domain_events(): void { - $domainEvent = CourseCreatedDomainEventMother::random(); + $domainEvent = CourseCreatedDomainEventMother::create(); $this->configurer->configure($this->exchangeName, $this->fakeSubscriber);