Skip to content

Commit

Permalink
feat(field): basic config validation
Browse files Browse the repository at this point in the history
  • Loading branch information
domtra committed Nov 7, 2016
1 parent 2111e9f commit b1b7ce3
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
17 changes: 17 additions & 0 deletions acf-field-group-composer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Plugin Name: ACF Field Group Composer
* Plugin URI: PLUGIN SITE HERE
* Description: PLUGIN DESCRIPTION HERE
* Author: YOUR NAME HERE
* Author URI: YOUR SITE HERE
* Text Domain: acf-field-group-composer
* Domain Path: /languages
* Version: 0.1.0
*
* @package ACF_Field_Group_Composer
*/

require_once __DIR__ . '/lib/WPStarter/Helpers.php';
require_once __DIR__ . '/lib/WPStarter/ConstructionPlan.php';
require_once __DIR__ . '/lib/WPStarter/Renderer.php';
7 changes: 7 additions & 0 deletions lib/ACFComposer/ACFComposer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace ACFComposer;

class ACFComposer {

}
27 changes: 27 additions & 0 deletions lib/ACFComposer/Field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace ACFComposer;

use Exception;

class Field {
function __construct($config) {
$this->config = $this->validateConfig($config);
}

protected function validateConfig($config) {
if(!array_key_exists('name', $config)) {
throw new Exception('Field config needs to contain a \'name\' property.');
}
if(!array_key_exists('label', $config)) {
throw new Exception('Field config needs to contain a \'label\' property.');
}
if(!array_key_exists('type', $config)) {
throw new Exception('Field config needs to contain a \'type\' property.');
}
if(array_key_exists('key', $config)) {
throw new Exception('Field config must not contain a \'key\' property.');
}
return $config;
}
}
20 changes: 20 additions & 0 deletions tests/test-acfComposer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* Class ConstructionPlanTest
*
* @package Wp_Starter_Plugin
*/

/**
* Construction plan test case.
*/
require_once dirname(__DIR__) . '/lib/ACFComposer/ACFComposer.php';

use ACFComposer\TestCase;
use ACFComposer\ACFComposer;

class ACFComposerTest extends TestCase {
function testSimple() {
$this->assertTrue(true);
}
}
56 changes: 56 additions & 0 deletions tests/test-field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

require_once dirname(__DIR__) . '/lib/ACFComposer/Field.php';

use ACFComposer\TestCase;
use ACFComposer\Field;

class FieldTest extends TestCase {
function testAssignsValidConfig() {
$config = [
'name' => 'someField',
'label' => 'Some Field',
'type' => 'someType'
];
$field = new Field($config);
$this->assertEquals($config, $field->config);
}

function testFailsWithoutName() {
$config = [
'label' => 'Some Field',
'type' => 'someType'
];
$this->expectException(Exception::class);
new Field($config);
}

function testFailsWithoutLabel() {
$config = [
'name' => 'someField',
'type' => 'someType'
];
$this->expectException(Exception::class);
new Field($config);
}

function testFailsWithoutType() {
$config = [
'name' => 'someField',
'label' => 'Some Field'
];
$this->expectException(Exception::class);
new Field($config);
}

function testFailsWithKey() {
$config = [
'name' => 'someField',
'label' => 'Some Field',
'type' => 'someType',
'key' => 'someKey'
];
$this->expectException(Exception::class);
new Field($config);
}
}

0 comments on commit b1b7ce3

Please sign in to comment.