Skip to content

Commit

Permalink
test event model
Browse files Browse the repository at this point in the history
  • Loading branch information
sokil committed Dec 14, 2014
1 parent 7d96d37 commit c85f947
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ public function subscribe($eventName, $callable, $priority = 0)
return $this;
}

/**
*
* @param string $eventName
* @param mixed $target
* @return \Sokil\FraudDetector\Event
*/
public function trigger($eventName, $target = null)
{
$event = new Event();
Expand All @@ -248,7 +254,6 @@ public function trigger($eventName, $target = null)
$event->setTarget($target);
}

$this->eventDispatcher->dispatch($eventName, $event);
return $this;
return $this->eventDispatcher->dispatch($eventName, $event);
}
}
31 changes: 31 additions & 0 deletions tests/DetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,35 @@ public function testIsProcessorDeclared()

$this->assertFalse($detector->isProcessorDeclared('SOME_UNEXISTED_PROCESSOR'));
}

public function testEvents()
{
$detector = new Detector;

$status = new \stdclass;
$status->value = '';

$detector->subscribe('event1', function($e) use($status) {
$status->value .= '[1.10]' . $e->getTarget();
}, 10);

$detector->subscribe('event1', function($e) use($status) {
$status->value .= '[1.20]' . $e->getTarget();
}, 20);

$detector->subscribe('event2', function($e) use($status) {
$status->value .= '[2.10]' . $e->getTarget();

$e->cancel();
}, 10);

$event1 = $detector->trigger('event1', 'target1');
$this->assertInstanceOf('\Sokil\FraudDetector\Event', $event1);
$this->assertFalse($event1->isCancelled());

$event2 = $detector->trigger('event2', 'target2');
$this->assertTrue($event2->isCancelled());

$this->assertEquals('[1.20]target1[1.10]target1[2.10]target2', $status->value);
}
}

0 comments on commit c85f947

Please sign in to comment.