From 7e063484e44be7c6d9fe58ca588e2fb80c1fef8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 22 Dec 2013 16:21:55 +0100 Subject: [PATCH 1/5] Move group() logic to Builder::add_group() --- lib/functions.php | 12 +----------- lib/phake/Builder.php | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/functions.php b/lib/functions.php index 75f51f9..ae5356c 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -18,17 +18,7 @@ function task() { } function group($name, $lambda = null) { - $thrown = null; - builder()->push_group($name); - try { - if ($lambda instanceof Closure) $lambda(); - } catch (\Exception $e) { - $thrown = $e; - } - builder()->pop_group(); - if ($thrown) { - throw $e; - } + builder()->add_group($name, $lambda); } function before($task, $lambda) { diff --git a/lib/phake/Builder.php b/lib/phake/Builder.php index d5dd264..6d61fa5 100644 --- a/lib/phake/Builder.php +++ b/lib/phake/Builder.php @@ -45,12 +45,22 @@ public function add_task($name, $work, $deps) { $this->assign_description($node); } - public function push_group($name) { + public function add_group($name, $lambda = null) { + $thrown = null; + $this->target_node = $this->target_node->child_with_name($name); - } - public function pop_group() { + try { + if ($lambda instanceof \Closure) $lambda(); + } catch (\Exception $e) { + $thrown = $e; + } + $this->target_node = $this->target_node->get_parent(); + + if ($thrown) { + throw $e; + } } public function before($name, $lambda) { From 61e3107ed9777b092c4c6902315045a0adb7ffb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 22 Dec 2013 16:25:05 +0100 Subject: [PATCH 2/5] Group lambda gets passed the $application argument Added for consistency with task lambdas --- lib/phake/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/phake/Builder.php b/lib/phake/Builder.php index 6d61fa5..bf1dc28 100644 --- a/lib/phake/Builder.php +++ b/lib/phake/Builder.php @@ -51,7 +51,7 @@ public function add_group($name, $lambda = null) { $this->target_node = $this->target_node->child_with_name($name); try { - if ($lambda instanceof \Closure) $lambda(); + if ($lambda instanceof \Closure) $lambda($this->application); } catch (\Exception $e) { $thrown = $e; } From 3e284bdeb7abcf1e49bcceb51fcb0fa097f158cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 22 Dec 2013 16:29:14 +0100 Subject: [PATCH 3/5] Always pass current node context to lambdas --- lib/phake/Builder.php | 2 +- lib/phake/Node.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/phake/Builder.php b/lib/phake/Builder.php index bf1dc28..1495952 100644 --- a/lib/phake/Builder.php +++ b/lib/phake/Builder.php @@ -51,7 +51,7 @@ public function add_group($name, $lambda = null) { $this->target_node = $this->target_node->child_with_name($name); try { - if ($lambda instanceof \Closure) $lambda($this->application); + if ($lambda instanceof \Closure) $lambda($this->application, $this->target_node); } catch (\Exception $e) { $thrown = $e; } diff --git a/lib/phake/Node.php b/lib/phake/Node.php index a84bcb5..a9f447f 100644 --- a/lib/phake/Node.php +++ b/lib/phake/Node.php @@ -92,9 +92,9 @@ public function invoke(Application $application) { return; } - foreach ($this->before as $t) $t($application); - foreach ($this->lambdas as $t) $t($application); - foreach ($this->after as $t) $t($application); + foreach ($this->before as $t) $t($application, $this); + foreach ($this->lambdas as $t) $t($application, $this); + foreach ($this->after as $t) $t($application, $this); $this->has_run = true; } From 32222ab0d351dfa2fe739930535adc6548105e1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 22 Dec 2013 16:45:11 +0100 Subject: [PATCH 4/5] Test arguments passed to lambdas --- tests/BuilderTest.php | 19 +++++++++++++++++++ tests/fixtures/arguments.php | 15 +++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 tests/fixtures/arguments.php diff --git a/tests/BuilderTest.php b/tests/BuilderTest.php index 57e378a..9ed787f 100644 --- a/tests/BuilderTest.php +++ b/tests/BuilderTest.php @@ -81,4 +81,23 @@ public function testGroups() ); $builder->get_application()->invoke('default'); } + + public function testArguments() + { + $builder = new Builder(); + + $builder->load_runfile($this->getFixture('arguments.php')); + + $this->expectOutputString(<<get_application()->invoke('default'); + } } diff --git a/tests/fixtures/arguments.php b/tests/fixtures/arguments.php new file mode 100644 index 0000000..f6094be --- /dev/null +++ b/tests/fixtures/arguments.php @@ -0,0 +1,15 @@ +get_name() . "\n"; + echo get_class($app) . "\n"; + echo get_class($node) . "\n"; + + task('test', function($app, $node) { + echo $node->get_name() . "\n"; + echo get_class($app) . "\n"; + echo get_class($node) . "\n"; + }); +}); + +task('default', 'first:test'); From 3c333597e9543ba77a67a6a34ad2cbc4aa7ef6ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 22 Dec 2013 16:45:51 +0100 Subject: [PATCH 5/5] Test context will be reset when group throws an Exception --- tests/BuilderTest.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/BuilderTest.php b/tests/BuilderTest.php index 9ed787f..ad18242 100644 --- a/tests/BuilderTest.php +++ b/tests/BuilderTest.php @@ -100,4 +100,25 @@ public function testArguments() ); $builder->get_application()->invoke('default'); } + + public function testBuilderGroupException() + { + $builder = new Builder(); + + try { + $builder->add_group('exception', function() { + throw new \Exception(); + }); + $this->fail('No exception thrown'); + } + catch (Exception $e) { + // exception caught. Now check our context is still correct + } + + $that = $this; + $builder->add_group('ok', function($app, $node) use ($that) { + $that->assertInstanceOf('phake\Node', $node); + $that->assertEquals('ok', $node->get_name()); + }); + } }