Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass current Node as argument to lambdas #41

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 13 additions & 3 deletions lib/phake/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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($this->application, $this->target_node);
} catch (\Exception $e) {
$thrown = $e;
}

$this->target_node = $this->target_node->get_parent();

if ($thrown) {
throw $e;
}
}

public function before($name, $lambda) {
Expand Down
6 changes: 3 additions & 3 deletions lib/phake/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
40 changes: 40 additions & 0 deletions tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,44 @@ public function testGroups()
);
$builder->get_application()->invoke('default');
}

public function testArguments()
{
$builder = new Builder();

$builder->load_runfile($this->getFixture('arguments.php'));

$this->expectOutputString(<<<EOF
first
phake\Application
phake\Node
first:test
phake\Application
phake\Node

EOF
);
$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());
});
}
}
15 changes: 15 additions & 0 deletions tests/fixtures/arguments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

group('first', function($app, $node) {
echo $node->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');