Skip to content

Commit

Permalink
Allow profiles to be provided through CSV (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
cspray authored Nov 10, 2024
1 parent de7b314 commit 6a799ef
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 21 deletions.
54 changes: 37 additions & 17 deletions src/Profiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
));
}

/**
Expand Down
56 changes: 52 additions & 4 deletions test/Unit/ProfilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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());
}
}

0 comments on commit 6a799ef

Please sign in to comment.