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); + } }