-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(field): basic config validation
- Loading branch information
Showing
5 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace ACFComposer; | ||
|
||
class ACFComposer { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |