Skip to content

Commit 004941c

Browse files
committed
Allow a custom loop to be provided to the initialization.
1 parent d665e89 commit 004941c

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/Database.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ class Database
2222
*/
2323
protected $pollInterval = 0.01;
2424

25-
public function __construct($credentials = null)
25+
public function __construct($credentials = null, $loop = null)
2626
{
2727
if (!is_null($credentials))
2828
{
2929
ConnectionFactory::init($credentials);
3030
}
3131

32-
$this->loop = Factory::create();
32+
// Use the provided loop, otherwise create one.
33+
$this->loop = $loop ?: Factory::create();
34+
3335
$this->initLoop();
3436

3537
$this->pool = new ConnectionPool();

tests/DatabaseTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use DustinGraham\ReactMysql\Command;
44
use DustinGraham\ReactMysql\Database;
5+
use React\EventLoop\Factory;
56

67
class DatabaseTest extends TestCase
78
{
@@ -29,4 +30,16 @@ public function testForCoverage()
2930
{
3031
new Database($this->getCredentials());
3132
}
33+
34+
public function testCustomLoop()
35+
{
36+
// Custom Loop
37+
$loop = Factory::create();
38+
$database = new Database($this->getCredentials(), $loop);
39+
$this->assertSame($loop, $database->loop);
40+
41+
// No Custom Loop
42+
$databaseTwo = new Database($this->getCredentials());
43+
$this->assertNotSame($loop, $databaseTwo->loop);
44+
}
3245
}

0 commit comments

Comments
 (0)