ResponseTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * File ResponseTest.php
  4. *
  5. * @author Edward Pfremmer <epfremme@nerdery.com>
  6. */
  7. namespace Epfremme\Swagger\Tests\Entity;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Epfremme\Swagger\Entity\Examples;
  10. use Epfremme\Swagger\Entity\Headers;
  11. use Epfremme\Swagger\Entity\Response;
  12. use Epfremme\Swagger\Entity\Parameters;
  13. use Epfremme\Swagger\Entity\Schemas\AbstractSchema;
  14. use Epfremme\Swagger\Entity\Schemas\ObjectSchema;
  15. use Epfremme\Swagger\Tests\Mixin\SerializerContextTrait;
  16. /**
  17. * Class ResponseTest
  18. *
  19. * @package Epfremme\Swagger
  20. * @subpackage Tests\Entity
  21. */
  22. class ResponseTest extends \PHPUnit_Framework_TestCase
  23. {
  24. use SerializerContextTrait;
  25. /**
  26. * @var Response
  27. */
  28. protected $response;
  29. /**
  30. * {@inheritdoc}
  31. */
  32. protected function setUp()
  33. {
  34. $this->response = new Response();
  35. }
  36. /**
  37. * @covers Epfremme\Swagger\Entity\Response::getDescription
  38. * @covers Epfremme\Swagger\Entity\Response::setDescription
  39. */
  40. public function testDescription()
  41. {
  42. $this->assertClassHasAttribute('description', Response::class);
  43. $this->assertInstanceOf(Response::class, $this->response->setDescription('foo'));
  44. $this->assertAttributeEquals('foo', 'description', $this->response);
  45. $this->assertEquals('foo', $this->response->getDescription());
  46. }
  47. /**
  48. * @covers Epfremme\Swagger\Entity\Response::getSchema
  49. * @covers Epfremme\Swagger\Entity\Response::setSchema
  50. */
  51. public function testSchema()
  52. {
  53. $schema = new ObjectSchema();
  54. $this->assertClassHasAttribute('schema', Response::class);
  55. $this->assertInstanceOf(Response::class, $this->response->setSchema($schema));
  56. $this->assertAttributeInstanceOf(ObjectSchema::class, 'schema', $this->response);
  57. $this->assertAttributeEquals($schema, 'schema', $this->response);
  58. $this->assertEquals($schema, $this->response->getSchema());
  59. }
  60. /**
  61. * @covers Epfremme\Swagger\Entity\Response::getHeaders
  62. * @covers Epfremme\Swagger\Entity\Response::setHeaders
  63. */
  64. public function testHeaders()
  65. {
  66. $headers = new ArrayCollection([
  67. 'foo' => new Headers\StringHeader(),
  68. 'bar' => new Headers\IntegerHeader(),
  69. 'baz' => new Headers\StringHeader(),
  70. ]);
  71. $this->assertClassHasAttribute('headers', Response::class);
  72. $this->assertInstanceOf(Response::class, $this->response->setHeaders($headers));
  73. $this->assertAttributeInstanceOf(ArrayCollection::class, 'headers', $this->response);
  74. $this->assertAttributeEquals($headers, 'headers', $this->response);
  75. $this->assertEquals($headers, $this->response->getHeaders());
  76. $this->assertContainsOnlyInstancesOf(Headers\AbstractHeader::class, $this->response->getHeaders());
  77. }
  78. /**
  79. * @covers Epfremme\Swagger\Entity\Response::getExamples
  80. * @covers Epfremme\Swagger\Entity\Response::setExamples
  81. */
  82. public function testExamples()
  83. {
  84. $examples = new Examples();
  85. $this->assertClassHasAttribute('examples', Response::class);
  86. $this->assertInstanceOf(Response::class, $this->response->setExamples($examples));
  87. $this->assertAttributeInstanceOf(Examples::class, 'examples', $this->response);
  88. $this->assertAttributeEquals($examples, 'examples', $this->response);
  89. $this->assertEquals($examples, $this->response->getExamples());
  90. }
  91. /**
  92. * @covers Epfremme\Swagger\Entity\Response
  93. */
  94. public function testSerialize()
  95. {
  96. $data = json_encode([
  97. 'description' => 'bar',
  98. 'schema' => [
  99. 'type' => AbstractSchema::OBJECT_TYPE,
  100. 'format' => 'foo',
  101. 'title' => 'bar',
  102. 'description' => 'baz',
  103. 'example' => 'qux',
  104. 'externalDocs' => (object)[],
  105. ],
  106. 'headers' => [
  107. 'X-Rate-Limit-Limit' => [
  108. 'description' => 'The number of allowed requests in the current period',
  109. 'type' => 'integer'
  110. ],
  111. 'X-Rate-Limit-Remaining' => [
  112. 'description' => 'The number of remaining requests in the current period',
  113. 'type' => 'integer'
  114. ],
  115. 'X-Rate-Limit-Reset' => [
  116. 'description' => 'The number of seconds left in the current period',
  117. 'type' => 'integer'
  118. ],
  119. ],
  120. 'examples' => [
  121. 'text/plain' => [
  122. 'foo' => 'bar',
  123. 'baz' => 'foo'
  124. ],
  125. 'application/json' => [
  126. 'key' => 'any'
  127. ],
  128. ]
  129. ]);
  130. $response = $this->getSerializer()->deserialize($data, Response::class, 'json');
  131. $this->assertInstanceOf(Response::class, $response);
  132. $this->assertAttributeEquals('bar', 'description', $response);
  133. $this->assertAttributeInstanceOf(AbstractSchema::class, 'schema', $response);
  134. $this->assertAttributeInstanceOf(ArrayCollection::class, 'headers', $response);
  135. $this->assertAttributeContainsOnly(Headers\AbstractHeader::class, 'headers', $response);
  136. $this->assertAttributeInstanceOf(Examples::class, 'examples', $response);
  137. $json = $this->getSerializer()->serialize($response, 'json');
  138. $this->assertJson($json);
  139. $this->assertJsonStringEqualsJsonString($data, $json);
  140. }
  141. }