Skip to content

Commit

Permalink
AssetFactory // fix method call for Asset::withDependencies() to use …
Browse files Browse the repository at this point in the history
…spread operator on array input.
  • Loading branch information
Chrico committed Mar 25, 2021
1 parent 6303cd3 commit 5a67ecb
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
8 changes: 7 additions & 1 deletion src/AssetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public static function create(array $config): Asset
$propertiesToMethod = [
'filePath' => 'withFilePath',
'version' => 'withVersion',
'dependencies' => 'withDependencies',
'location' => 'forLocation',
'enqueue' => 'canEnqueue',
'handler' => 'useHandler',
Expand Down Expand Up @@ -89,6 +88,13 @@ public static function create(array $config): Asset
$asset->{$methodName}($config[$key]);
}

$dependencies = $config['dependencies'] ?? null;
if (is_array($dependencies)) {
$asset->withDependencies(...$dependencies);
} elseif (is_scalar($dependencies)) {
$asset->withDependencies((string) $dependencies);
}

return $asset;
}

Expand Down
68 changes: 58 additions & 10 deletions tests/phpunit/Unit/AssetFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ public function testBasic(): void
$expectedType = Script::class;
$expectedUrl = 'foo.js';

$factory = AssetFactory::create(
$asset = AssetFactory::create(
[
'handle' => $expectedHandle,
'url' => $expectedUrl,
'type' => $expectedType,
]
);

static::assertInstanceOf(Script::class, $factory);
static::assertInstanceOf($expectedType, $factory);
static::assertSame($expectedUrl, $factory->url());
static::assertSame($expectedHandle, $factory->handle());
static::assertSame(Asset::FRONTEND, $factory->location());
static::assertInstanceOf(Script::class, $asset);
static::assertInstanceOf($expectedType, $asset);
static::assertSame($expectedUrl, $asset->url());
static::assertSame($expectedHandle, $asset->handle());
static::assertSame(Asset::FRONTEND, $asset->location());
}

/**
Expand All @@ -69,7 +69,7 @@ public function testCreateLocation(): void
{
$expectedLocation = Asset::BACKEND;

$factory = AssetFactory::create(
$asset = AssetFactory::create(
[
'handle' => 'foo',
'location' => $expectedLocation,
Expand All @@ -78,7 +78,55 @@ public function testCreateLocation(): void
]
);

static::assertSame($expectedLocation, $factory->location());
static::assertSame($expectedLocation, $asset->location());
}

/**
* @param mixed $input
* @param array $expected
*
* @test
*
* @dataProvider provideDependencies
*/
public function testDependencies($input, array $expected): void
{
$asset = AssetFactory::create(
[
'handle' => 'foo',
'url' => 'foo.js',
'type' => Script::class,
'dependencies' => $input
]
);

static::assertSame($expected, $asset->dependencies());
}

/**
* @see testDependencies
*/
public function provideDependencies(): \Generator
{
yield "string" => [
'dependency-1',
['dependency-1'],
];

yield "int" => [
1,
["1"],
];

yield "multiple dependencies" => [
['dependency-1', 'dependency-2', 'dependency-3'],
['dependency-1', 'dependency-2', 'dependency-3'],
];

yield "non scalar - bool" => [
new \stdClass(),
[],
];
}

/**
Expand All @@ -87,7 +135,7 @@ public function testCreateLocation(): void
public function testCreateMultipleLocations(): void
{
$expected = Asset::FRONTEND | Asset::BACKEND | Asset::CUSTOMIZER;
$factory = AssetFactory::create(
$asset = AssetFactory::create(
[
'handle' => 'foo',
'url' => 'foo.css',
Expand All @@ -96,7 +144,7 @@ public function testCreateMultipleLocations(): void
]
);

static::assertSame($expected, $factory->location());
static::assertSame($expected, $asset->location());
}

/**
Expand Down

0 comments on commit 5a67ecb

Please sign in to comment.