Skip to content

Commit

Permalink
refactor: Leverage the Requirements value object for the requirements
Browse files Browse the repository at this point in the history
factory
  • Loading branch information
theofidry committed Dec 13, 2023
1 parent 1a72f9f commit 8635aab
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 16 deletions.
9 changes: 3 additions & 6 deletions src/RequirementChecker/AppRequirementsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@ final class AppRequirementsFactory
{
private const SELF_PACKAGE = '__APPLICATION__';

/**
* @return list<Requirement> Configured requirements
*/
public static function create(
DecodedComposerJson $composerJson,
DecodedComposerLock $composerLock,
CompressionAlgorithm $compressionAlgorithm,
): array {
): Requirements {
return self::configureExtensionRequirements(
self::retrievePhpVersionRequirements($composerJson, $composerLock),
$composerJson,
Expand Down Expand Up @@ -101,7 +98,7 @@ private static function configureExtensionRequirements(
DecodedComposerJson $composerJson,
DecodedComposerLock $composerLock,
CompressionAlgorithm $compressionAlgorithm,
): array {
): Requirements {
[$extensionRequirements, $extensionConflicts] = self::collectExtensionRequirements(
$composerJson,
$composerLock,
Expand All @@ -126,7 +123,7 @@ private static function configureExtensionRequirements(
}
}

return $requirements;
return new Requirements($requirements);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/RequirementChecker/Requirements.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
),
);
}
}
14 changes: 5 additions & 9 deletions src/RequirementChecker/RequirementsDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
),
];
Expand Down
5 changes: 4 additions & 1 deletion tests/RequirementChecker/AppRequirementsFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
90 changes: 90 additions & 0 deletions tests/RequirementChecker/RequirementsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <[email protected]>
* Théo Fidry <[email protected]>
*
* 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.',
],
],
];
}
}

0 comments on commit 8635aab

Please sign in to comment.