Skip to content

Commit

Permalink
refactor: Make it possible to create a Requirement back from its array
Browse files Browse the repository at this point in the history
form
  • Loading branch information
theofidry committed Oct 22, 2023
1 parent 0e218f2 commit c43cc3d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
21 changes: 16 additions & 5 deletions src/RequirementChecker/Requirement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}

Expand Down Expand Up @@ -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 [
Expand Down
37 changes: 31 additions & 6 deletions tests/RequirementChecker/RequirementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
}
}

0 comments on commit c43cc3d

Please sign in to comment.