Skip to content

Commit

Permalink
feat(ResolveConfig): make resolve field group fail on invalid locatio…
Browse files Browse the repository at this point in the history
…n and field
  • Loading branch information
domtra committed Nov 8, 2016
1 parent d2438df commit 4924c75
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/ACFComposer/ResolveConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ public static function forFieldGroup($config) {

$keySuffix = $output['name'];
$output['key'] = "group_{$keySuffix}";
$output = self::forNestedEntities($output, $keySuffix);
$output['fields'] = array_map(function($field) use ($keySuffix){
return self::forField($field, $keySuffix);
}, $output['fields']);
$output['location'] = array_map(function($locationArray) {
return array_map(function($location) {
return self::forLocation($location);
}, $locationArray);
}, $output['location']);
return $output;
}

public static function forLocation($config) {
return self::validateConfig($config, ['param', 'operator', 'value']);
}

public static function forField($config, $keySuffix = '') {
return self::forEntity($config, ['name', 'label', 'type'], $keySuffix);
}
Expand Down
44 changes: 44 additions & 0 deletions tests/test-resolveConfigForFieldGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,48 @@ function testForFieldGroupWithValidConfig() {
$config['fields'] = [$fieldConfig];
$this->assertEquals($config, $output);
}

function testForFieldGroupFailsWithInvalidField() {
$fieldConfig = [
'name' => 'someField',
'label' => 'Some Field'
];
$locationConfig = [
'param' => 'someParam',
'operator' => 'someOperator',
'value' => 'someValue'
];
$config = [
'name' => 'someGroup',
'title' => 'Some Group',
'fields' => [$fieldConfig],
'location' => [
[$locationConfig]
]
];
$this->expectException(Exception::class);
ResolveConfig::forFieldGroup($config);
}

function testForFieldGroupFailsWithInvalidLocation() {
$fieldConfig = [
'name' => 'someField',
'label' => 'Some Field',
'type' => 'someType'
];
$locationConfig = [
'operator' => 'someOperator',
'value' => 'someValue'
];
$config = [
'name' => 'someGroup',
'title' => 'Some Group',
'fields' => [$fieldConfig],
'location' => [
[$locationConfig]
]
];
$this->expectException(Exception::class);
ResolveConfig::forFieldGroup($config);
}
}

0 comments on commit 4924c75

Please sign in to comment.