Skip to content

Commit

Permalink
Bug fix. Issue #2: Unparsed words fire ParseWasSuccessful event on wo…
Browse files Browse the repository at this point in the history
…rd creation.
zachleigh committed Apr 19, 2016
1 parent d9882ba commit ca79349
Showing 7 changed files with 124 additions and 9 deletions.
13 changes: 9 additions & 4 deletions src/Classes/LimelightWord.php
Original file line number Diff line number Diff line change
@@ -82,6 +82,11 @@ class LimelightWord
*/
private $converter;

/**
* @var Limelight\Limelight
*/
private $limelight;

/**
* Flag for calling Converter.
*
@@ -96,12 +101,14 @@ class LimelightWord
* @param array $properties
* @param Converter $converter
*/
public function __construct($token, $properties, Converter $converter)
public function __construct($token, $properties, Converter $converter, Limelight $limelight)
{
$this->setProperties($token, $properties);

$this->converter = $converter;

$this->limelight = $limelight;

$this->setMissingParameters();
}

@@ -426,9 +433,7 @@ private function setMissingParameters()

$this->pronunciation = $this->word;

$limelight = new Limelight();

$results = $limelight->noParse($this->word());
$results = $this->limelight->noParse($this->word(), ['Romaji'], true);

$this->setPluginData('Romaji', $results->toRomaji()->words());
}
27 changes: 26 additions & 1 deletion src/Events/Dispatcher.php
Original file line number Diff line number Diff line change
@@ -13,6 +13,13 @@ class Dispatcher
*/
protected $registeredListeners = [];

/**
* When false, events are fired.
*
* @var boolean
*/
protected $supressEvents = false;

/**
* Construct.
*
@@ -72,7 +79,7 @@ public function getListeners()
*/
public function fire($eventName, $payload = null)
{
if (isset($this->registeredListeners[$eventName])) {
if (isset($this->registeredListeners[$eventName]) && $this->supressEvents === false) {
$listeners = $this->registeredListeners[$eventName];

return array_map(function (LimelightListener $listener) use ($payload) {
@@ -82,4 +89,22 @@ public function fire($eventName, $payload = null)

return false;
}

/**
* Turn eventing on/off.
*
* @param bool $supressEvents
*
* @return bool
*/
public function toggleEvents($supressEvents)
{
if ($supressEvents === true && $this->supressEvents === false) {
$this->supressEvents = true;
} elseif ($supressEvents === true && $this->supressEvents === true) {
$this->supressEvents = false;
}

return $this->supressEvents;
}
}
11 changes: 9 additions & 2 deletions src/Limelight.php
Original file line number Diff line number Diff line change
@@ -63,14 +63,21 @@ public function parse($text, $runPlugins = true)
*
* @param string $text
* @param array $pluginWhiteList [Plugins to run]
* @param bool $fireEvents [When false, events will not be fired]
*
* @return Limelight\Classes\LimelightResults/ InvalidInputException
*/
public function noParse($text, $pluginWhiteList = ['Romaji'])
public function noParse($text, $pluginWhiteList = ['Romaji'], $supressEvents = false)
{
$this->dispatcher->toggleEvents($supressEvents);

$noParser = new NoParser($this, $this->dispatcher);

return $noParser->handle($text, $pluginWhiteList);
$results = $noParser->handle($text, $pluginWhiteList);

$this->dispatcher->toggleEvents($supressEvents);

return $results;
}

/**
2 changes: 1 addition & 1 deletion src/Parse/NoParser.php
Original file line number Diff line number Diff line change
@@ -57,7 +57,7 @@ public function handle($text, array $pluginWhiteList)

$properties = $this->buildProperties();

$words = [new LimelightWord($token, $properties, $converter)];
$words = [new LimelightWord($token, $properties, $converter, $this->limelight)];

$this->dispatcher->fire('WordWasCreated', $words[0]);

2 changes: 1 addition & 1 deletion src/Parse/TokenParser.php
Original file line number Diff line number Diff line change
@@ -172,7 +172,7 @@ private function appendWordToLast($current, $properties, $previousWord)
*/
private function makeNewWord($current, $properties, Converter $converter)
{
$word = new LimelightWord($current, $properties, $converter);
$word = new LimelightWord($current, $properties, $converter, $this->limelight);

$this->dispatcher->fire('WordWasCreated', $word);

44 changes: 44 additions & 0 deletions tests/Integration/LimelightTest.php
Original file line number Diff line number Diff line change
@@ -210,4 +210,48 @@ public function it_fires_event_that_sets_new_listener_after_results_object_is_cr

$config->resetConfig();
}

/**
* @test
*/
public function it_doesnt_fires_event_when_supressed_in_noparse()
{
$config = Config::getInstance();

$config->set(['Limelight\Tests\Stubs\TestListener'], 'listeners', 'WordWasCreated');

$this->assertEquals('Limelight\Tests\Stubs\TestListener', $config->get('listeners')['WordWasCreated'][0]);

$limelight = new Limelight();

$results = $limelight->noParse('できるかな。。。', ['Romaji'], true);

$this->assertEquals(['Limelight\Tests\Stubs\TestListener'], $config->get('listeners')['WordWasCreated']);

$config->resetConfig();
}

/**
* @test
*/
public function it_turns_events_back_on_after_running_noparse_with_event_supression()
{
$config = Config::getInstance();

$config->set(['Limelight\Tests\Stubs\TestListener'], 'listeners', 'WordWasCreated');

$this->assertEquals('Limelight\Tests\Stubs\TestListener', $config->get('listeners')['WordWasCreated'][0]);

$limelight = new Limelight();

$results = $limelight->noParse('できるかな。。。', ['Romaji'], true);

$this->assertEquals(['Limelight\Tests\Stubs\TestListener'], $config->get('listeners')['WordWasCreated']);

$results = $limelight->parse('出来るかな。。。');

$this->assertEquals([], $config->get('listeners')['WordWasCreated']);

$config->resetConfig();
}
}
34 changes: 34 additions & 0 deletions tests/Unit/EventTest.php
Original file line number Diff line number Diff line change
@@ -133,6 +133,40 @@ public function dispatcher_sends_payload()
$this->assertEquals('Payload says Hello!', $result[0]);
}

/**
* @test
*/
public function dispatcher_can_be_supressed()
{
$dispatcher = $this->buildDispatcher();

$dispatcher->toggleEvents(true);

$listener = new TestListener();

$dispatcher->addListeners($listener, 'WordWasCreated');

$result = $dispatcher->fire('WordWasCreated');

$this->assertFalse($result);
}

/**
* @test
*/
public function dispatcher_toggles_event_supression()
{
$dispatcher = $this->buildDispatcher();

$supressEvents = $dispatcher->toggleEvents(true);

$this->assertTrue($supressEvents);

$supressEvents = $dispatcher->toggleEvents(true);

$this->assertFalse($supressEvents);
}

/**
* @test
*

0 comments on commit ca79349

Please sign in to comment.