diff --git a/src/RequirementChecker/AppRequirementsFactory.php b/src/RequirementChecker/AppRequirementsFactory.php index f978fb67d..cc2a17318 100644 --- a/src/RequirementChecker/AppRequirementsFactory.php +++ b/src/RequirementChecker/AppRequirementsFactory.php @@ -33,14 +33,11 @@ final class AppRequirementsFactory { private const SELF_PACKAGE = '__APPLICATION__'; - /** - * @return list Configured requirements - */ public static function create( DecodedComposerJson $composerJson, DecodedComposerLock $composerLock, CompressionAlgorithm $compressionAlgorithm, - ): array { + ): Requirements { return self::configureExtensionRequirements( self::retrievePhpVersionRequirements($composerJson, $composerLock), $composerJson, @@ -101,7 +98,7 @@ private static function configureExtensionRequirements( DecodedComposerJson $composerJson, DecodedComposerLock $composerLock, CompressionAlgorithm $compressionAlgorithm, - ): array { + ): Requirements { [$extensionRequirements, $extensionConflicts] = self::collectExtensionRequirements( $composerJson, $composerLock, @@ -126,7 +123,7 @@ private static function configureExtensionRequirements( } } - return $requirements; + return new Requirements($requirements); } /** diff --git a/src/RequirementChecker/Requirements.php b/src/RequirementChecker/Requirements.php index 13f9af47a..d59af516d 100644 --- a/src/RequirementChecker/Requirements.php +++ b/src/RequirementChecker/Requirements.php @@ -18,6 +18,8 @@ use Countable; use IteratorAggregate; use Traversable; +use function array_map; +use function array_values; use function count; final readonly class Requirements implements Countable, IteratorAggregate @@ -38,4 +40,14 @@ public function count(): int { return count($this->requirements); } + + public function toArray(): array + { + return array_values( + array_map( + static fn (Requirement $requirement) => $requirement->toArray(), + $this->requirements, + ), + ); + } } diff --git a/src/RequirementChecker/RequirementsDumper.php b/src/RequirementChecker/RequirementsDumper.php index fc396002b..f8b1700b3 100644 --- a/src/RequirementChecker/RequirementsDumper.php +++ b/src/RequirementChecker/RequirementsDumper.php @@ -21,7 +21,6 @@ use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\SplFileInfo; use Webmozart\Assert\Assert; -use function array_map; use function str_replace; use function var_export; @@ -76,20 +75,17 @@ private static function dumpRequirementsConfig( DecodedComposerLock $composerLock, CompressionAlgorithm $compressionAlgorithm, ): array { - $requirements = array_map( - static fn (Requirement $requirement) => $requirement->toArray(), - AppRequirementsFactory::create( - $composerJson, - $composerLock, - $compressionAlgorithm, - ), + $requirements = AppRequirementsFactory::create( + $composerJson, + $composerLock, + $compressionAlgorithm, ); return [ '.requirements.php', str_replace( '\'__CONFIG__\'', - var_export($requirements, true), + var_export($requirements->toArray(), true), self::REQUIREMENTS_CONFIG_TEMPLATE, ), ]; diff --git a/tests/RequirementChecker/AppRequirementsFactoryTest.php b/tests/RequirementChecker/AppRequirementsFactoryTest.php index 33210260b..b8b47b6de 100644 --- a/tests/RequirementChecker/AppRequirementsFactoryTest.php +++ b/tests/RequirementChecker/AppRequirementsFactoryTest.php @@ -42,7 +42,10 @@ public function test_it_can_generate_and_serialized_requirements_from_a_composer $compressionAlgorithm, ); - self::assertEquals($expected, $actual); + self::assertEquals( + (new Requirements($expected))->toArray(), + $actual->toArray(), + ); } public static function lockContentsProvider(): iterable diff --git a/tests/RequirementChecker/RequirementsTest.php b/tests/RequirementChecker/RequirementsTest.php new file mode 100644 index 000000000..5880db2d8 --- /dev/null +++ b/tests/RequirementChecker/RequirementsTest.php @@ -0,0 +1,90 @@ + + * Théo Fidry + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace KevinGH\Box\RequirementChecker; + +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\TestCase; + +/** + * @internal + */ +#[CoversClass(Requirements::class)] +final class RequirementsTest extends TestCase +{ + #[DataProvider('requirementsProvider')] + public function test_it_can_be_cast_into_an_array( + Requirements $requirements, + array $expected, + ): void { + $actual = $requirements->toArray(); + + self::assertSame($expected, $actual); + } + + public static function requirementsProvider(): iterable + { + yield 'empty' => [ + new Requirements([]), + [], + ]; + + yield 'nominal' => [ + new Requirements([ + Requirement::forPHP('7.2', null), + Requirement::forRequiredExtension('http', null), + ]), + [ + [ + 'type' => 'php', + 'condition' => '7.2', + 'source' => null, + 'message' => 'This application requires a PHP version matching "7.2".', + 'helpMessage' => 'This application requires a PHP version matching "7.2".', + ], + [ + 'type' => 'extension', + 'condition' => 'http', + 'source' => null, + 'message' => 'This application requires the extension "http".', + 'helpMessage' => 'This application requires the extension "http". You either need to enable it or request the application to be shipped with a polyfill for this extension.', + ], + ], + ]; + + yield 'non list' => [ + new Requirements([ + 10 => Requirement::forPHP('7.2', null), + 20 => Requirement::forRequiredExtension('http', null), + ]), + [ + [ + 'type' => 'php', + 'condition' => '7.2', + 'source' => null, + 'message' => 'This application requires a PHP version matching "7.2".', + 'helpMessage' => 'This application requires a PHP version matching "7.2".', + ], + [ + 'type' => 'extension', + 'condition' => 'http', + 'source' => null, + 'message' => 'This application requires the extension "http".', + 'helpMessage' => 'This application requires the extension "http". You either need to enable it or request the application to be shipped with a polyfill for this extension.', + ], + ], + ]; + } +}