ClientTest.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\BrowserKit;
  11. use Symfony\Component\BrowserKit\Client;
  12. use Symfony\Component\BrowserKit\History;
  13. use Symfony\Component\BrowserKit\CookieJar;
  14. use Symfony\Component\BrowserKit\Request;
  15. use Symfony\Component\BrowserKit\Response;
  16. class TestClient extends Client
  17. {
  18. protected $nextResponse = null;
  19. protected $nextScript = null;
  20. public function setNextResponse(Response $response)
  21. {
  22. $this->nextResponse = $response;
  23. }
  24. public function setNextScript($script)
  25. {
  26. $this->nextScript = $script;
  27. }
  28. protected function doRequest($request)
  29. {
  30. if (null === $this->nextResponse) {
  31. return new Response();
  32. }
  33. $response = $this->nextResponse;
  34. $this->nextResponse = null;
  35. return $response;
  36. }
  37. protected function getScript($request)
  38. {
  39. $r = new \ReflectionClass('Symfony\Component\BrowserKit\Response');
  40. $path = $r->getFileName();
  41. return <<<EOF
  42. <?php
  43. require_once('$path');
  44. echo serialize($this->nextScript);
  45. EOF;
  46. }
  47. }
  48. class ClientTest extends \PHPUnit_Framework_TestCase
  49. {
  50. /**
  51. * @covers Symfony\Component\BrowserKit\Client::getHistory
  52. */
  53. public function testGetHistory()
  54. {
  55. $client = new TestClient(array(), $history = new History());
  56. $this->assertSame($history, $client->getHistory(), '->getHistory() returns the History');
  57. }
  58. /**
  59. * @covers Symfony\Component\BrowserKit\Client::getCookieJar
  60. */
  61. public function testGetCookieJar()
  62. {
  63. $client = new TestClient(array(), null, $cookieJar = new CookieJar());
  64. $this->assertSame($cookieJar, $client->getCookieJar(), '->getCookieJar() returns the CookieJar');
  65. }
  66. /**
  67. * @covers Symfony\Component\BrowserKit\Client::getRequest
  68. */
  69. public function testGetRequest()
  70. {
  71. $client = new TestClient();
  72. $client->request('GET', 'http://example.com/');
  73. $this->assertEquals('http://example.com/', $client->getRequest()->getUri(), '->getCrawler() returns the Request of the last request');
  74. }
  75. /**
  76. * @covers Symfony\Component\BrowserKit\Client::getResponse
  77. */
  78. public function testGetResponse()
  79. {
  80. $client = new TestClient();
  81. $client->setNextResponse(new Response('foo'));
  82. $client->request('GET', 'http://example.com/');
  83. $this->assertEquals('foo', $client->getResponse()->getContent(), '->getCrawler() returns the Response of the last request');
  84. }
  85. public function testGetContent()
  86. {
  87. $json = '{"jsonrpc":"2.0","method":"echo","id":7,"params":["Hello World"]}';
  88. $client = new TestClient();
  89. $client->request('POST', 'http://example.com/jsonrpc', array(), array(), array(), $json);
  90. $this->assertEquals($json, $client->getRequest()->getContent());
  91. }
  92. /**
  93. * @covers Symfony\Component\BrowserKit\Client::getCrawler
  94. */
  95. public function testGetCrawler()
  96. {
  97. $client = new TestClient();
  98. $client->setNextResponse(new Response('foo'));
  99. $crawler = $client->request('GET', 'http://example.com/');
  100. $this->assertSame($crawler, $client->getCrawler(), '->getCrawler() returns the Crawler of the last request');
  101. }
  102. public function testRequestHttpHeaders()
  103. {
  104. $client = new TestClient();
  105. $client->request('GET', '/');
  106. $headers = $client->getRequest()->getServer();
  107. $this->assertEquals('localhost', $headers['HTTP_HOST'], '->request() sets the HTTP_HOST header');
  108. $client = new TestClient();
  109. $client->request('GET', 'http://www.example.com');
  110. $headers = $client->getRequest()->getServer();
  111. $this->assertEquals('www.example.com', $headers['HTTP_HOST'], '->request() sets the HTTP_HOST header');
  112. $client->request('GET', 'https://www.example.com');
  113. $headers = $client->getRequest()->getServer();
  114. $this->assertTrue($headers['HTTPS'], '->request() sets the HTTPS header');
  115. }
  116. public function testRequestURIConversion()
  117. {
  118. $client = new TestClient();
  119. $client->request('GET', '/foo');
  120. $this->assertEquals('http://localhost/foo', $client->getRequest()->getUri(), '->request() converts the URI to an absolute one');
  121. $client = new TestClient();
  122. $client->request('GET', 'http://www.example.com');
  123. $this->assertEquals('http://www.example.com', $client->getRequest()->getUri(), '->request() does not change absolute URIs');
  124. $client = new TestClient();
  125. $client->request('GET', 'http://www.example.com/');
  126. $client->request('GET', '/foo');
  127. $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->request() uses the previous request for relative URLs');
  128. $client = new TestClient();
  129. $client->request('GET', 'http://www.example.com/foo');
  130. $client->request('GET', '#');
  131. $this->assertEquals('http://www.example.com/foo#', $client->getRequest()->getUri(), '->request() uses the previous request for #');
  132. $client->request('GET', '#');
  133. $this->assertEquals('http://www.example.com/foo#', $client->getRequest()->getUri(), '->request() uses the previous request for #');
  134. $client->request('GET', '#foo');
  135. $this->assertEquals('http://www.example.com/foo#foo', $client->getRequest()->getUri(), '->request() uses the previous request for #');
  136. $client = new TestClient();
  137. $client->request('GET', 'http://www.example.com/foo/');
  138. $client->request('GET', 'bar');
  139. $this->assertEquals('http://www.example.com/foo/bar', $client->getRequest()->getUri(), '->request() uses the previous request for relative URLs');
  140. $client = new TestClient();
  141. $client->request('GET', 'http://www.example.com/foo/foobar');
  142. $client->request('GET', 'bar');
  143. $this->assertEquals('http://www.example.com/foo/bar', $client->getRequest()->getUri(), '->request() uses the previous request for relative URLs');
  144. }
  145. public function testRequestReferer()
  146. {
  147. $client = new TestClient();
  148. $client->request('GET', 'http://www.example.com/foo/foobar');
  149. $client->request('GET', 'bar');
  150. $server = $client->getRequest()->getServer();
  151. $this->assertEquals('http://www.example.com/foo/foobar', $server['HTTP_REFERER'], '->request() sets the referer');
  152. }
  153. public function testRequestHistory()
  154. {
  155. $client = new TestClient();
  156. $client->request('GET', 'http://www.example.com/foo/foobar');
  157. $client->request('GET', 'bar');
  158. $this->assertEquals('http://www.example.com/foo/bar', $client->getHistory()->current()->getUri(), '->request() updates the History');
  159. $this->assertEquals('http://www.example.com/foo/foobar', $client->getHistory()->back()->getUri(), '->request() updates the History');
  160. }
  161. public function testRequestCookies()
  162. {
  163. $client = new TestClient();
  164. $client->setNextResponse(new Response('<html><a href="/foo">foo</a></html>', 200, array('Set-Cookie' => 'foo=bar')));
  165. $client->request('GET', 'http://www.example.com/foo/foobar');
  166. $this->assertEquals(array('foo' => 'bar'), $client->getCookieJar()->allValues('http://www.example.com/foo/foobar'), '->request() updates the CookieJar');
  167. $client->request('GET', 'bar');
  168. $this->assertEquals(array('foo' => 'bar'), $client->getCookieJar()->allValues('http://www.example.com/foo/foobar'), '->request() updates the CookieJar');
  169. }
  170. public function testClick()
  171. {
  172. $client = new TestClient();
  173. $client->setNextResponse(new Response('<html><a href="/foo">foo</a></html>'));
  174. $crawler = $client->request('GET', 'http://www.example.com/foo/foobar');
  175. $client->click($crawler->filter('a')->link());
  176. $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->click() clicks on links');
  177. }
  178. public function testSubmit()
  179. {
  180. $client = new TestClient();
  181. $client->setNextResponse(new Response('<html><form action="/foo"><input type="submit" /></form></html>'));
  182. $crawler = $client->request('GET', 'http://www.example.com/foo/foobar');
  183. $client->submit($crawler->filter('input')->form());
  184. $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->submit() submit forms');
  185. }
  186. public function testFollowRedirect()
  187. {
  188. $client = new TestClient();
  189. $client->followRedirects(false);
  190. $client->request('GET', 'http://www.example.com/foo/foobar');
  191. try {
  192. $client->followRedirect();
  193. $this->fail('->followRedirect() throws a \LogicException if the request was not redirected');
  194. } catch (\Exception $e) {
  195. $this->assertInstanceof('LogicException', $e, '->followRedirect() throws a \LogicException if the request was not redirected');
  196. }
  197. $client->setNextResponse(new Response('', 200, array('Location' => 'http://www.example.com/redirected')));
  198. $client->request('GET', 'http://www.example.com/foo/foobar');
  199. $client->followRedirect();
  200. $this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() follows a redirect if any');
  201. $client = new TestClient();
  202. $client->setNextResponse(new Response('', 200, array('Location' => 'http://www.example.com/redirected')));
  203. $client->request('GET', 'http://www.example.com/foo/foobar');
  204. $this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() automatically follows redirects if followRedirects is true');
  205. }
  206. public function testBack()
  207. {
  208. $client = new TestClient();
  209. $client->request('GET', 'http://www.example.com/foo/foobar');
  210. $client->request('GET', 'http://www.example.com/foo');
  211. $client->back();
  212. $this->assertEquals('http://www.example.com/foo/foobar', $client->getRequest()->getUri(), '->back() goes back in the history');
  213. }
  214. public function testForward()
  215. {
  216. $client = new TestClient();
  217. $client->request('GET', 'http://www.example.com/foo/foobar');
  218. $client->request('GET', 'http://www.example.com/foo');
  219. $client->back();
  220. $client->forward();
  221. $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->forward() goes forward in the history');
  222. }
  223. public function testReload()
  224. {
  225. $client = new TestClient();
  226. $client->request('GET', 'http://www.example.com/foo/foobar');
  227. $client->reload();
  228. $this->assertEquals('http://www.example.com/foo/foobar', $client->getRequest()->getUri(), '->forward() reloads the current page');
  229. }
  230. public function testRestart()
  231. {
  232. $client = new TestClient();
  233. $client->request('GET', 'http://www.example.com/foo/foobar');
  234. $client->restart();
  235. $this->assertTrue($client->getHistory()->isEmpty(), '->restart() clears the history');
  236. $this->assertEquals(array(), $client->getCookieJar()->all(), '->restart() clears the cookies');
  237. }
  238. public function testInsulatedRequests()
  239. {
  240. $client = new TestClient();
  241. $client->insulate();
  242. $client->setNextScript("new Symfony\Component\BrowserKit\Response('foobar')");
  243. $client->request('GET', 'http://www.example.com/foo/foobar');
  244. $this->assertEquals('foobar', $client->getResponse()->getContent(), '->insulate() process the request in a forked process');
  245. $client->setNextScript("new Symfony\Component\BrowserKit\Response('foobar)");
  246. try {
  247. $client->request('GET', 'http://www.example.com/foo/foobar');
  248. $this->fail('->request() throws a \RuntimeException if the script has an error');
  249. } catch (\Exception $e) {
  250. $this->assertInstanceof('RuntimeException', $e, '->request() throws a \RuntimeException if the script has an error');
  251. }
  252. }
  253. public function testGetServerParameter()
  254. {
  255. $client = new TestClient();
  256. $this->assertEquals('localhost', $client->getServerParameter('HTTP_HOST'));
  257. $this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
  258. $this->assertEquals('testvalue', $client->getServerParameter('testkey', 'testvalue'));
  259. }
  260. public function testSetServerParameter()
  261. {
  262. $client = new TestClient();
  263. $this->assertEquals('localhost', $client->getServerParameter('HTTP_HOST'));
  264. $this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
  265. $client->setServerParameter('HTTP_HOST', 'testhost');
  266. $this->assertEquals('testhost', $client->getServerParameter('HTTP_HOST'));
  267. $client->setServerParameter('HTTP_USER_AGENT', 'testua');
  268. $this->assertEquals('testua', $client->getServerParameter('HTTP_USER_AGENT'));
  269. }
  270. }