ClientTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\HttpKernel\Test;
  11. use Symfony\Components\HttpKernel\Test\Client;
  12. use Symfony\Components\HttpKernel\Test\RequestTester;
  13. use Symfony\Components\HttpKernel\Test\ResponseTester;
  14. use Symfony\Components\HttpKernel\HttpKernel;
  15. use Symfony\Components\HttpKernel\Request;
  16. use Symfony\Components\HttpKernel\Response;
  17. use Symfony\Components\EventDispatcher\EventDispatcher;
  18. use Symfony\Components\EventDispatcher\Event;
  19. require_once __DIR__.'/TestHttpKernel.php';
  20. class TestClient extends Client
  21. {
  22. protected function getScript($request)
  23. {
  24. $script = parent::getScript($request);
  25. $script = preg_replace('/(\->register\(\);)/', "$0\nrequire_once '".__DIR__."/TestHttpKernel.php';", $script);
  26. return $script;
  27. }
  28. }
  29. class ClientTest extends \PHPUnit_Framework_TestCase
  30. {
  31. public function testDoRequest()
  32. {
  33. $client = new Client(new TestHttpKernel());
  34. $client->request('GET', '/');
  35. $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
  36. $client->request('GET', 'http://www.example.com/');
  37. $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
  38. $this->assertEquals('www.example.com', $client->getRequest()->getHost(), '->doRequest() uses the request handler to make the request');
  39. }
  40. public function testGetScript()
  41. {
  42. $client = new TestClient(new TestHttpKernel());
  43. $client->insulate();
  44. $client->request('GET', '/');
  45. $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->getScript() returns a script that uses the request handler to make the request');
  46. }
  47. public function testAddHasGetTester()
  48. {
  49. $client = new TestClient(new TestHttpKernel());
  50. $client->request('GET', '/');
  51. $client->addTester('foo', $tester = new RequestTester($client->getRequest()));
  52. $this->assertSame($tester, $client->getTester('foo'), '->addTester() adds a tester object');
  53. try {
  54. $client->getTester('bar');
  55. $this->pass('->getTester() throws an \InvalidArgumentException if the tester object does not exist');
  56. } catch (\Exception $e) {
  57. $this->assertInstanceof('InvalidArgumentException', $e, '->getTester() throws an \InvalidArgumentException if the tester object does not exist');
  58. }
  59. $this->assertTrue($client->hasTester('foo'), '->hasTester() returns true if the tester object exist');
  60. $this->assertFalse($client->hasTester('bar'), '->hasTester() returns false if the tester object does not exist');
  61. }
  62. public function testMagicCall()
  63. {
  64. $client = new TestClient(new TestHttpKernel());
  65. $client->request('DELETE', '/foo');
  66. $client->addTester('request', new RequestTester($client->getRequest()));
  67. $client->addTester('response', new ResponseTester($client->getResponse()));
  68. $client->setTestCase($this);
  69. $client->assertRequestMethod('DELETE');
  70. $client->assertTrue(true, '->__call() redirects assert methods to PHPUnit');
  71. try {
  72. $client->foobar();
  73. $this->pass('->__call() throws a \BadMethodCallException if the method does not exist');
  74. } catch (\Exception $e) {
  75. $this->assertInstanceof('BadMethodCallException', $e, '->__call() throws a \BadMethodCallException if the method does not exist');
  76. }
  77. try {
  78. $client->assertFoo();
  79. $this->pass('->__call() throws a \BadMethodCallException if the method does not exist');
  80. } catch (\Exception $e) {
  81. $this->assertInstanceof('BadMethodCallException', $e, '->__call() throws a \BadMethodCallException if the method does not exist');
  82. }
  83. try {
  84. $client->assertFooBar();
  85. $this->pass('->__call() throws a \BadMethodCallException if the method does not exist');
  86. } catch (\Exception $e) {
  87. $this->assertInstanceof('BadMethodCallException', $e, '->__call() throws a \BadMethodCallException if the method does not exist');
  88. }
  89. }
  90. }