From c37be6be2b0d451cf8836bcb240caaa41b44b5d5 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 16 Jan 2010 12:23:15 -0500 Subject: [PATCH] starting work on dsl --- classes/FixtureBuilder.inc | 76 +++++++++++++++++++++++++++++++++ test/FixtureBuilderTest.php | 84 +++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 classes/FixtureBuilder.inc create mode 100644 test/FixtureBuilderTest.php diff --git a/classes/FixtureBuilder.inc b/classes/FixtureBuilder.inc new file mode 100644 index 0000000..c30e99b --- /dev/null +++ b/classes/FixtureBuilder.inc @@ -0,0 +1,76 @@ +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']; + } +} diff --git a/test/FixtureBuilderTest.php b/test/FixtureBuilderTest.php new file mode 100644 index 0000000..256717b --- /dev/null +++ b/test/FixtureBuilderTest.php @@ -0,0 +1,84 @@ + 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); + } +}