Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow profiles to be provided through CSV #399

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions src/Profiles.php
Original file line number Diff line number Diff line change
@@ -6,38 +6,58 @@

final class Profiles {

private function __construct(
/**
* @var non-empty-list<non-empty-string> $profiles
*/
private readonly array $profiles
) {
}

public static function defaultOnly() : self {
return new self(['default']);
}
/**
* @var non-empty-list<non-empty-string> $profiles
*/
private readonly array $profiles;

/**
* @param list<string> $profiles
* @return self
* @throws InvalidProfiles
*/
public static function fromList(array $profiles) : self {
private function __construct(array $profiles) {
if ($profiles === []) {
throw InvalidProfiles::fromEmptyProfilesList();
}

$clean = [];

foreach ($profiles as $profile) {
if ($profile === '') {
throw InvalidProfiles::fromEmptyProfile();
}

$clean[] = $profile;
}
return new self($clean);

$this->profiles = $clean;
}

public static function defaultOnly() : self {
return new self(['default']);
}

/**
* @param list<string> $profiles
* @return self
* @throws InvalidProfiles
*/
public static function fromList(array $profiles) : self {
return new self($profiles);
}

public static function fromCommaDelimitedString(string $profiles) : self {
return self::fromDelimitedString($profiles, ',');
}

/**
* @param string $profilesString
* @param non-empty-string $delimiter
* @return self
* @throws InvalidProfiles
*/
public static function fromDelimitedString(string $profilesString, string $delimiter) : self {
return new self(array_map(
static fn(string $profile) => trim($profile),
explode($delimiter, $profilesString)
));
}

/**
56 changes: 52 additions & 4 deletions test/Unit/ProfilesTest.php
Original file line number Diff line number Diff line change
@@ -4,7 +4,9 @@

use Cspray\AnnotatedContainer\Exception\InvalidProfiles;
use Cspray\AnnotatedContainer\Profiles;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Closure;

class ProfilesTest extends TestCase {

@@ -55,17 +57,63 @@ public function testIsAnyActiveReturnsFalseIfNoProfileIsListed() : void {
self::assertFalse($subject->isAnyActive($actual));
}

public function testPassEmptyListToProfilesFromListThrowsException() : void {
public static function emptyProfilesProvider() : array {
return [
'fromList' => [static fn() => Profiles::fromList([])],
];
}

#[DataProvider('emptyProfilesProvider')]
public function testPassEmptyListToProfilesFromListThrowsException(Closure $closure) : void {
$this->expectException(InvalidProfiles::class);
$this->expectExceptionMessage('A non-empty list of non-empty strings MUST be provided for Profiles.');

Profiles::fromList([]);
$closure();
}

public function testPassEmptyProfileToProfilesFromListThrowsException() : void {
public static function emptyProfileProvider() : array {
return [
'fromList' => [static fn() => Profiles::fromList([''])],
'fromDelimitedString' => [static fn() => Profiles::fromDelimitedString('', ',')],
];
}

#[DataProvider('emptyProfileProvider')]
public function testPassEmptyProfileToProfilesFromListThrowsException(Closure $closure) : void {
$this->expectException(InvalidProfiles::class);
$this->expectExceptionMessage('All profiles MUST be non-empty strings.');

Profiles::fromList(['']);
$closure();
}

public static function delimitedStringProvider() : array {
return [
['foo,bar,baz', ',', ['foo', 'bar', 'baz']],
['erykah|badu|on|on', '|', ['erykah', 'badu', 'on', 'on']],
['harry/mack/goat', '/', ['harry', 'mack', 'goat']],
[' check ; for ; trailing ; leading ; spaces', ';', ['check', 'for', 'trailing', 'leading', 'spaces']],
];
}

#[DataProvider('delimitedStringProvider')]
public function testDelimitedStringParsedCorrectly(string $profiles, string $delimiter, array $expected) : void {
$profiles = Profiles::fromDelimitedString($profiles, $delimiter);

self::assertSame($expected, $profiles->toArray());
}

public static function commaDelimitedStringProvider() : array {
return [
['foo,bar,baz', ['foo', 'bar', 'baz']],
[' not , worth , it ', ['not', 'worth', 'it']],
['some|non|comma|delimiter', ['some|non|comma|delimiter']],
];
}

#[DataProvider('commaDelimitedStringProvider')]
public function testCommaDelimitedStringParsedCorrectly(string $profiles, array $expected) : void {
$profiles = Profiles::fromCommaDelimitedString($profiles);

self::assertSame($expected, $profiles->toArray());
}
}
Loading