Skip to content

Commit

Permalink
feat(layout): add basic layout class
Browse files Browse the repository at this point in the history
  • Loading branch information
domtra committed Nov 8, 2016
1 parent 0629020 commit 796b862
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/ACFComposer/Layout.php
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;
}
}
58 changes: 58 additions & 0 deletions tests/test-layout.php
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);
}
}

0 comments on commit 796b862

Please sign in to comment.