Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from wenbinye/fix-onetimelistener
Fix PrioritizedListenersForEvent when OneTimeListener added
- Loading branch information
Showing
2 changed files
with
44 additions
and
1 deletion.
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
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,42 @@ | ||
<?php | ||
|
||
namespace League\Event; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class PrioritizedListenersForEventTest extends TestCase | ||
{ | ||
public function testOneTimeListener(): void | ||
{ | ||
$group = new PrioritizedListenersForEvent(); | ||
$group->addListener(function () { | ||
return 1; | ||
}, 1); | ||
$group->addListener(function () { | ||
return 2; | ||
}, 2); | ||
$group->addListener(new OneTimeListener(function () { | ||
return 3; | ||
}), 3); | ||
$listener1 = $group->getListeners(); | ||
$this->assertCount(3, $listener1); | ||
$this->assertEquals([null, 2, 1], self::inspectListener($listener1)); | ||
|
||
$group->addListener(function () { | ||
return 4; | ||
}, 4); | ||
$listener2 = $group->getListeners(); | ||
$this->assertCount(3, $listener2); | ||
$this->assertEquals([4, 2, 1], self::inspectListener($listener2)); | ||
} | ||
|
||
public static function inspectListener(iterable $listeners): array | ||
{ | ||
$event = new \stdClass(); | ||
$ret = []; | ||
foreach ($listeners as $listener) { | ||
$ret[] = $listener($event); | ||
} | ||
return $ret; | ||
} | ||
} |