From 004941c9a395bd09029874dabc49401eb317267b Mon Sep 17 00:00:00 2001 From: Dustin Graham Date: Fri, 20 May 2016 09:56:26 -0700 Subject: [PATCH] Allow a custom loop to be provided to the initialization. --- src/Database.php | 6 ++++-- tests/DatabaseTest.php | 13 +++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Database.php b/src/Database.php index 6ed3f82..9acf41e 100644 --- a/src/Database.php +++ b/src/Database.php @@ -22,14 +22,16 @@ class Database */ protected $pollInterval = 0.01; - public function __construct($credentials = null) + public function __construct($credentials = null, $loop = null) { if (!is_null($credentials)) { ConnectionFactory::init($credentials); } - $this->loop = Factory::create(); + // Use the provided loop, otherwise create one. + $this->loop = $loop ?: Factory::create(); + $this->initLoop(); $this->pool = new ConnectionPool(); diff --git a/tests/DatabaseTest.php b/tests/DatabaseTest.php index d546de6..e23bf90 100644 --- a/tests/DatabaseTest.php +++ b/tests/DatabaseTest.php @@ -2,6 +2,7 @@ use DustinGraham\ReactMysql\Command; use DustinGraham\ReactMysql\Database; +use React\EventLoop\Factory; class DatabaseTest extends TestCase { @@ -29,4 +30,16 @@ public function testForCoverage() { new Database($this->getCredentials()); } + + public function testCustomLoop() + { + // Custom Loop + $loop = Factory::create(); + $database = new Database($this->getCredentials(), $loop); + $this->assertSame($loop, $database->loop); + + // No Custom Loop + $databaseTwo = new Database($this->getCredentials()); + $this->assertNotSame($loop, $databaseTwo->loop); + } }