Skip to content

Commit

Permalink
Extended test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Aug 29, 2015
1 parent 376d14e commit 44227ec
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 131 deletions.
7 changes: 7 additions & 0 deletions lib/EventHookReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ static private function make_key($hook)
return implode('#', $hook);
}

if ($hook instanceof \Closure)
{
$reflection = new \ReflectionFunction($hook);

return $reflection->getFileName() . '#'. $reflection->getStartLine() . '#'. $reflection->getEndLine();
}

if (is_object($hook))
{
/* @var $hook object */
Expand Down
89 changes: 67 additions & 22 deletions tests/EventCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace ICanBoogie;

use ICanBoogie\EventTest\CallableInstance;
use ICanBoogie\EventTest\Hooks;
use ICanBoogie\EventTest\Target;

class EventCollectionTest extends \PHPUnit_Framework_TestCase
Expand All @@ -20,33 +18,59 @@ public function setUp()
EventCollection::set_instance_provider(function () use ($events) { return $events; });
}

/**
* @dataProvider provide_test_should_resolve_event_type_from_hook
*
* @param mixed $hook
*/
public function test_should_resolve_event_type_from_hook($hook)
public function test_get()
{
$this->assertEquals('ICanBoogie\EventTest\Target::practice:before', $this->events->attach($hook)->type);
$this->assertSame($this->events, EventCollection::get());
}

public function provide_test_should_resolve_event_type_from_hook()
public function test_generic_event()
{
return [
$type = 'type' . uniqid();
$invoked = false;

$this->events->attach($type, function(Event $event) use (&$invoked) {

$invoked = true;

});

[ 'ICanBoogie\EventTest\before_target_practice' ],
[ Hooks::class . '::before_target_practice' ],
[ [ Hooks::class, 'before_target_practice' ] ],
[ function(Target\BeforePracticeEvent $event, Target $target) { } ],
[ new CallableInstance ]
new Event(null, $type);

];
$this->assertTrue($invoked);
}

/**
* @depends test_should_resolve_event_type_from_hook
*/
public function test_detach_hook()
public function test_detach_generic_event_hook()
{
$type = 'type' . uniqid();
$hook = function(Event $event) {

$this->fail("Should not be invoked");

};

$this->events->attach($type, $hook);
$this->events->detach($type, $hook);

new Event(null, $type);
}

public function test_detach_event_hook()
{
$type = 'type' . uniqid();

$hook = function(Event $event, Target $target) {

$this->fail("Should not be invoked");

};

$this->events->attach($type, $hook);
$this->events->detach($type, $hook);

new Event(new Target, $type);
}

public function test_detach_typed_event_hook()
{
$hook = function(Target\BeforePracticeEvent $event, Target $target) {

Expand All @@ -69,7 +93,7 @@ public function test_detach_unattached_hook()
}

/**
* @depends test_detach_hook
* @depends test_detach_typed_event_hook
*/
public function test_attach_to()
{
Expand Down Expand Up @@ -103,4 +127,25 @@ public function test_attach_to_should_throw_exception_when_target_is_not_an_obje
{
$this->events->attach_to(uniqid(), function() {});
}

public function test_once()
{
$invoked_count = 0;

$this->events->once(function(Target\PracticeEvent $event, Target $target) use (&$invoked_count) {

$invoked_count++;

});

$target = new Target;

new Target\PracticeEvent($target);
$this->assertEquals(1, $invoked_count);

new Target\PracticeEvent($target);
new Target\PracticeEvent($target);
new Target\PracticeEvent($target);
$this->assertEquals(1, $invoked_count);
}
}
8 changes: 8 additions & 0 deletions tests/EventHookRefectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public function test_invalid()
EventHookReflection::assert_valid(123);
}

public function test_from()
{
$hook = function(Target\PracticeEvent $event, Target $target) {};

$reflection = EventHookReflection::from($hook);
$this->assertSame($reflection, EventHookReflection::from($hook));
}

/**
* @dataProvider provide_event_hooks
*
Expand Down
141 changes: 32 additions & 109 deletions tests/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,60 +36,57 @@ public function setUp()
EventCollection::set_instance_provider(function () use ($events) { return $events; });
}

/**
* Are event hooks attached to classes are correctly detached ?
*/
public function testDetachTypedEvent()
public function test_from()
{
$a = new A;
$target = new Target;
$event = Target\BeforePracticeEvent::from([ 'target' => $target ]);

$done = null;
$this->assertInstanceOf(Target\BeforePracticeEvent::class, $event);
}

$hook = function(Event $event) use (&$done)
{
$done = true;
};
public function test_stop()
{
$type = 'event-' . uniqid();

$this->events->attach($type, function(Event $event) {

$this->events->attach(get_class($a) . '::tmp', $hook);
$this->fail("Should not be invoked.");

new Event($a, 'tmp');
});

$this->assertTrue($done);
$this->events->attach($type, function(Event $event) {

$done = null;
$event->stop();

$this->events->detach(get_class($a) . '::tmp', $hook);
});

new Event($a, 'tmp');
$event = new Event(null, $type);

$this->assertNull($done);
$this->assertTrue($event->stopped);
}

/**
* Are event hooks attached to classes are correctly detached ?
*/
public function testDetachTypedEventUsingInterface()
public function test_used_by()
{
$a = new A;
$type = 'event-' . uniqid();

$done = null;
$hook1 = function(Event $event) { };
$hook2 = function(Event $event) { };

$he = $this->events->attach(get_class($a) . '::tmp', function(Event $event) use (&$done)
{
$done = true;
});

new Event($a, 'tmp');
$this->events->attach($type, $hook1);
$this->events->attach($type, $hook2);

$this->assertTrue($done);
$event = new Event(null, $type);

$done = null;

$he->detach();
$this->assertSame([ $hook2, $hook1 ], $event->used_by);
$this->assertEquals(2, $event->used);
}

new Event($a, 'tmp');
public function test_target()
{
$target = new Target;
$event = new Event($target, uniqid());

$this->assertNull($done);
$this->assertSame($target, $event->target);
}

/**
Expand Down Expand Up @@ -170,80 +167,6 @@ public function testEventHooks()
$this->assertEquals('one,two,three,four,five', implode(',', array_keys($b_processed)));
}

public function test_once()
{
$n = 0;
$m = 0;

$once = function() use(&$n) {

$n++;

};

$this->events->once('once', $once);

$eh = $this->events->attach('once', function() use(&$m) {

$m++;

});

new Event(null, 'once');
new Event(null, 'once');
new Event(null, 'once');

$this->assertEquals(1, $n);
$this->assertEquals(3, $m);

$eh->detach();

$this->events->once('once', $once);

new Event(null, 'once');
new Event(null, 'once');
new Event(null, 'once');

$this->assertEquals(2, $n);

$this->events->attach('once', $once);

new Event(null, 'once');
new Event(null, 'once');
new Event(null, 'once');

$this->assertEquals(5, $n);
}

public function test_once_with_closure()
{
$events = $this->events;
$n = 0;
$target = new A;

$eh = $events->once(function(ProcessEvent $event, A $target) use (&$n) {

$n++;

});

$eh->detach();

new ProcessEvent($target, []);
$this->assertEquals(0, $n);

$events->once(function(ProcessEvent $event, A $target) use (&$n) {

$n++;

});

new ProcessEvent($target, []);
$this->assertEquals(1, $n);

new ProcessEvent($target, []);
$this->assertEquals(1, $n);
}

/**
* @dataProvider provide_test_reserved
Expand Down

0 comments on commit 44227ec

Please sign in to comment.