Skip to content

Commit

Permalink
Allow a custom loop to be provided to the initialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
dustingraham committed May 20, 2016
1 parent d665e89 commit 004941c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 13 additions & 0 deletions tests/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use DustinGraham\ReactMysql\Command;
use DustinGraham\ReactMysql\Database;
use React\EventLoop\Factory;

class DatabaseTest extends TestCase
{
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 004941c

Please sign in to comment.