ClientTest.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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('', 302, 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('', 302, 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 testFollowRedirectWithCookies()
  207. {
  208. $client = new TestClient();
  209. $client->followRedirects(false);
  210. $client->setNextResponse(new Response('', 302, array(
  211. 'Location' => 'http://www.example.com/redirected',
  212. 'Set-Cookie' => 'foo=bar',
  213. )));
  214. $client->request('GET', 'http://www.example.com/');
  215. $this->assertEquals(array(), $client->getRequest()->getCookies());
  216. $client->followRedirect();
  217. $this->assertEquals(array('foo' => 'bar'), $client->getRequest()->getCookies());
  218. }
  219. public function testBack()
  220. {
  221. $client = new TestClient();
  222. $parameters = array('foo' => 'bar');
  223. $files = array('myfile.foo' => 'baz');
  224. $server = array('X_TEST_FOO' => 'bazbar');
  225. $content = 'foobarbaz';
  226. $client->request('GET', 'http://www.example.com/foo/foobar', $parameters, $files, $server, $content);
  227. $client->request('GET', 'http://www.example.com/foo');
  228. $client->back();
  229. $this->assertEquals('http://www.example.com/foo/foobar', $client->getRequest()->getUri(), '->back() goes back in the history');
  230. $this->assertArrayHasKey('foo', $client->getRequest()->getParameters(), '->back() keeps parameters');
  231. $this->assertArrayHasKey('myfile.foo', $client->getRequest()->getFiles(), '->back() keeps files');
  232. $this->assertArrayHasKey('X_TEST_FOO', $client->getRequest()->getServer(), '->back() keeps $_SERVER');
  233. $this->assertEquals($content, $client->getRequest()->getContent(), '->back() keeps content');
  234. }
  235. public function testForward()
  236. {
  237. $client = new TestClient();
  238. $parameters = array('foo' => 'bar');
  239. $files = array('myfile.foo' => 'baz');
  240. $server = array('X_TEST_FOO' => 'bazbar');
  241. $content = 'foobarbaz';
  242. $client->request('GET', 'http://www.example.com/foo/foobar');
  243. $client->request('GET', 'http://www.example.com/foo', $parameters, $files, $server, $content);
  244. $client->back();
  245. $client->forward();
  246. $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->forward() goes forward in the history');
  247. $this->assertArrayHasKey('foo', $client->getRequest()->getParameters(), '->forward() keeps parameters');
  248. $this->assertArrayHasKey('myfile.foo', $client->getRequest()->getFiles(), '->forward() keeps files');
  249. $this->assertArrayHasKey('X_TEST_FOO', $client->getRequest()->getServer(), '->forward() keeps $_SERVER');
  250. $this->assertEquals($content, $client->getRequest()->getContent(), '->forward() keeps content');
  251. }
  252. public function testReload()
  253. {
  254. $client = new TestClient();
  255. $parameters = array('foo' => 'bar');
  256. $files = array('myfile.foo' => 'baz');
  257. $server = array('X_TEST_FOO' => 'bazbar');
  258. $content = 'foobarbaz';
  259. $client->request('GET', 'http://www.example.com/foo/foobar', $parameters, $files, $server, $content);
  260. $client->reload();
  261. $this->assertEquals('http://www.example.com/foo/foobar', $client->getRequest()->getUri(), '->reload() reloads the current page');
  262. $this->assertArrayHasKey('foo', $client->getRequest()->getParameters(), '->reload() keeps parameters');
  263. $this->assertArrayHasKey('myfile.foo', $client->getRequest()->getFiles(), '->reload() keeps files');
  264. $this->assertArrayHasKey('X_TEST_FOO', $client->getRequest()->getServer(), '->reload() keeps $_SERVER');
  265. $this->assertEquals($content, $client->getRequest()->getContent(), '->reload() keeps content');
  266. }
  267. public function testRestart()
  268. {
  269. $client = new TestClient();
  270. $client->request('GET', 'http://www.example.com/foo/foobar');
  271. $client->restart();
  272. $this->assertTrue($client->getHistory()->isEmpty(), '->restart() clears the history');
  273. $this->assertEquals(array(), $client->getCookieJar()->all(), '->restart() clears the cookies');
  274. }
  275. public function testInsulatedRequests()
  276. {
  277. $client = new TestClient();
  278. $client->insulate();
  279. $client->setNextScript("new Symfony\Component\BrowserKit\Response('foobar')");
  280. $client->request('GET', 'http://www.example.com/foo/foobar');
  281. $this->assertEquals('foobar', $client->getResponse()->getContent(), '->insulate() process the request in a forked process');
  282. $client->setNextScript("new Symfony\Component\BrowserKit\Response('foobar)");
  283. try {
  284. $client->request('GET', 'http://www.example.com/foo/foobar');
  285. $this->fail('->request() throws a \RuntimeException if the script has an error');
  286. } catch (\Exception $e) {
  287. $this->assertInstanceof('RuntimeException', $e, '->request() throws a \RuntimeException if the script has an error');
  288. }
  289. }
  290. public function testGetServerParameter()
  291. {
  292. $client = new TestClient();
  293. $this->assertEquals('localhost', $client->getServerParameter('HTTP_HOST'));
  294. $this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
  295. $this->assertEquals('testvalue', $client->getServerParameter('testkey', 'testvalue'));
  296. }
  297. public function testSetServerParameter()
  298. {
  299. $client = new TestClient();
  300. $this->assertEquals('localhost', $client->getServerParameter('HTTP_HOST'));
  301. $this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
  302. $client->setServerParameter('HTTP_HOST', 'testhost');
  303. $this->assertEquals('testhost', $client->getServerParameter('HTTP_HOST'));
  304. $client->setServerParameter('HTTP_USER_AGENT', 'testua');
  305. $this->assertEquals('testua', $client->getServerParameter('HTTP_USER_AGENT'));
  306. }
  307. }