ClientTest.php 11 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\Components\BrowserKit;
  11. use Symfony\Components\BrowserKit\Client;
  12. use Symfony\Components\BrowserKit\History;
  13. use Symfony\Components\BrowserKit\CookieJar;
  14. use Symfony\Components\BrowserKit\Request;
  15. use Symfony\Components\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. {
  32. return new Response();
  33. }
  34. else
  35. {
  36. $response = $this->nextResponse;
  37. $this->nextResponse = null;
  38. return $response;
  39. }
  40. }
  41. protected function getScript($request)
  42. {
  43. $r = new \ReflectionClass('Symfony\Components\BrowserKit\Response');
  44. $path = $r->getFileName();
  45. return <<<EOF
  46. <?php
  47. require_once('$path');
  48. echo serialize($this->nextScript);
  49. EOF;
  50. }
  51. }
  52. class ClientTest extends \PHPUnit_Framework_TestCase
  53. {
  54. /**
  55. * @covers Symfony\Components\BrowserKit\Client::getHistory
  56. */
  57. public function testGetHistory()
  58. {
  59. $client = new TestClient(array(), $history = new History());
  60. $this->assertSame($history, $client->getHistory(), '->getHistory() returns the History');
  61. }
  62. /**
  63. * @covers Symfony\Components\BrowserKit\Client::getCookieJar
  64. */
  65. public function testGetCookieJar()
  66. {
  67. $client = new TestClient(array(), null, $cookieJar = new CookieJar());
  68. $this->assertSame($cookieJar, $client->getCookieJar(), '->getCookieJar() returns the CookieJar');
  69. }
  70. /**
  71. * @covers Symfony\Components\BrowserKit\Client::getRequest
  72. */
  73. public function testGetRequest()
  74. {
  75. $client = new TestClient();
  76. $client->request('GET', 'http://example.com/');
  77. $this->assertEquals('http://example.com/', $client->getRequest()->getUri(), '->getCrawler() returns the Request of the last request');
  78. }
  79. /**
  80. * @covers Symfony\Components\BrowserKit\Client::getResponse
  81. */
  82. public function testGetResponse()
  83. {
  84. $client = new TestClient();
  85. $client->setNextResponse(new Response('foo'));
  86. $client->request('GET', 'http://example.com/');
  87. $this->assertEquals('foo', $client->getResponse()->getContent(), '->getCrawler() returns the Response of the last request');
  88. }
  89. /**
  90. * @covers Symfony\Components\BrowserKit\Client::getCrawler
  91. */
  92. public function testGetCrawler()
  93. {
  94. $client = new TestClient();
  95. $client->setNextResponse(new Response('foo'));
  96. $crawler = $client->request('GET', 'http://example.com/');
  97. $this->assertSame($crawler, $client->getCrawler(), '->getCrawler() returns the Crawler of the last request');
  98. }
  99. public function testRequestHttpHeaders()
  100. {
  101. $client = new TestClient();
  102. $client->request('GET', '/');
  103. $headers = $client->getRequest()->getServer();
  104. $this->assertEquals('localhost', $headers['HTTP_HOST'], '->request() sets the HTTP_HOST header');
  105. $client = new TestClient();
  106. $client->request('GET', 'http://www.example.com');
  107. $headers = $client->getRequest()->getServer();
  108. $this->assertEquals('www.example.com', $headers['HTTP_HOST'], '->request() sets the HTTP_HOST header');
  109. $client->request('GET', 'https://www.example.com');
  110. $headers = $client->getRequest()->getServer();
  111. $this->assertTrue($headers['HTTPS'], '->request() sets the HTTPS header');
  112. }
  113. public function testRequestURIConversion()
  114. {
  115. $client = new TestClient();
  116. $client->request('GET', '/foo');
  117. $this->assertEquals('http://localhost/foo', $client->getRequest()->getUri(), '->request() converts the URI to an absolute one');
  118. $client = new TestClient();
  119. $client->request('GET', 'http://www.example.com');
  120. $this->assertEquals('http://www.example.com', $client->getRequest()->getUri(), '->request() does not change absolute URIs');
  121. $client = new TestClient();
  122. $client->request('GET', 'http://www.example.com/');
  123. $client->request('GET', '/foo');
  124. $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->request() uses the previous request for relative URLs');
  125. $client = new TestClient();
  126. $client->request('GET', 'http://www.example.com/foo');
  127. $client->request('GET', '#');
  128. $this->assertEquals('http://www.example.com/foo#', $client->getRequest()->getUri(), '->request() uses the previous request for #');
  129. $client->request('GET', '#');
  130. $this->assertEquals('http://www.example.com/foo#', $client->getRequest()->getUri(), '->request() uses the previous request for #');
  131. $client->request('GET', '#foo');
  132. $this->assertEquals('http://www.example.com/foo#foo', $client->getRequest()->getUri(), '->request() uses the previous request for #');
  133. $client = new TestClient();
  134. $client->request('GET', 'http://www.example.com/foo/');
  135. $client->request('GET', 'bar');
  136. $this->assertEquals('http://www.example.com/foo/bar', $client->getRequest()->getUri(), '->request() uses the previous request for relative URLs');
  137. $client = new TestClient();
  138. $client->request('GET', 'http://www.example.com/foo/foobar');
  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. }
  142. public function testRequestReferer()
  143. {
  144. $client = new TestClient();
  145. $client->request('GET', 'http://www.example.com/foo/foobar');
  146. $client->request('GET', 'bar');
  147. $server = $client->getRequest()->getServer();
  148. $this->assertEquals('http://www.example.com/foo/foobar', $server['HTTP_REFERER'], '->request() sets the referer');
  149. }
  150. public function testRequestHistory()
  151. {
  152. $client = new TestClient();
  153. $client->request('GET', 'http://www.example.com/foo/foobar');
  154. $client->request('GET', 'bar');
  155. $this->assertEquals('http://www.example.com/foo/bar', $client->getHistory()->current()->getUri(), '->request() updates the History');
  156. $this->assertEquals('http://www.example.com/foo/foobar', $client->getHistory()->back()->getUri(), '->request() updates the History');
  157. }
  158. public function testRequestCookies()
  159. {
  160. $client = new TestClient();
  161. $client->setNextResponse(new Response('<html><a href="/foo">foo</a></html>', 200, array(), array('foo' => array('value' => 'bar'))));
  162. $client->request('GET', 'http://www.example.com/foo/foobar');
  163. $this->assertEquals(array('foo' => 'bar'), $client->getCookieJar()->getValues('http://www.example.com/foo/foobar'), '->request() updates the CookieJar');
  164. $client->request('GET', 'bar');
  165. $this->assertEquals(array('foo' => 'bar'), $client->getCookieJar()->getValues('http://www.example.com/foo/foobar'), '->request() updates the CookieJar');
  166. }
  167. public function testClick()
  168. {
  169. $client = new TestClient();
  170. $client->setNextResponse(new Response('<html><a href="/foo">foo</a></html>'));
  171. $crawler = $client->request('GET', 'http://www.example.com/foo/foobar');
  172. $client->click($crawler->filter('a')->link());
  173. $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->click() clicks on links');
  174. }
  175. public function testSubmit()
  176. {
  177. $client = new TestClient();
  178. $client->setNextResponse(new Response('<html><form action="/foo"><input type="submit" /></form></html>'));
  179. $crawler = $client->request('GET', 'http://www.example.com/foo/foobar');
  180. $client->submit($crawler->filter('input')->form());
  181. $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->submit() submit forms');
  182. }
  183. public function testFollowRedirect()
  184. {
  185. $client = new TestClient();
  186. $client->followRedirects(false);
  187. $client->request('GET', 'http://www.example.com/foo/foobar');
  188. try
  189. {
  190. $client->followRedirect();
  191. $this->fail('->followRedirect() throws a \LogicException if the request was not redirected');
  192. }
  193. catch (\Exception $e)
  194. {
  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\Components\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\Components\BrowserKit\Response('foobar)");
  246. try
  247. {
  248. $client->request('GET', 'http://www.example.com/foo/foobar');
  249. $this->fail('->request() throws a \RuntimeException if the script has an error');
  250. }
  251. catch (\Exception $e)
  252. {
  253. $this->assertInstanceof('RuntimeException', $e, '->request() throws a \RuntimeException if the script has an error');
  254. }
  255. }
  256. }