Skip to content

Commit

Permalink
Merge pull request #1 from bleech/fix-simple-fields-array
Browse files Browse the repository at this point in the history
Fix simple fields array (Scrutinizer check faulty, needs fix)
  • Loading branch information
Doğa Gürdal authored Dec 14, 2016
2 parents 465ea94 + 3235129 commit 1f8376e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
22 changes: 18 additions & 4 deletions lib/ACFComposer/ResolveConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ public static function forFieldGroup($config) {

$keySuffix = $output['name'];
$output['key'] = "group_{$keySuffix}";
$output['fields'] = array_map(function ($field) use ($keySuffix) {
return self::forField($field, [$keySuffix]);
}, $output['fields']);
$output['fields'] = array_reduce($config['fields'], function ($carry, $fieldConfig) use ($keySuffix) {
$fields = self::forField($fieldConfig, [$keySuffix]);
if (!self::isAssoc($fields)) {
foreach ($fields as $field) {
array_push($carry, $field);
}
} else {
array_push($carry, $fields);
}
return $carry;
}, []);
$output['location'] = array_map('self::mapLocation', $output['location']);
return $output;
}
Expand All @@ -31,7 +39,13 @@ public static function forLayout($config, $parentKeys = []) {

protected static function forEntity($config, $requiredAttributes, $parentKeys = []) {
if (is_string($config)) {
$config = apply_filters($config, null);
$filterName = $config;
$config = apply_filters($filterName, null);

if (is_null($config)) {
trigger_error("ACFComposer: Filter {$filterName} does not exist!", E_USER_WARNING);
return [];
}
}
if (!self::isAssoc($config)) {
return array_map(function ($singleConfig) use ($requiredAttributes, $parentKeys) {
Expand Down
18 changes: 18 additions & 0 deletions tests/test-resolveConfigForField.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ function testForFieldGetConfigFromFilter() {
$this->assertEquals($someField, $output);
}

function testForFieldTriggerErrorWithoutFilter() {
$config = 'ACFComposer/Fields/someField';
Filters::expectApplied($config)
->once()
->andReturn(null);
$this->expectException('PHPUnit_Framework_Error_Warning');
$output = ResolveConfig::forField($config);
}

function testForFieldReturnEmptyArrayWithoutFilter() {
$config = 'ACFComposer/Fields/someField';
Filters::expectApplied($config)
->once()
->andReturn(null);
$output = @ResolveConfig::forField($config);
$this->assertEquals($output, []);
}

function testForFieldWithValidSubField() {
$subFieldConfig = [
'name' => 'subField',
Expand Down
28 changes: 26 additions & 2 deletions tests/test-resolveConfigForFieldGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,24 @@

class ResolveConfigForFieldGroupTest extends TestCase {
function testForFieldGroupWithValidConfig() {
$filterName = 'ACFComposer/Fields/someField';
$fieldConfig = [
'name' => 'someField',
'label' => 'Some Field',
'type' => 'someType'
];
$fieldConfigMulti = [
[
'name' => 'someField1',
'label' => 'Some Field1',
'type' => 'someType'
],
[
'name' => 'someField2',
'label' => 'Some Field2',
'type' => 'someType'
]
];
$locationConfig = [
'param' => 'someParam',
'operator' => 'someOperator',
Expand All @@ -21,15 +34,26 @@ function testForFieldGroupWithValidConfig() {
$config = [
'name' => 'someGroup',
'title' => 'Some Group',
'fields' => [$fieldConfig],
'fields' => [
$filterName,
$fieldConfig,
$fieldConfigMulti
],
'location' => [
[$locationConfig]
]
];

Filters::expectApplied($filterName)
->once()
->andReturn($fieldConfig);

$output = ResolveConfig::forFieldGroup($config);
$fieldConfig['key'] = 'field_someGroup_someField';
$fieldConfigMulti[0]['key'] = 'field_someGroup_someField1';
$fieldConfigMulti[1]['key'] = 'field_someGroup_someField2';
$config['key'] = 'group_someGroup';
$config['fields'] = [$fieldConfig];
$config['fields'] = [$fieldConfig, $fieldConfig, $fieldConfigMulti[0], $fieldConfigMulti[1]];
$this->assertEquals($config, $output);
}

Expand Down

0 comments on commit 1f8376e

Please sign in to comment.