Skip to content

Commit

Permalink
Merge pull request #27 from inpsyde/issue-26-restore-bc
Browse files Browse the repository at this point in the history
Issue 26 restore bc
  • Loading branch information
Chrico authored Apr 1, 2021
2 parents ff63f34 + 1b76320 commit 4af9e2b
Show file tree
Hide file tree
Showing 2 changed files with 265 additions and 6 deletions.
96 changes: 92 additions & 4 deletions src/AssetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Inpsyde\Assets;

use Inpsyde\Assets\Exception\InvalidArgumentException;
use Inpsyde\Assets\Loader\PhpFileLoader;
use Inpsyde\Assets\Loader\ArrayLoader;
use Inpsyde\Assets\Asset;
Expand All @@ -37,7 +38,7 @@ final class AssetFactory
*/
public static function create(array $config): Asset
{
self::validateConfig($config);
$config = self::validateConfig($config);

$location = $config['location'] ?? Asset::FRONTEND;
$handle = $config['handle'];
Expand Down Expand Up @@ -68,7 +69,17 @@ public static function create(array $config): Asset

if ($class === Script::class) {
/** @var Script $asset */
$propertiesToMethod['translation'] = 'withTranslation';

foreach ($config['localize'] as $objectName => $data) {
$asset->withLocalize($objectName, $data);
}

if (isset($config['translation'])) {
$asset->withTranslation(
$config['translation']['domain'],
$config['translation']['path']
);
}

$inFooter = $config['inFooter'] ?? true;
$inFooter
Expand Down Expand Up @@ -114,9 +125,21 @@ public static function create(array $config): Asset
/**
* @param array $config
*
* @return array
*
* @throws Exception\MissingArgumentException
*/
private static function validateConfig(array $config)
private static function validateConfig(array $config): array
{
self::ensureRequiredConfigFields($config);
$config = self::normalizeVersionConfig($config);
$config = self::normalizeTranslationConfig($config);
$config = self::normalizeLocalizeConfig($config);

return $config;
}

private static function ensureRequiredConfigFields(array $config): void
{
$requiredFields = [
'type',
Expand All @@ -125,7 +148,7 @@ private static function validateConfig(array $config)
];

foreach ($requiredFields as $key) {
if (!isset($config[$key])) {
if (! isset($config[$key])) {
throw new Exception\MissingArgumentException(
sprintf(
'The given config <code>%s</code> is missing.',
Expand All @@ -136,6 +159,71 @@ private static function validateConfig(array $config)
}
}

private static function normalizeVersionConfig(array $config): array
{
// some existing configurations uses time() as version parameter which leads to
// fatal errors since 2.5
if (isset($config['version'])) {
$config['version'] = (string)$config['version'];
}

return $config;
}

private static function normalizeTranslationConfig(array $config): array
{
if (! isset($config['translation'])) {
return $config;
}

if (is_string($config['translation'])) {
// backward compatibility
$config['translation'] = [
'domain' => (string)$config['translation'],
'path' => null,
];

return $config;
}

if (! is_array($config['translation'])) {
throw new InvalidArgumentException(
"Config key <code>translation</code> must be of type string or array"
);
}

if (! isset($config['translation']['domain'])) {
throw new Exception\MissingArgumentException(
'Config key <code>translation[domain]</code> is missing.'
);
}

if (! isset($config['translation']['path'])) {
$config['translation']['path'] = null;
}

return $config;
}

private static function normalizeLocalizeConfig(array $config): array
{
if (!isset($config['localize'])) {
$config['localize'] = [];

return $config;
}
if (is_callable($config['localize'])) {
$config['localize'] = $config['localize']();
}
if (!is_array($config['localize'])) {
throw new InvalidArgumentException(
'Config key <code>localize</code> must evaluate as an array'
);
}

return $config;
}

/**
* @param string $file
*
Expand Down
175 changes: 173 additions & 2 deletions tests/phpunit/Unit/AssetFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ public function testCreateMultipleLocations(): void
* @test
* @dataProvider provideInvalidConfig
*/
public function testInvalidConfig(array $config): void
public function testInvalidConfig(array $config, string $expectedExceptionType): void
{
$this->expectException(MissingArgumentException::class);
$this->expectException($expectedExceptionType);

AssetFactory::create($config);
}
Expand Down Expand Up @@ -277,13 +277,184 @@ public function provideInvalidConfig(): \Generator
'handle' => 'foo',
'url' => 'foo.css',
],
MissingArgumentException::class,
];

yield 'missing url' => [
[
'handle' => 'foo',
'location' => Asset::FRONTEND,
],
MissingArgumentException::class,
];

yield 'missing translation.domain' => [
[
'handle' => 'foo',
'location' => Asset::FRONTEND,
'url' => 'foo.css',
'type' => Script::class,
'translation' => [
'no-domain' => 'fail!',
],
],
MissingArgumentException::class,
];

yield 'invalid translation type' => [
[
'handle' => 'foo',
'location' => Asset::FRONTEND,
'url' => 'foo.css',
'type' => Script::class,
'translation' => 3.1415,
],
InvalidArgumentException::class,
];

yield 'invalid localization' => [
[
'handle' => 'foo',
'location' => Asset::FRONTEND,
'url' => 'foo.js',
'type' => Script::class,
'localize' => static function () {

return 'thisShouldBeAnArray';
},
],
InvalidArgumentException::class,
];
}

/**
* @dataProvider provideConfigWithTranslation
*/
public function testCreateWithTranslation(array $config, array $expected): void
{
/** @var Script $asset */
$asset = AssetFactory::create($config);
static::assertSame(
$expected,
$asset->translation()
);
}

/**
* @see testCreateWithTranslation
*/
public function provideConfigWithTranslation(): array
{
return [
'config with array translation' => [
'config' => [
'type' => Script::class,
'url' => 'https://localhost',
'handle' => 'unique-script',
'translation' => [
'domain' => 'whatever',
'path' => 'not/relevant',
],
],
'expected' => [
'domain' => 'whatever',
'path' => 'not/relevant',
],
],
'config with string translation' => [
'config' => [
'type' => Script::class,
'url' => 'https://localhost',
'handle' => 'unique-script',
'translation' => 'whatever-else',
],
'expected' => [
'domain' => 'whatever-else',
'path' => null,
],
],
'config without translation' => [
'config' => [
'type' => Script::class,
'url' => 'https://localhost',
'handle' => 'unique-script',
],
'expected' => [],
],
];
}

/**
* @dataProvider provideConfigWithLocalize
*/
public function testCreateWithLocalize(array $config, array $expected): void
{
/** @var Script $asset */
$asset = AssetFactory::create($config);
static::assertSame(
$expected,
$asset->localize()
);
}
/**
* @see testCreateWithLocalize
*/
public function provideConfigWithLocalize(): array
{
return [
'localize is array' => [
'config' => [
'type' => Script::class,
'url' => 'https://localhost',
'handle' => 'script-with-localize',
'localize' => [
'SomeObject' => [
'propertyOne' => 'someValue',
],
],
],
'expected' => [
'SomeObject' => [
'propertyOne' => 'someValue',
],
],
],
'localize is callable' => [
'config' => [
'type' => Script::class,
'url' => 'https://localhost',
'handle' => 'script-with-localize',
'localize' => static function () {
return [
'SomeObject' => [
'propertyTwo' => 'someValue',
],
];
},
],
'expected' => [
'SomeObject' => [
'propertyTwo' => 'someValue',
],
],
],
'localized value is callable' => [
'config' => [
'type' => Script::class,
'url' => 'https://localhost',
'handle' => 'script-with-localize',
'localize' => [
'SomeObject' => static function () {
return ['propertyThree' => 'someValue'];
},
],
],
'expected' => [
'SomeObject' => [
'propertyThree' => 'someValue',
],
],
],
];
}
}

0 comments on commit 4af9e2b

Please sign in to comment.