From a016dc324c01fca3c8a921d641ab251c43fa4e18 Mon Sep 17 00:00:00 2001 From: Soufiane Ghzal Date: Mon, 1 May 2017 15:06:57 +0200 Subject: [PATCH] browser have setters --- src/Core/Browser/Browser.php | 33 ++++++++++++++++++ test/suites/TDD/Core/Browser/BrowserTest.php | 36 ++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/Core/Browser/Browser.php b/src/Core/Browser/Browser.php index 7690529..410d90f 100644 --- a/src/Core/Browser/Browser.php +++ b/src/Core/Browser/Browser.php @@ -6,6 +6,7 @@ namespace Serps\Core\Browser; use Psr\Http\Message\RequestInterface; +use Serps\Core\Cookie\Cookie; use Serps\Core\Cookie\CookieJarInterface; use Serps\Core\Http\HttpClientInterface; use Serps\Core\Http\ProxyInterface; @@ -92,4 +93,36 @@ public function getHttpClient() { return $this->httpClient; } + + /** + * @param null|string $acceptLanguage + */ + public function setAcceptLanguage($acceptLanguage) + { + $this->acceptLanguage = $acceptLanguage; + } + + /** + * @param null|string $userAgent + */ + public function setUserAgent($userAgent) + { + $this->userAgent = $userAgent; + } + + /** + * @param null|CookieJarInterface $cookieJar + */ + public function setCookieJar(CookieJarInterface $cookieJar = null) + { + $this->cookieJar = $cookieJar; + } + + /** + * @param null|ProxyInterface $proxy + */ + public function setProxy(ProxyInterface $proxy = null) + { + $this->proxy = $proxy; + } } diff --git a/test/suites/TDD/Core/Browser/BrowserTest.php b/test/suites/TDD/Core/Browser/BrowserTest.php index 63b9121..b77b460 100644 --- a/test/suites/TDD/Core/Browser/BrowserTest.php +++ b/test/suites/TDD/Core/Browser/BrowserTest.php @@ -5,6 +5,8 @@ namespace Serps\Test\Core\Browser; +use Serps\Core\Cookie\ArrayCookieJar; +use Serps\Core\Http\Proxy; use Serps\Core\Http\StackingHttpClient; use Serps\Core\Browser\Browser; use Serps\Core\Psr7\RequestBuilder; @@ -90,4 +92,38 @@ public function testSendRequest() $this->assertEquals('POST', strtoupper($httpClient->getStack()[0]->request->getMethod())); $this->assertEquals('a=b', (string) $httpClient->getStack()[0]->request->getBody()); } + + public function testAccessors() + { + + $userAgent= 'bar'; + $language = 'foo'; + $cookieJar = new ArrayCookieJar(); + $proxy = Proxy::createFromString('localhost:80'); + + $browser = new Browser(new StackingHttpClient(), $userAgent, $language, $cookieJar, $proxy); + + $this->assertEquals('foo', $browser->getAcceptLanguage()); + $this->assertEquals('bar', $browser->getUserAgent()); + $this->assertSame($cookieJar, $browser->getCookieJar()); + $this->assertSame($proxy, $browser->getProxy()); + + + $cookieJar2 = new ArrayCookieJar(); + $proxy2 = Proxy::createFromString('localhost:90'); + $browser->setAcceptLanguage('baz'); + $browser->setUserAgent('qux'); + $browser->setCookieJar($cookieJar2); + $browser->setProxy($proxy2); + + $this->assertEquals('baz', $browser->getAcceptLanguage()); + $this->assertEquals('qux', $browser->getUserAgent()); + $this->assertSame($cookieJar2, $browser->getCookieJar()); + $this->assertSame($proxy2, $browser->getProxy()); + + $browser->setCookieJar(null); + $browser->setProxy(null); + $this->assertNull($browser->getCookieJar()); + $this->assertNull($browser->getProxy()); + } }