ClientTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\RequestHandler\Test;
  11. use Symfony\Components\RequestHandler\Test\Client;
  12. use Symfony\Components\RequestHandler\Test\RequestTester;
  13. use Symfony\Components\RequestHandler\Test\ResponseTester;
  14. use Symfony\Components\RequestHandler\RequestHandler;
  15. use Symfony\Components\RequestHandler\Request;
  16. use Symfony\Components\RequestHandler\Response;
  17. use Symfony\Components\EventDispatcher\EventDispatcher;
  18. use Symfony\Components\EventDispatcher\Event;
  19. require_once __DIR__.'/TestRequestHandler.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__."/TestRequestHandler.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 TestRequestHandler());
  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 TestRequestHandler());
  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 TestRequestHandler());
  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. {
  55. $client->getTester('bar');
  56. $this->pass('->getTester() throws an \InvalidArgumentException if the tester object does not exist');
  57. }
  58. catch (\Exception $e)
  59. {
  60. $this->assertInstanceof('InvalidArgumentException', $e, '->getTester() throws an \InvalidArgumentException if the tester object does not exist');
  61. }
  62. $this->assertTrue($client->hasTester('foo'), '->hasTester() returns true if the tester object exist');
  63. $this->assertFalse($client->hasTester('bar'), '->hasTester() returns false if the tester object does not exist');
  64. }
  65. public function testMagicCall()
  66. {
  67. $client = new TestClient(new TestRequestHandler());
  68. $client->request('DELETE', '/foo');
  69. $client->addTester('request', new RequestTester($client->getRequest()));
  70. $client->addTester('response', new ResponseTester($client->getResponse()));
  71. $client->setTestCase($this);
  72. $client->assertRequestMethod('DELETE');
  73. $client->assertTrue(true, '->__call() redirects assert methods to PHPUnit');
  74. try
  75. {
  76. $client->foobar();
  77. $this->pass('->__call() throws a \BadMethodCallException if the method does not exist');
  78. }
  79. catch (\Exception $e)
  80. {
  81. $this->assertInstanceof('BadMethodCallException', $e, '->__call() throws a \BadMethodCallException if the method does not exist');
  82. }
  83. try
  84. {
  85. $client->assertFoo();
  86. $this->pass('->__call() throws a \BadMethodCallException if the method does not exist');
  87. }
  88. catch (\Exception $e)
  89. {
  90. $this->assertInstanceof('BadMethodCallException', $e, '->__call() throws a \BadMethodCallException if the method does not exist');
  91. }
  92. try
  93. {
  94. $client->assertFooBar();
  95. $this->pass('->__call() throws a \BadMethodCallException if the method does not exist');
  96. }
  97. catch (\Exception $e)
  98. {
  99. $this->assertInstanceof('BadMethodCallException', $e, '->__call() throws a \BadMethodCallException if the method does not exist');
  100. }
  101. }
  102. }