starting work on dsl
This commit is contained in:
parent
17eb44e92a
commit
c37be6be2b
76
classes/FixtureBuilder.inc
Normal file
76
classes/FixtureBuilder.inc
Normal file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
class FixtureBuilder {
|
||||
public $deferred_builds;
|
||||
private $current_object;
|
||||
|
||||
function __construct() {
|
||||
$this->deferred_builds = array();
|
||||
$this->current_object = null;
|
||||
}
|
||||
|
||||
function ensure_type($type) {
|
||||
if (!isset($this->deferred_builds[$type])) {
|
||||
$this->deferred_builds[$type] = array();
|
||||
}
|
||||
}
|
||||
|
||||
function check_correct_type($name) {
|
||||
$ok = false;
|
||||
if (!empty($this->current_object)) {
|
||||
if ($name != $this->current_object['type']) {
|
||||
throw new LogicException("You can't call ${name} at this point.");
|
||||
}
|
||||
$ok = true;
|
||||
}
|
||||
return $ok;
|
||||
}
|
||||
|
||||
function category($name) {
|
||||
if ($this->check_correct_type('category')) {
|
||||
$this->defer();
|
||||
}
|
||||
|
||||
$this->current_object = array(
|
||||
'type' => 'category', 'name' => $name
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
function post($title) {
|
||||
if ($this->check_correct_type('post')) {
|
||||
$this->defer();
|
||||
}
|
||||
|
||||
$this->current_object = array(
|
||||
'type' => 'post', 'post' => array(
|
||||
'post_title' => $title,
|
||||
'post_type' => 'post'
|
||||
)
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
function date($date) {
|
||||
$this->check_correct_type('post');
|
||||
|
||||
$this->current_object['post']['post_date'] = $date;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function defer() {
|
||||
if (!empty($this->current_object)) {
|
||||
$this->ensure_type($this->current_object['type']);
|
||||
$this->{"build_{$this->current_object['type']}"}($this->current_object);
|
||||
}
|
||||
}
|
||||
|
||||
function build_category($obj) {
|
||||
$this->deferred_builds['category'] = array_merge($this->deferred_builds['category'], explode('/', $obj['name']));
|
||||
}
|
||||
|
||||
function build_post($obj) {
|
||||
$this->deferred_builds['post'][] = $obj['post'];
|
||||
}
|
||||
}
|
84
test/FixtureBuilderTest.php
Normal file
84
test/FixtureBuilderTest.php
Normal file
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
require_once('PHPUnit/Framework.php');
|
||||
require_once('MockPress/mockpress.php');
|
||||
require_once(dirname(__FILE__) . '/../classes/FixtureBuilder.inc');
|
||||
|
||||
class FixtureBuilderTest extends PHPUnit_Framework_TestCase {
|
||||
function setUp() {
|
||||
_reset_wp();
|
||||
}
|
||||
|
||||
function providerTestDeferBuild() {
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
array('category', array('test'))
|
||||
),
|
||||
array('category' => array('test')),
|
||||
array(true)
|
||||
),
|
||||
array(
|
||||
array(
|
||||
array('category', array('test/test2'))
|
||||
),
|
||||
array('category' => array('test', 'test2')),
|
||||
array(true)
|
||||
),
|
||||
array(
|
||||
array(
|
||||
array('category', array('test')),
|
||||
array('post', array('Post title')),
|
||||
),
|
||||
array('category' => array('test')),
|
||||
array(true, false)
|
||||
),
|
||||
array(
|
||||
array(
|
||||
array('category', array('test')),
|
||||
array('category', array('test2')),
|
||||
),
|
||||
array('category' => array('test', 'test2')),
|
||||
array(true, true)
|
||||
),
|
||||
array(
|
||||
array(
|
||||
array('post', array('Post title')),
|
||||
array('date', array('2010-01-01'))
|
||||
),
|
||||
array('post' => array(
|
||||
array(
|
||||
'post_title' => 'Post title',
|
||||
'post_date' => '2010-01-01',
|
||||
'post_type' => 'post',
|
||||
)
|
||||
)),
|
||||
array(true, true)
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerTestDeferBuild
|
||||
*/
|
||||
function testDeferBuild($instructions, $expected_deferred_build, $expected_exceptions) {
|
||||
$builder = new FixtureBuilder();
|
||||
|
||||
foreach ($instructions as $info) {
|
||||
list($instruction, $parameters) = $info;
|
||||
$expected_exception = array_shift($expected_exceptions);
|
||||
|
||||
try {
|
||||
$return = call_user_func_array(array($builder, $instruction), $parameters);
|
||||
$this->assertTrue($expected_exception);
|
||||
} catch (Exception $e) {
|
||||
$this->assertFalse($expected_exception);
|
||||
$this->assertType('LogicException', $e);
|
||||
}
|
||||
}
|
||||
|
||||
$builder->defer();
|
||||
|
||||
$this->assertEquals($expected_deferred_build, $builder->deferred_builds);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user