ValidatedDescriptionHandlerTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace GuzzleHttp\Tests\Command\Guzzle\Handler;
  3. use GuzzleHttp\Client as HttpClient;
  4. use GuzzleHttp\Command\Guzzle\Description;
  5. use GuzzleHttp\Command\Guzzle\GuzzleClient;
  6. /**
  7. * @covers \GuzzleHttp\Command\Guzzle\Handler\ValidatedDescriptionHandler
  8. */
  9. class ValidatedDescriptionHandlerTest extends \PHPUnit_Framework_TestCase
  10. {
  11. /**
  12. * @expectedException \GuzzleHttp\Command\Exception\CommandException
  13. * @expectedExceptionMessage Validation errors: [bar] is a required string
  14. */
  15. public function testValidates()
  16. {
  17. $description = new Description([
  18. 'operations' => [
  19. 'foo' => [
  20. 'uri' => 'http://httpbin.org',
  21. 'httpMethod' => 'GET',
  22. 'responseModel' => 'j',
  23. 'parameters' => [
  24. 'bar' => [
  25. 'type' => 'string',
  26. 'required' => true
  27. ]
  28. ]
  29. ]
  30. ]
  31. ]);
  32. $client = new GuzzleClient(new HttpClient(), $description);
  33. $client->foo([]);
  34. }
  35. public function testSuccessfulValidationDoesNotThrow()
  36. {
  37. $description = new Description([
  38. 'operations' => [
  39. 'foo' => [
  40. 'uri' => 'http://httpbin.org',
  41. 'httpMethod' => 'GET',
  42. 'responseModel' => 'j',
  43. 'parameters' => []
  44. ]
  45. ],
  46. 'models' => [
  47. 'j' => [
  48. 'type' => 'object'
  49. ]
  50. ]
  51. ]);
  52. $client = new GuzzleClient(new HttpClient(), $description);
  53. $client->foo([]);
  54. }
  55. /**
  56. * @expectedException \GuzzleHttp\Command\Exception\CommandException
  57. * @expectedExceptionMessage Validation errors: [bar] must be of type string
  58. */
  59. public function testValidatesAdditionalParameters()
  60. {
  61. $description = new Description([
  62. 'operations' => [
  63. 'foo' => [
  64. 'uri' => 'http://httpbin.org',
  65. 'httpMethod' => 'GET',
  66. 'responseModel' => 'j',
  67. 'additionalParameters' => [
  68. 'type' => 'string'
  69. ]
  70. ]
  71. ],
  72. 'models' => [
  73. 'j' => [
  74. 'type' => 'object'
  75. ]
  76. ]
  77. ]);
  78. $client = new GuzzleClient(new HttpClient(), $description);
  79. $client->foo(['bar' => new \stdClass()]);
  80. }
  81. public function testFilterBeforeValidate()
  82. {
  83. $description = new Description([
  84. 'operations' => [
  85. 'foo' => [
  86. 'uri' => 'http://httpbin.org',
  87. 'httpMethod' => 'GET',
  88. 'parameters' => [
  89. 'bar' => [
  90. 'location' => 'uri',
  91. 'type' => 'string',
  92. 'format' => 'date-time',
  93. 'required' => true
  94. ]
  95. ]
  96. ]
  97. ]
  98. ]);
  99. $client = new GuzzleClient(new HttpClient(), $description);
  100. $client->foo(['bar' => new \DateTimeImmutable()]); // Should not throw any exception
  101. }
  102. }