ExternalTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
  2. /**
  3. * Unit tests for external request client
  4. *
  5. * @group kohana
  6. * @group kohana.request
  7. * @group kohana.request.client
  8. * @group kohana.request.client.external
  9. *
  10. * @package Kohana
  11. * @category Tests
  12. * @author Kohana Team
  13. * @copyright (c) 2008-2012 Kohana Team
  14. * @license http://kohanaframework.org/license
  15. */
  16. class Kohana_Request_Client_ExternalTest extends Unittest_TestCase {
  17. /**
  18. * Provider for test_factory()
  19. *
  20. * @return array
  21. */
  22. public function provider_factory()
  23. {
  24. Request_Client_External::$client = 'Request_Client_Stream';
  25. $return = array(
  26. array(
  27. array(),
  28. NULL,
  29. 'Request_Client_Stream'
  30. ),
  31. array(
  32. array(),
  33. 'Request_Client_Stream',
  34. 'Request_Client_Stream'
  35. )
  36. );
  37. if (extension_loaded('curl'))
  38. {
  39. $return[] = array(
  40. array(),
  41. 'Request_Client_Curl',
  42. 'Request_Client_Curl'
  43. );
  44. }
  45. if (extension_loaded('http'))
  46. {
  47. $return[] = array(
  48. array(),
  49. 'Request_Client_HTTP',
  50. 'Request_Client_HTTP'
  51. );
  52. }
  53. return $return;
  54. }
  55. /**
  56. * Tests the [Request_Client_External::factory()] method
  57. *
  58. * @dataProvider provider_factory
  59. *
  60. * @param array $params params
  61. * @param string $client client
  62. * @param Request_Client_External $expected expected
  63. * @return void
  64. */
  65. public function test_factory($params, $client, $expected)
  66. {
  67. $this->assertInstanceOf($expected, Request_Client_External::factory($params, $client));
  68. }
  69. /**
  70. * Data provider for test_options
  71. *
  72. * @return array
  73. */
  74. public function provider_options()
  75. {
  76. return array(
  77. array(
  78. NULL,
  79. NULL,
  80. array()
  81. ),
  82. array(
  83. array('foo' => 'bar', 'stfu' => 'snafu'),
  84. NULL,
  85. array('foo' => 'bar', 'stfu' => 'snafu')
  86. ),
  87. array(
  88. 'foo',
  89. 'bar',
  90. array('foo' => 'bar')
  91. ),
  92. array(
  93. array('foo' => 'bar'),
  94. 'foo',
  95. array('foo' => 'bar')
  96. )
  97. );
  98. }
  99. /**
  100. * Tests the [Request_Client_External::options()] method
  101. *
  102. * @dataProvider provider_options
  103. *
  104. * @param mixed $key key
  105. * @param mixed $value value
  106. * @param array $expected expected
  107. * @return void
  108. */
  109. public function test_options($key, $value, $expected)
  110. {
  111. // Create a mock external client
  112. $client = new Request_Client_Stream;
  113. $client->options($key, $value);
  114. $this->assertSame($expected, $client->options());
  115. }
  116. /**
  117. * Data provider for test_execute
  118. *
  119. * @return array
  120. */
  121. public function provider_execute()
  122. {
  123. $json = '{"foo": "bar", "snafu": "stfu"}';
  124. $post = array('foo' => 'bar', 'snafu' => 'stfu');
  125. return array(
  126. array(
  127. 'application/json',
  128. $json,
  129. array(),
  130. array(
  131. 'content-type' => 'application/json',
  132. 'body' => $json
  133. )
  134. ),
  135. array(
  136. 'application/json',
  137. $json,
  138. $post,
  139. array(
  140. 'content-type' => 'application/x-www-form-urlencoded',
  141. 'body' => http_build_query($post, NULL, '&')
  142. )
  143. )
  144. );
  145. }
  146. /**
  147. * Tests the [Request_Client_External::_send_message()] method
  148. *
  149. * @dataProvider provider_execute
  150. *
  151. * @return void
  152. */
  153. public function test_execute($content_type, $body, $post, $expected)
  154. {
  155. $old_request = Request::$initial;
  156. Request::$initial = TRUE;
  157. // Create a mock Request
  158. $request = new Request('http://kohanaframework.org/');
  159. $request->method(HTTP_Request::POST)
  160. ->headers('content-type', $content_type)
  161. ->body($body)
  162. ->post($post);
  163. $client = $this->getMock('Request_Client_External', array('_send_message'));
  164. $client->expects($this->once())
  165. ->method('_send_message')
  166. ->with($request)
  167. ->will($this->returnValue($this->getMock('Response')));
  168. $request->client($client);
  169. $this->assertInstanceOf('Response', $request->execute());
  170. $this->assertSame($expected['body'], $request->body());
  171. $this->assertSame($expected['content-type'], (string) $request->headers('content-type'));
  172. Request::$initial = $old_request;
  173. }
  174. }