Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Browser::withSender() method #38

Merged
merged 2 commits into from
Jul 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,32 @@ The `request($method, $url, $headers = array(), $content = '')` method can be us

The `send(Request $request)` method can be used to send an arbitrary [`Request` object](#request).

#### withOptions()

The `withOptions(array $options)` method can be used to change the [options](#options) to use:

```php
$newBrowser = $browser->withOptions($options);
```

Notice that the [`Browser`](#browser) is an immutable object, i.e. the `withOptions()` method
actually returns a *new* [`Browser`](#browser) instance with the [options](#options) applied.

See [options](#options) for more details.

#### withSender()

The `withSender(Sender $sender)` method can be used to change the [`Sender`](#sender) instance to use:

```php
$newBrowser = $browser->withSender($sender);
```

Notice that the [`Browser`](#browser) is an immutable object, i.e. the `withSender()` method
actually returns a *new* [`Browser`](#browser) instance with the given [`Sender`](#sender) applied.

See [`Sender`](#sender) for more details.

### Message

The `Message` is an abstract base class for the [`Response`](#response) and [`Request`](#request).
Expand Down Expand Up @@ -139,6 +165,8 @@ and keeps track of its transmission and converts its reponses back to [`Response
It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage)
and the default [`Connector`](https://github.com/reactphp/socket-client) and [DNS `Resolver`](https://github.com/reactphp/dns).

See also [`Browser::withSender()`](#withsender) for changing the `Sender` instance during runtime.

### SOCKS proxy

You can also establish your outgoing connections through a SOCKS proxy server
Expand Down
8 changes: 8 additions & 0 deletions src/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,12 @@ public function withOptions(array $options)

return $browser;
}

public function withSender(Sender $sender)
{
$browser = clone $this;
$browser->sender = $sender;

return $browser;
}
}
27 changes: 27 additions & 0 deletions tests/BrowserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Clue\React\Buzz\Browser;
use React\Promise\Deferred;

class BrowserTest extends TestCase
{
private $loop;
private $sender;
private $browser;

public function setUp()
{
$this->loop = $this->getMock('React\EventLoop\LoopInterface');
$this->sender = $this->getMockBuilder('Clue\React\Buzz\Io\Sender')->disableOriginalConstructor()->getMock();
$this->browser = new Browser($this->loop, $this->sender);
}

public function testWithSender()
{
$sender = $this->getMockBuilder('Clue\React\Buzz\Io\Sender')->disableOriginalConstructor()->getMock();

$browser = $this->browser->withSender($sender);

$this->assertNotSame($this->browser, $browser);
}
}