diff --git a/lib/ACFComposer/ResolveConfig.php b/lib/ACFComposer/ResolveConfig.php index 24cd26e..c476026 100644 --- a/lib/ACFComposer/ResolveConfig.php +++ b/lib/ACFComposer/ResolveConfig.php @@ -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; } @@ -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) { diff --git a/tests/test-resolveConfigForField.php b/tests/test-resolveConfigForField.php index e17a4be..27c0b1c 100644 --- a/tests/test-resolveConfigForField.php +++ b/tests/test-resolveConfigForField.php @@ -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', diff --git a/tests/test-resolveConfigForFieldGroup.php b/tests/test-resolveConfigForFieldGroup.php index bcd6428..42c4443 100644 --- a/tests/test-resolveConfigForFieldGroup.php +++ b/tests/test-resolveConfigForFieldGroup.php @@ -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', @@ -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); }