functionsTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace GuzzleHttp\Test;
  3. use GuzzleHttp;
  4. use PHPUnit\Framework\TestCase;
  5. class FunctionsTest extends TestCase
  6. {
  7. public function testExpandsTemplate()
  8. {
  9. $this->assertSame(
  10. 'foo/123',
  11. GuzzleHttp\uri_template('foo/{bar}', ['bar' => '123'])
  12. );
  13. }
  14. public function noBodyProvider()
  15. {
  16. return [['get'], ['head'], ['delete']];
  17. }
  18. public function testProvidesDefaultUserAgent()
  19. {
  20. $ua = GuzzleHttp\default_user_agent();
  21. $this->assertRegExp('#^GuzzleHttp/.+ curl/.+ PHP/.+$#', $ua);
  22. }
  23. public function typeProvider()
  24. {
  25. return [
  26. ['foo', 'string(3) "foo"'],
  27. [true, 'bool(true)'],
  28. [false, 'bool(false)'],
  29. [10, 'int(10)'],
  30. [1.0, 'float(1)'],
  31. [new StrClass(), 'object(GuzzleHttp\Test\StrClass)'],
  32. [['foo'], 'array(1)']
  33. ];
  34. }
  35. /**
  36. * @dataProvider typeProvider
  37. */
  38. public function testDescribesType($input, $output)
  39. {
  40. $this->assertSame($output, GuzzleHttp\describe_type($input));
  41. }
  42. public function testParsesHeadersFromLines()
  43. {
  44. $lines = ['Foo: bar', 'Foo: baz', 'Abc: 123', 'Def: a, b'];
  45. $this->assertSame([
  46. 'Foo' => ['bar', 'baz'],
  47. 'Abc' => ['123'],
  48. 'Def' => ['a, b'],
  49. ], GuzzleHttp\headers_from_lines($lines));
  50. }
  51. public function testParsesHeadersFromLinesWithMultipleLines()
  52. {
  53. $lines = ['Foo: bar', 'Foo: baz', 'Foo: 123'];
  54. $this->assertSame([
  55. 'Foo' => ['bar', 'baz', '123'],
  56. ], GuzzleHttp\headers_from_lines($lines));
  57. }
  58. public function testReturnsDebugResource()
  59. {
  60. $this->assertInternalType('resource', GuzzleHttp\debug_resource());
  61. }
  62. public function testProvidesDefaultCaBundler()
  63. {
  64. $this->assertFileExists(GuzzleHttp\default_ca_bundle());
  65. }
  66. public function noProxyProvider()
  67. {
  68. return [
  69. ['mit.edu', ['.mit.edu'], false],
  70. ['foo.mit.edu', ['.mit.edu'], true],
  71. ['mit.edu', ['mit.edu'], true],
  72. ['mit.edu', ['baz', 'mit.edu'], true],
  73. ['mit.edu', ['', '', 'mit.edu'], true],
  74. ['mit.edu', ['baz', '*'], true],
  75. ];
  76. }
  77. /**
  78. * @dataProvider noproxyProvider
  79. */
  80. public function testChecksNoProxyList($host, $list, $result)
  81. {
  82. $this->assertSame(
  83. $result,
  84. \GuzzleHttp\is_host_in_noproxy($host, $list)
  85. );
  86. }
  87. /**
  88. * @expectedException \InvalidArgumentException
  89. */
  90. public function testEnsuresNoProxyCheckHostIsSet()
  91. {
  92. \GuzzleHttp\is_host_in_noproxy('', []);
  93. }
  94. public function testEncodesJson()
  95. {
  96. $this->assertSame('true', \GuzzleHttp\json_encode(true));
  97. }
  98. /**
  99. * @expectedException \InvalidArgumentException
  100. */
  101. public function testEncodesJsonAndThrowsOnError()
  102. {
  103. \GuzzleHttp\json_encode("\x99");
  104. }
  105. public function testDecodesJson()
  106. {
  107. $this->assertTrue(\GuzzleHttp\json_decode('true'));
  108. }
  109. /**
  110. * @expectedException \InvalidArgumentException
  111. */
  112. public function testDecodesJsonAndThrowsOnError()
  113. {
  114. \GuzzleHttp\json_decode('{{]]');
  115. }
  116. }
  117. final class StrClass
  118. {
  119. public function __toString()
  120. {
  121. return 'foo';
  122. }
  123. }