Skip to content

Commit 2fa27cf

Browse files
committed
DI for Browser and only exchange its Sender instance
1 parent a840421 commit 2fa27cf

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

src/Factory.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@
1111
class Factory
1212
{
1313
private $loop;
14+
private $browser;
1415

15-
public function __construct(LoopInterface $loop)
16+
public function __construct(LoopInterface $loop, Browser $browser = null)
1617
{
18+
if ($browser === null) {
19+
$browser = new Browser($loop);
20+
}
21+
1722
$this->loop = $loop;
23+
$this->browser = $browser;
1824
}
1925

2026
public function createClient($url = null)
@@ -23,18 +29,17 @@ public function createClient($url = null)
2329
$url = 'unix:///var/run/docker.sock';
2430
}
2531

26-
$sender = null;
32+
$browser = $this->browser;
2733

2834
if (substr($url, 0, 7) === 'unix://') {
2935
// send everything through a local unix domain socket
3036
$sender = Sender::createFromLoopUnix($this->loop, $url);
37+
$browser = $browser->withSender($sender);
3138

3239
// pretend all HTTP URLs to be on localhost
3340
$url = 'http://localhost';
3441
}
3542

36-
$browser = new Browser($this->loop, $sender);
37-
3843
return new Client($browser, $url);
3944
}
4045
}

tests/FactoryTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,34 @@
66
class FactoryTest extends TestCase
77
{
88
private $loop;
9+
private $browser;
910
private $factory;
1011

1112
public function setUp()
1213
{
1314
$this->loop = LoopFactory::create();
14-
$this->factory = new Factory($this->loop);
15+
$this->browser = $this->getMockBuilder('Clue\React\Buzz\Browser')->disableOriginalConstructor()->getMock();
16+
$this->factory = new Factory($this->loop, $this->browser);
1517
}
1618

17-
public function testCreateClientDefault()
19+
public function testCtorDefaultBrowser()
1820
{
21+
$factory = new Factory($this->loop);
22+
}
23+
24+
public function testCreateClientUsesCustomUnixSender()
25+
{
26+
$this->browser->expects($this->once())->method('withSender')->will($this->returnValue($this->browser));
27+
1928
$client = $this->factory->createClient();
2029

2130
$this->assertInstanceOf('Clue\React\Docker\Client', $client);
2231
}
32+
33+
public function testCreateClientWithHttp()
34+
{
35+
$this->browser->expects($this->never())->method('withSender');
36+
37+
$this->factory->createClient('http://localhost:1234/');
38+
}
2339
}

0 commit comments

Comments
 (0)