diff --git a/src/RequirementChecker/Requirement.php b/src/RequirementChecker/Requirement.php index 529c85153..137c42f28 100644 --- a/src/RequirementChecker/Requirement.php +++ b/src/RequirementChecker/Requirement.php @@ -20,11 +20,11 @@ final class Requirement { public function __construct( - private readonly RequirementType $type, - private readonly string $condition, - private readonly ?string $source, - private readonly string $message, - private readonly string $helpMessage, + public readonly RequirementType $type, + public readonly string $condition, + public readonly ?string $source, + public readonly string $message, + public readonly string $helpMessage, ) { } @@ -115,6 +115,17 @@ public static function forConflictingExtension(string $extension, ?string $packa ); } + public static function fromArray(array $value): self + { + return new self( + RequirementType::from($value['type']), + $value['condition'], + $value['source'], + $value['message'], + $value['helpMessage'], + ); + } + public function toArray(): array { return [ diff --git a/tests/RequirementChecker/RequirementTest.php b/tests/RequirementChecker/RequirementTest.php index 508f1129b..c4948e227 100644 --- a/tests/RequirementChecker/RequirementTest.php +++ b/tests/RequirementChecker/RequirementTest.php @@ -35,7 +35,10 @@ public function test_it_can_be_created_for_a_php_version(): void 'helpMessage' => 'The application requires a version matching "^8.2".', ]; - self::assertSame($expected, $requirement->toArray()); + $actual = $requirement->toArray(); + + self::assertSame($expected, $actual); + self::assertItCanBeCreatedFromItsArrayForm($requirement, $actual); } public function test_it_can_be_created_for_a_php_version_for_a_package(): void @@ -50,7 +53,10 @@ public function test_it_can_be_created_for_a_php_version_for_a_package(): void 'helpMessage' => 'The package "box/test" requires a version matching "^8.2".', ]; - self::assertSame($expected, $requirement->toArray()); + $actual = $requirement->toArray(); + + self::assertSame($expected, $actual); + self::assertItCanBeCreatedFromItsArrayForm($requirement, $actual); } public function test_it_can_be_created_for_an_extension_constraint(): void @@ -65,7 +71,10 @@ public function test_it_can_be_created_for_an_extension_constraint(): void 'helpMessage' => 'The application requires the extension "mbstring". You either need to enable it or request the application to be shipped with a polyfill for this extension.', ]; - self::assertSame($expected, $requirement->toArray()); + $actual = $requirement->toArray(); + + self::assertSame($expected, $actual); + self::assertItCanBeCreatedFromItsArrayForm($requirement, $actual); } public function test_it_can_be_created_for_an_extension_constraint_for_a_package(): void @@ -80,7 +89,10 @@ public function test_it_can_be_created_for_an_extension_constraint_for_a_package 'helpMessage' => 'The package "box/test" requires the extension "mbstring". You either need to enable it or request the application to be shipped with a polyfill for this extension.', ]; - self::assertSame($expected, $requirement->toArray()); + $actual = $requirement->toArray(); + + self::assertSame($expected, $actual); + self::assertItCanBeCreatedFromItsArrayForm($requirement, $actual); } public function test_it_can_be_created_for_a_conflicting_extension_constraint(): void @@ -95,7 +107,10 @@ public function test_it_can_be_created_for_a_conflicting_extension_constraint(): 'helpMessage' => 'The application conflicts with the extension "mbstring". You need to disable it in order to run this application.', ]; - self::assertSame($expected, $requirement->toArray()); + $actual = $requirement->toArray(); + + self::assertSame($expected, $actual); + self::assertItCanBeCreatedFromItsArrayForm($requirement, $actual); } public function test_it_can_be_created_for_a_conflicting_extension_constraint_for_a_package(): void @@ -110,6 +125,16 @@ public function test_it_can_be_created_for_a_conflicting_extension_constraint_fo 'helpMessage' => 'The package "box/test" conflicts with the extension "mbstring". You need to disable it in order to run this application.', ]; - self::assertSame($expected, $requirement->toArray()); + $actual = $requirement->toArray(); + + self::assertSame($expected, $actual); + self::assertItCanBeCreatedFromItsArrayForm($requirement, $actual); + } + + private static function assertItCanBeCreatedFromItsArrayForm(Requirement $expected, array $arrayForm): void + { + $actual = Requirement::fromArray($arrayForm); + + self::assertEquals($expected, $actual); } }