-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
organized tests and introduced react/promise
- Loading branch information
Showing
9 changed files
with
231 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
composer.lock | ||
vendor | ||
vendor | ||
test.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
namespace observr\Tests { | ||
use observr; | ||
|
||
/** | ||
* @package observr | ||
*/ | ||
class EmitterTest extends ObservrTestCase { | ||
function testEmitterPromise() { | ||
$click = new observr\Emitter('click'); | ||
|
||
$getPromise = function($result,$error=null) use ($click) { | ||
if($error) { | ||
$click->fail($error); | ||
} else { | ||
$click->complete($result); | ||
} | ||
return $click->promise(); | ||
}; | ||
|
||
$button = new Mock\Button; | ||
|
||
$c=0; | ||
$getPromise('hello')->then( | ||
function($value)use(&$c) { | ||
$c++; | ||
} | ||
); | ||
|
||
$click($button); | ||
$this->assertEquals(1,$c); | ||
} | ||
|
||
function testEmitterBasic() { | ||
$click = new observr\Emitter('click'); | ||
|
||
$button = new Mock\Button; | ||
|
||
$c = 0; | ||
|
||
$button->attach($click, function($s,$e)use(&$c) { | ||
$c++; | ||
}); | ||
|
||
$click->attach(function($s,$e)use(&$c) { | ||
$c++; | ||
}); | ||
|
||
$click($button); | ||
|
||
$this->assertEquals(2,$c); | ||
} | ||
|
||
function testCombinedEventSource() { | ||
$click = new observr\Emitter('click'); | ||
$mouseup = new observr\Emitter('mouseup'); | ||
$mousedown = new observr\Emitter('mousedown'); | ||
|
||
$combined = $click->map(function($sender, $e) { | ||
return $e['x'] = 'Click'; | ||
})->merge($mousedown->map(function($sender,$e) { | ||
return $e['x'] = 'Mousedown'; | ||
}))->merge($mouseup->map(function($sender,$e) { | ||
return $e['x'] = 'Mouseup'; | ||
})); | ||
|
||
$button = new Mock\Button; | ||
|
||
$c = 0; | ||
$button->attach($click,function($sender,$e)use(&$c) { | ||
$this->assertEquals('Click',$e['x']); | ||
$c++; | ||
}); | ||
|
||
$button->attach($mouseup,function($sender,$e)use(&$c) { | ||
$this->assertEquals('Mouseup',$e['x']); | ||
$c++; | ||
}); | ||
|
||
$button->attach($mousedown,function($sender,$e)use(&$c) { | ||
$this->assertEquals('Mousedown',$e['x']); | ||
$c++; | ||
}); | ||
|
||
$combined($button); | ||
|
||
$this->assertEquals(3,$c); | ||
} | ||
|
||
function testEventFilter() { | ||
$click = new observr\Emitter('click'); | ||
|
||
$doOK = $click | ||
->filter(function($button,$e) { | ||
if($button instanceof Mock\Button) { | ||
return true; | ||
} | ||
|
||
return false; | ||
})->map(function($button,$e) { | ||
$button->value = 'Ok'; | ||
}); | ||
|
||
$button = new Mock\Button; | ||
|
||
$c = 0; | ||
$button->attach($click,function($sender,$e)use(&$c) { | ||
$c++; | ||
}); | ||
|
||
$doOK($button); | ||
|
||
$this->assertEquals(1,$c); | ||
$this->assertEquals('Ok',$button->value); | ||
} | ||
} | ||
} |
Oops, something went wrong.