ClientTest.php 12 KB

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