ClientTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. 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\Components\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\Components\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\Components\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\Components\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\Components\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. /**
  87. * @covers Symfony\Components\BrowserKit\Client::getCrawler
  88. */
  89. public function testGetCrawler()
  90. {
  91. $client = new TestClient();
  92. $client->setNextResponse(new Response('foo'));
  93. $crawler = $client->request('GET', 'http://example.com/');
  94. $this->assertSame($crawler, $client->getCrawler(), '->getCrawler() returns the Crawler of the last request');
  95. }
  96. public function testRequestHttpHeaders()
  97. {
  98. $client = new TestClient();
  99. $client->request('GET', '/');
  100. $headers = $client->getRequest()->getServer();
  101. $this->assertEquals('localhost', $headers['HTTP_HOST'], '->request() sets the HTTP_HOST header');
  102. $client = new TestClient();
  103. $client->request('GET', 'http://www.example.com');
  104. $headers = $client->getRequest()->getServer();
  105. $this->assertEquals('www.example.com', $headers['HTTP_HOST'], '->request() sets the HTTP_HOST header');
  106. $client->request('GET', 'https://www.example.com');
  107. $headers = $client->getRequest()->getServer();
  108. $this->assertTrue($headers['HTTPS'], '->request() sets the HTTPS header');
  109. }
  110. public function testRequestURIConversion()
  111. {
  112. $client = new TestClient();
  113. $client->request('GET', '/foo');
  114. $this->assertEquals('http://localhost/foo', $client->getRequest()->getUri(), '->request() converts the URI to an absolute one');
  115. $client = new TestClient();
  116. $client->request('GET', 'http://www.example.com');
  117. $this->assertEquals('http://www.example.com', $client->getRequest()->getUri(), '->request() does not change absolute URIs');
  118. $client = new TestClient();
  119. $client->request('GET', 'http://www.example.com/');
  120. $client->request('GET', '/foo');
  121. $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->request() uses the previous request for relative URLs');
  122. $client = new TestClient();
  123. $client->request('GET', 'http://www.example.com/foo');
  124. $client->request('GET', '#');
  125. $this->assertEquals('http://www.example.com/foo#', $client->getRequest()->getUri(), '->request() uses the previous request for #');
  126. $client->request('GET', '#');
  127. $this->assertEquals('http://www.example.com/foo#', $client->getRequest()->getUri(), '->request() uses the previous request for #');
  128. $client->request('GET', '#foo');
  129. $this->assertEquals('http://www.example.com/foo#foo', $client->getRequest()->getUri(), '->request() uses the previous request for #');
  130. $client = new TestClient();
  131. $client->request('GET', 'http://www.example.com/foo/');
  132. $client->request('GET', 'bar');
  133. $this->assertEquals('http://www.example.com/foo/bar', $client->getRequest()->getUri(), '->request() uses the previous request for relative URLs');
  134. $client = new TestClient();
  135. $client->request('GET', 'http://www.example.com/foo/foobar');
  136. $client->request('GET', 'bar');
  137. $this->assertEquals('http://www.example.com/foo/bar', $client->getRequest()->getUri(), '->request() uses the previous request for relative URLs');
  138. }
  139. public function testRequestReferer()
  140. {
  141. $client = new TestClient();
  142. $client->request('GET', 'http://www.example.com/foo/foobar');
  143. $client->request('GET', 'bar');
  144. $server = $client->getRequest()->getServer();
  145. $this->assertEquals('http://www.example.com/foo/foobar', $server['HTTP_REFERER'], '->request() sets the referer');
  146. }
  147. public function testRequestHistory()
  148. {
  149. $client = new TestClient();
  150. $client->request('GET', 'http://www.example.com/foo/foobar');
  151. $client->request('GET', 'bar');
  152. $this->assertEquals('http://www.example.com/foo/bar', $client->getHistory()->current()->getUri(), '->request() updates the History');
  153. $this->assertEquals('http://www.example.com/foo/foobar', $client->getHistory()->back()->getUri(), '->request() updates the History');
  154. }
  155. public function testRequestCookies()
  156. {
  157. $client = new TestClient();
  158. $client->setNextResponse(new Response('<html><a href="/foo">foo</a></html>', 200, array(), array('foo' => array('value' => 'bar'))));
  159. $client->request('GET', 'http://www.example.com/foo/foobar');
  160. $this->assertEquals(array('foo' => 'bar'), $client->getCookieJar()->getValues('http://www.example.com/foo/foobar'), '->request() updates the CookieJar');
  161. $client->request('GET', 'bar');
  162. $this->assertEquals(array('foo' => 'bar'), $client->getCookieJar()->getValues('http://www.example.com/foo/foobar'), '->request() updates the CookieJar');
  163. }
  164. public function testClick()
  165. {
  166. $client = new TestClient();
  167. $client->setNextResponse(new Response('<html><a href="/foo">foo</a></html>'));
  168. $crawler = $client->request('GET', 'http://www.example.com/foo/foobar');
  169. $client->click($crawler->filter('a')->link());
  170. $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->click() clicks on links');
  171. }
  172. public function testSubmit()
  173. {
  174. $client = new TestClient();
  175. $client->setNextResponse(new Response('<html><form action="/foo"><input type="submit" /></form></html>'));
  176. $crawler = $client->request('GET', 'http://www.example.com/foo/foobar');
  177. $client->submit($crawler->filter('input')->form());
  178. $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->submit() submit forms');
  179. }
  180. public function testFollowRedirect()
  181. {
  182. $client = new TestClient();
  183. $client->followRedirects(false);
  184. $client->request('GET', 'http://www.example.com/foo/foobar');
  185. try {
  186. $client->followRedirect();
  187. $this->fail('->followRedirect() throws a \LogicException if the request was not redirected');
  188. } catch (\Exception $e) {
  189. $this->assertInstanceof('LogicException', $e, '->followRedirect() throws a \LogicException if the request was not redirected');
  190. }
  191. $client->setNextResponse(new Response('', 200, array('Location' => 'http://www.example.com/redirected')));
  192. $client->request('GET', 'http://www.example.com/foo/foobar');
  193. $client->followRedirect();
  194. $this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() follows a redirect if any');
  195. $client = new TestClient();
  196. $client->setNextResponse(new Response('', 200, array('Location' => 'http://www.example.com/redirected')));
  197. $client->request('GET', 'http://www.example.com/foo/foobar');
  198. $this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() automatically follows redirects if followRedirects is true');
  199. }
  200. public function testBack()
  201. {
  202. $client = new TestClient();
  203. $client->request('GET', 'http://www.example.com/foo/foobar');
  204. $client->request('GET', 'http://www.example.com/foo');
  205. $client->back();
  206. $this->assertEquals('http://www.example.com/foo/foobar', $client->getRequest()->getUri(), '->back() goes back in the history');
  207. }
  208. public function testForward()
  209. {
  210. $client = new TestClient();
  211. $client->request('GET', 'http://www.example.com/foo/foobar');
  212. $client->request('GET', 'http://www.example.com/foo');
  213. $client->back();
  214. $client->forward();
  215. $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->forward() goes forward in the history');
  216. }
  217. public function testReload()
  218. {
  219. $client = new TestClient();
  220. $client->request('GET', 'http://www.example.com/foo/foobar');
  221. $client->reload();
  222. $this->assertEquals('http://www.example.com/foo/foobar', $client->getRequest()->getUri(), '->forward() reloads the current page');
  223. }
  224. public function testRestart()
  225. {
  226. $client = new TestClient();
  227. $client->request('GET', 'http://www.example.com/foo/foobar');
  228. $client->restart();
  229. $this->assertTrue($client->getHistory()->isEmpty(), '->restart() clears the history');
  230. $this->assertEquals(array(), $client->getCookieJar()->all(), '->restart() clears the cookies');
  231. }
  232. public function testInsulatedRequests()
  233. {
  234. $client = new TestClient();
  235. $client->insulate();
  236. $client->setNextScript("new Symfony\Components\BrowserKit\Response('foobar')");
  237. $client->request('GET', 'http://www.example.com/foo/foobar');
  238. $this->assertEquals('foobar', $client->getResponse()->getContent(), '->insulate() process the request in a forked process');
  239. $client->setNextScript("new Symfony\Components\BrowserKit\Response('foobar)");
  240. try {
  241. $client->request('GET', 'http://www.example.com/foo/foobar');
  242. $this->fail('->request() throws a \RuntimeException if the script has an error');
  243. } catch (\Exception $e) {
  244. $this->assertInstanceof('RuntimeException', $e, '->request() throws a \RuntimeException if the script has an error');
  245. }
  246. }
  247. }