Skip to content

Commit

Permalink
Added new tests to Script::withLocalize and support back to single cl…
Browse files Browse the repository at this point in the history
…osure.
  • Loading branch information
Chrico committed Jul 17, 2019
1 parent dfbf4d3 commit a46932b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 46 deletions.
20 changes: 18 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,24 @@ Added new methods:
* append inline styles
* set directly `Style::useAsyncFilter()` to attach filter.

### Breaking Change
`Script::localize()` now does not accept anymore a single closure, instead you can add multiple `$objectValues` which can contain a closoure. For migration have a look at [Migration from 1.3 to 1.4](https://github.com/inpsyde/assets/blob/1.4/docs/99%20-%20Migration.md#from-13-to-14).
### Script localization
Script localization accepts now multiple closures as objectValues:

```php
<?php
use Inpsyde\Assets\Script;

$script = new Script('handle', 'handle.js');
$script
->withLocalize('objectName', 'objectValue')
->withLocalize('posts', function(): array {
return get_posts(['post_type' => 'page']);
})
->withLocalize('foo', function(): string {
return 'bar';
});
```


----

Expand Down
37 changes: 0 additions & 37 deletions docs/99 - Migration.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,5 @@
# Migration

## From 1.3 to 1.4
Due the change in version 1.4 to mutable `Style` and `Script`, we've now the possibility to add multiple localized data to an instance.

**Before:**
Before it was only possible to add a single callable for lazy evaluation which contained everything.

```
<?php
use Inpsyde\Assets\Script;
$script = new Script(
'handle',
'script.js',
[
'localize' => function() : array {
return [
'objectName' => 'objectValue'
'posts' => get_posts(['post_type' => 'page'])
];
}
]
```

**After:**
Now, due the public API `Script::withLocalize`, we can add multiple values - including callables - to different `$objectNames` to be more flexible:

```php
use Inpsyde\Assets\Script;

$script = new Script('handle', 'handle.js');
$script
->withLocalize('objectName', 'objectValue')
->withLocalize('posts', function() {
return get_posts(['post_type' => 'page']);
});
```

## From 1.0 to 1.1
In version 1.1 we didn't implemented breaking changes, but we're renamend some internals for `Inpsyde\Assets\Asset` to be more clear when using the config-driven approach. In future you'll have to change your Asset-configuration, since we don't want to ship to much legacy code.

Expand Down
2 changes: 1 addition & 1 deletion src/BaseAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ abstract protected function defaultHandler(): string;
*/
public function data(): array
{
return $this->config('data', []);
return (array)$this->config('data', []);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Script.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ public function localize(): array
{
$localize = $this->config('localize', []);

// @deprecated
is_callable($localize) and $localize = $localize();

$output = [];
foreach ($localize as $objectName => $data) {
$output[$objectName] = is_callable($data)
? $data()
: (array) $data;
: $data;
}

return (array) $output;
Expand Down
61 changes: 56 additions & 5 deletions tests/phpunit/Unit/ScriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,67 @@ public function testWithTranslation()
static::assertSame($expected, $testee->translation());
}

public function testWithLocalize()
/**
* @param string $objectName
* @param $objectValue
* @param $expected
*
* @dataProvider provideLocalized
*/
public function testWithLocalize(string $objectName, $objectValue, $expected)
{
$expectedKey = 'foo';
$expectedValue = ['bar' => 'baz'];
$expected = [$expectedKey => $expectedValue];
$testee = new Script('handle', 'script.js');

static::assertEmpty($testee->localize());

$testee->withLocalize($expectedKey, $expectedValue);
$testee->withLocalize($objectName, $objectValue);

static::assertSame($expected, $testee->localize());
}

public function provideLocalized()
{
yield 'string value' => [
'objectName',
'objectValue',
['objectName' => 'objectValue'],
];

yield 'int value' => [
'objectName',
2,
['objectName' => 2],
];

$expectedValue = ['foo', 'bar' => 'baz'];
yield 'array value' => [
'objectName',
$expectedValue,
['objectName' => $expectedValue],
];

yield 'closure' => [
'objectName',
function (): string {
return 'objectValue';
},
['objectName' => 'objectValue'],
];
}

public function testLocalizedSingleClosure()
{
$expected = ['foo' => ['bar' => 'baz']];
$testee = new Script(
'handle',
'script.js',
Asset::FRONTEND,
[
'localize' => function () use ($expected): array {
return $expected;
},
]
);

static::assertSame($expected, $testee->localize());
}
Expand Down

0 comments on commit a46932b

Please sign in to comment.