ExternalTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  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) Kohana Team
  14. * @license https://koseven.ga/LICENSE.md
  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 = [
  26. [
  27. [],
  28. NULL,
  29. 'Request_Client_Stream'
  30. ],
  31. [
  32. [],
  33. 'Request_Client_Stream',
  34. 'Request_Client_Stream'
  35. ]
  36. ];
  37. if (extension_loaded('curl'))
  38. {
  39. $return[] = [
  40. [],
  41. 'Request_Client_Curl',
  42. 'Request_Client_Curl'
  43. ];
  44. }
  45. if (extension_loaded('http') && function_exists('http_support'))
  46. {
  47. $return[] = [
  48. [],
  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 [
  77. [
  78. NULL,
  79. NULL,
  80. []
  81. ],
  82. [
  83. ['foo' => 'bar', 'stfu' => 'snafu'],
  84. NULL,
  85. ['foo' => 'bar', 'stfu' => 'snafu']
  86. ],
  87. [
  88. 'foo',
  89. 'bar',
  90. ['foo' => 'bar']
  91. ],
  92. [
  93. ['foo' => 'bar'],
  94. 'foo',
  95. ['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 = ['foo' => 'bar', 'snafu' => 'stfu'];
  125. return [
  126. [
  127. 'application/json',
  128. $json,
  129. [],
  130. [
  131. 'content-type' => 'application/json',
  132. 'body' => $json
  133. ]
  134. ],
  135. [
  136. 'application/json',
  137. $json,
  138. $post,
  139. [
  140. 'content-type' => 'application/x-www-form-urlencoded; charset='.Kohana::$charset,
  141. 'body' => http_build_query($post, NULL, '&')
  142. ]
  143. ]
  144. ];
  145. }
  146. }