Skip to content

Commit c72d470

Browse files
committed
Support URI scheme to create connection
1 parent 648174d commit c72d470

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Factory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,11 @@ public function __construct(LoopInterface $loop = null, ConnectorInterface $conn
158158
*/
159159
public function createConnection($uri)
160160
{
161-
$parts = parse_url('mysql://' . $uri);
161+
if (strpos($uri, '://') === false) {
162+
$uri = 'mysql://' . $uri;
163+
}
164+
165+
$parts = parse_url($uri);
162166
if (!isset($parts['scheme'], $parts['host']) || $parts['scheme'] !== 'mysql') {
163167
return \React\Promise\reject(new \InvalidArgumentException('Invalid connect uri given'));
164168
}

tests/FactoryTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,29 @@ public function testConnectWillUseHostAndDefaultPort()
3232
$factory->createConnection('127.0.0.1');
3333
}
3434

35+
public function testConnectWillUseGivenScheme()
36+
{
37+
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
38+
$pending = $this->getMockBuilder('React\Promise\PromiseInterface')->getMock();
39+
$connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
40+
$connector->expects($this->once())->method('connect')->with('127.0.0.1:3306')->willReturn($pending);
41+
42+
$factory = new Factory($loop, $connector);
43+
$factory->createConnection('mysql://127.0.0.1');
44+
}
45+
46+
public function testConnectWillRejectWhenGivenInvalidScheme()
47+
{
48+
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
49+
$connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
50+
51+
$factory = new Factory($loop, $connector);
52+
53+
$promise = $factory->createConnection('foo://127.0.0.1');
54+
55+
$promise->then(null, $this->expectCallableOnceWith($this->isInstanceOf('InvalidArgumentException')));
56+
}
57+
3558
public function testConnectWillUseGivenHostAndGivenPort()
3659
{
3760
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();

0 commit comments

Comments
 (0)