DescriptionTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace GuzzleHttp\Tests\Command\Guzzle;
  3. use GuzzleHttp\Command\Guzzle\Description;
  4. use GuzzleHttp\Command\Guzzle\Operation;
  5. use GuzzleHttp\Command\Guzzle\Parameter;
  6. use GuzzleHttp\Command\Guzzle\SchemaFormatter;
  7. /**
  8. * @covers \GuzzleHttp\Command\Guzzle\Description
  9. */
  10. class DescriptionTest extends \PHPUnit_Framework_TestCase
  11. {
  12. protected $operations;
  13. public function setup()
  14. {
  15. $this->operations = [
  16. 'test_command' => [
  17. 'name' => 'test_command',
  18. 'description' => 'documentationForCommand',
  19. 'httpMethod' => 'DELETE',
  20. 'class' => 'FooModel',
  21. 'parameters' => [
  22. 'bucket' => ['required' => true],
  23. 'key' => ['required' => true]
  24. ]
  25. ]
  26. ];
  27. }
  28. public function testConstructor()
  29. {
  30. $service = new Description(['operations' => $this->operations]);
  31. $this->assertEquals(1, count($service->getOperations()));
  32. $this->assertFalse($service->hasOperation('foobar'));
  33. $this->assertTrue($service->hasOperation('test_command'));
  34. }
  35. public function testContainsModels()
  36. {
  37. $d = new Description([
  38. 'operations' => ['foo' => []],
  39. 'models' => [
  40. 'Tag' => ['type' => 'object'],
  41. 'Person' => ['type' => 'object']
  42. ]
  43. ]);
  44. $this->assertTrue($d->hasModel('Tag'));
  45. $this->assertTrue($d->hasModel('Person'));
  46. $this->assertFalse($d->hasModel('Foo'));
  47. $this->assertInstanceOf(Parameter::class, $d->getModel('Tag'));
  48. $this->assertEquals(['Tag', 'Person'], array_keys($d->getModels()));
  49. }
  50. public function testCanUseResponseClass()
  51. {
  52. $d = new Description([
  53. 'operations' => [
  54. 'foo' => ['responseClass' => 'Tag']
  55. ],
  56. 'models' => ['Tag' => ['type' => 'object']]
  57. ]);
  58. $op = $d->getOperation('foo');
  59. $this->assertNotNull($op->getResponseModel());
  60. }
  61. /**
  62. * @expectedException \InvalidArgumentException
  63. */
  64. public function testRetrievingMissingModelThrowsException()
  65. {
  66. $d = new Description([]);
  67. $d->getModel('foo');
  68. }
  69. public function testHasAttributes()
  70. {
  71. $d = new Description([
  72. 'operations' => [],
  73. 'name' => 'Name',
  74. 'description' => 'Description',
  75. 'apiVersion' => '1.24'
  76. ]);
  77. $this->assertEquals('Name', $d->getName());
  78. $this->assertEquals('Description', $d->getDescription());
  79. $this->assertEquals('1.24', $d->getApiVersion());
  80. }
  81. public function testPersistsCustomAttributes()
  82. {
  83. $data = [
  84. 'operations' => ['foo' => ['class' => 'foo', 'parameters' => []]],
  85. 'name' => 'Name',
  86. 'description' => 'Test',
  87. 'apiVersion' => '1.24',
  88. 'auth' => 'foo',
  89. 'keyParam' => 'bar'
  90. ];
  91. $d = new Description($data);
  92. $this->assertEquals('foo', $d->getData('auth'));
  93. $this->assertEquals('bar', $d->getData('keyParam'));
  94. $this->assertEquals(['auth' => 'foo', 'keyParam' => 'bar'], $d->getData());
  95. $this->assertNull($d->getData('missing'));
  96. }
  97. /**
  98. * @expectedException \InvalidArgumentException
  99. */
  100. public function testThrowsExceptionForMissingOperation()
  101. {
  102. $s = new Description([]);
  103. $this->assertNull($s->getOperation('foo'));
  104. }
  105. /**
  106. * @expectedException \InvalidArgumentException
  107. */
  108. public function testValidatesOperationTypes()
  109. {
  110. new Description([
  111. 'operations' => ['foo' => new \stdClass()]
  112. ]);
  113. }
  114. public function testHasbaseUrl()
  115. {
  116. $description = new Description(['baseUrl' => 'http://foo.com']);
  117. $this->assertEquals('http://foo.com', $description->getBaseUri());
  118. }
  119. public function testHasbaseUri()
  120. {
  121. $description = new Description(['baseUri' => 'http://foo.com']);
  122. $this->assertEquals('http://foo.com', $description->getBaseUri());
  123. }
  124. public function testModelsHaveNames()
  125. {
  126. $desc = [
  127. 'models' => [
  128. 'date' => ['type' => 'string'],
  129. 'user'=> [
  130. 'type' => 'object',
  131. 'properties' => [
  132. 'dob' => ['$ref' => 'date']
  133. ]
  134. ]
  135. ]
  136. ];
  137. $s = new Description($desc);
  138. $this->assertEquals('string', $s->getModel('date')->getType());
  139. $this->assertEquals('dob', $s->getModel('user')->getProperty('dob')->getName());
  140. }
  141. public function testHasOperations()
  142. {
  143. $desc = ['operations' => ['foo' => ['parameters' => ['foo' => [
  144. 'name' => 'foo'
  145. ]]]]];
  146. $s = new Description($desc);
  147. $this->assertInstanceOf(Operation::class, $s->getOperation('foo'));
  148. $this->assertSame($s->getOperation('foo'), $s->getOperation('foo'));
  149. }
  150. public function testHasFormatter()
  151. {
  152. $s = new Description([]);
  153. $this->assertNotEmpty($s->format('date', 'now'));
  154. }
  155. public function testCanUseCustomFormatter()
  156. {
  157. $formatter = $this->getMockBuilder(SchemaFormatter::class)
  158. ->setMethods(['format'])
  159. ->getMock();
  160. $formatter->expects($this->once())
  161. ->method('format');
  162. $s = new Description([], ['formatter' => $formatter]);
  163. $s->format('time', 'now');
  164. }
  165. }