PathTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * File PathTest.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\Operation;
  10. use Epfremme\Swagger\Entity\Parameters;
  11. use Epfremme\Swagger\Entity\Path;
  12. use Epfremme\Swagger\Tests\Mixin\SerializerContextTrait;
  13. /**
  14. * Class PathTest
  15. *
  16. * @package Epfremme\Swagger
  17. * @subpackage Tests\Entity
  18. */
  19. class PathTest extends \PHPUnit_Framework_TestCase
  20. {
  21. use SerializerContextTrait;
  22. /**
  23. * @var Path
  24. */
  25. protected $path;
  26. /**
  27. * {@inheritdoc}
  28. */
  29. protected function setUp()
  30. {
  31. $this->path = new Path();
  32. }
  33. /**
  34. * @covers Epfremme\Swagger\Entity\Path::getOperations
  35. * @covers Epfremme\Swagger\Entity\Path::setOperations
  36. */
  37. public function testOperations()
  38. {
  39. $operations = new ArrayCollection([
  40. 'foo' => new Operation(),
  41. 'bar' => new Operation(),
  42. 'baz' => new Operation(),
  43. ]);
  44. $this->assertClassHasAttribute('operations', Path::class);
  45. $this->assertInstanceOf(Path::class, $this->path->setOperations($operations));
  46. $this->assertAttributeInstanceOf(ArrayCollection::class, 'operations', $this->path);
  47. $this->assertAttributeEquals($operations, 'operations', $this->path);
  48. $this->assertEquals($operations, $this->path->getOperations());
  49. $this->assertContainsOnlyInstancesOf(Operation::class, $this->path->getOperations());
  50. }
  51. /**
  52. * @covers Epfremme\Swagger\Entity\Path
  53. */
  54. public function testSerialize()
  55. {
  56. $data = json_encode([
  57. 'get' => [
  58. 'summary' => 'foo',
  59. 'description' => 'bar',
  60. 'responses' => [
  61. '200' => [
  62. 'description' => 'Pet updated.'
  63. ],
  64. '405' => [
  65. 'description' => 'Invalid input'
  66. ]
  67. ],
  68. 'schemes' => ['http', 'https'],
  69. 'security' => [
  70. [
  71. 'petstore_auth' => [
  72. 'read:pets',
  73. ]
  74. ]
  75. ],
  76. 'deprecated' => false,
  77. ],
  78. 'post' => [
  79. 'tags' => [
  80. 'foo'
  81. ],
  82. 'summary' => 'foo',
  83. 'description' => 'bar',
  84. 'externalDocs' => (object)[],
  85. 'operationId' => 'baz',
  86. 'consumes' => [
  87. 'application/x-www-form-urlencoded'
  88. ],
  89. 'produces' => [
  90. 'application/json',
  91. 'application/xml'
  92. ],
  93. 'parameters' => [
  94. [
  95. 'name' => 'petId',
  96. 'in' => Parameters\AbstractParameter::IN_PATH,
  97. 'description' => 'ID of pet that needs to be updated',
  98. 'required' => true,
  99. 'type' => Parameters\AbstractTypedParameter::STRING_TYPE
  100. ],
  101. [
  102. 'name' => 'name',
  103. 'in' => Parameters\AbstractParameter::IN_FORM_DATA,
  104. 'description' => 'Updated name of the pet',
  105. 'required' => false,
  106. 'type' => Parameters\AbstractTypedParameter::STRING_TYPE
  107. ],
  108. [
  109. 'name' => 'status',
  110. 'in' => Parameters\AbstractParameter::IN_FORM_DATA,
  111. 'description' => 'Updated status of the pet',
  112. 'required' => false,
  113. 'type' => Parameters\AbstractTypedParameter::STRING_TYPE
  114. ]
  115. ],
  116. 'responses' => [
  117. '200' => [
  118. 'description' => 'Pet updated.'
  119. ],
  120. '405' => [
  121. 'description' => 'Invalid input'
  122. ]
  123. ],
  124. 'schemes' => ['http', 'https'],
  125. 'security' => [
  126. [
  127. 'petstore_auth' => [
  128. 'read:pets',
  129. ]
  130. ]
  131. ],
  132. 'deprecated' => true,
  133. ],
  134. ]);
  135. $path = $this->getSerializer()->deserialize($data, Path::class, 'json');
  136. $this->assertInstanceOf(Path::class, $path);
  137. $this->assertAttributeInstanceOf(ArrayCollection::class, 'operations', $path);
  138. $this->assertAttributeContainsOnly(Operation::class, 'operations', $path);
  139. $json = $this->getSerializer()->serialize($path, 'json');
  140. $this->assertJson($json);
  141. $this->assertJsonStringEqualsJsonString($data, $json);
  142. }
  143. }