-
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(layout): add basic layout class
- Loading branch information
Showing
2 changed files
with
85 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,27 @@ | ||
<?php | ||
|
||
namespace ACFComposer; | ||
|
||
use Exception; | ||
|
||
class Layout { | ||
function __construct($config) { | ||
if(is_string($config)) { | ||
$config = apply_filters($config, null); | ||
} | ||
$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('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,58 @@ | ||
<?php | ||
|
||
require_once dirname(__DIR__) . '/lib/ACFComposer/Layout.php'; | ||
|
||
use ACFComposer\TestCase; | ||
use ACFComposer\Layout; | ||
use Brain\Monkey\WP\Filters; | ||
|
||
class LayoutTest extends TestCase { | ||
function testAssignsValidConfig() { | ||
$config = [ | ||
'name' => 'someLayout', | ||
'label' => 'Some Layout' | ||
]; | ||
$layout = new Layout($config); | ||
$this->assertEquals($config, $layout->config); | ||
} | ||
|
||
function testFailsWithoutName() { | ||
$config = [ | ||
'label' => 'Some Layout' | ||
]; | ||
$this->expectException(Exception::class); | ||
new Layout($config); | ||
} | ||
|
||
function testFailsWithoutLabel() { | ||
$config = [ | ||
'name' => 'someLayout' | ||
]; | ||
$this->expectException(Exception::class); | ||
new Layout($config); | ||
} | ||
|
||
function testFailsWithKey() { | ||
$config = [ | ||
'name' => 'someLayout', | ||
'label' => 'Some Layout', | ||
'key' => 'someKey' | ||
]; | ||
$this->expectException(Exception::class); | ||
new Layout($config); | ||
} | ||
|
||
function testGetConfigFromFilter() { | ||
$config = 'ACFComposer/Layouts/someLayout'; | ||
$someLayout = [ | ||
'name' => 'someLayout', | ||
'label' => 'Some Layout', | ||
'type' => 'someType' | ||
]; | ||
Filters::expectApplied($config) | ||
->once() | ||
->andReturn($someLayout); | ||
$layout = new Layout($config); | ||
$this->assertEquals($someLayout, $layout->config); | ||
} | ||
} |