AugmentPropertiesTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php declare(strict_types=1);
  2. /**
  3. * @license Apache 2.0
  4. */
  5. namespace OpenApiTests;
  6. use OpenApi\Annotations\Property;
  7. use OpenApi\Processors\AugmentProperties;
  8. use OpenApi\Processors\AugmentSchemas;
  9. use OpenApi\Processors\MergeIntoComponents;
  10. use OpenApi\Processors\MergeIntoOpenApi;
  11. use OpenApi\StaticAnalyser;
  12. use const OpenApi\UNDEFINED;
  13. /**
  14. * @group Properties
  15. */
  16. class AugmentPropertiesTest extends OpenApiTestCase
  17. {
  18. public function testAugmentProperties()
  19. {
  20. $analyser = new StaticAnalyser();
  21. $analysis = $analyser->fromFile(__DIR__ . '/Fixtures/Customer.php');
  22. $analysis->process(new MergeIntoOpenApi());
  23. $analysis->process(new MergeIntoComponents());
  24. $analysis->process(new AugmentSchemas());
  25. $customer = $analysis->openapi->components->schemas[0];
  26. $firstName = $customer->properties[0];
  27. $secondName = $customer->properties[1];
  28. $thirdName = $customer->properties[2];
  29. $fourthName = $customer->properties[3];
  30. $lastName = $customer->properties[4];
  31. $tags = $customer->properties[5];
  32. $submittedBy = $customer->properties[6];
  33. $friends = $customer->properties[7];
  34. $bestFriend = $customer->properties[8];
  35. // Verify no values where defined in the annotation.
  36. $this->assertSame(UNDEFINED, $firstName->property);
  37. $this->assertSame(UNDEFINED, $firstName->description);
  38. $this->assertSame(UNDEFINED, $firstName->type);
  39. $this->assertSame(UNDEFINED, $lastName->property);
  40. $this->assertSame(UNDEFINED, $lastName->description);
  41. $this->assertSame(UNDEFINED, $lastName->type);
  42. $this->assertSame(UNDEFINED, $tags->property);
  43. $this->assertSame(UNDEFINED, $tags->type);
  44. $this->assertSame(UNDEFINED, $tags->items);
  45. $this->assertSame(UNDEFINED, $submittedBy->property);
  46. $this->assertSame(UNDEFINED, $submittedBy->ref);
  47. $this->assertSame(UNDEFINED, $friends->property);
  48. $this->assertSame(UNDEFINED, $friends->type);
  49. $this->assertSame(UNDEFINED, $bestFriend->property);
  50. $this->assertSame(UNDEFINED, $bestFriend->nullable);
  51. $this->assertSame(UNDEFINED, $bestFriend->allOf);
  52. $analysis->process(new AugmentProperties());
  53. $expectedValues = [
  54. 'property' => 'firstname',
  55. 'example' => 'John',
  56. 'description' => 'The first name of the customer.',
  57. 'type' => 'string',
  58. ];
  59. $this->assertName($firstName, $expectedValues);
  60. $expectedValues = [
  61. 'property' => 'secondname',
  62. 'example' => 'Allan',
  63. 'description' => 'The second name of the customer.',
  64. 'type' => 'string',
  65. 'nullable' => true,
  66. ];
  67. $this->assertName($secondName, $expectedValues);
  68. $expectedValues = [
  69. 'property' => 'thirdname',
  70. 'example' => 'Peter',
  71. 'description' => 'The third name of the customer.',
  72. 'type' => 'string',
  73. 'nullable' => true,
  74. ];
  75. $this->assertName($thirdName, $expectedValues);
  76. $expectedValues = [
  77. 'property' => 'fourthname',
  78. 'example' => 'Unknown',
  79. 'description' => 'The unknown name of the customer.',
  80. 'type' => UNDEFINED,
  81. 'nullable' => true,
  82. ];
  83. $this->assertName($fourthName, $expectedValues);
  84. $expectedValues = [
  85. 'property' => 'lastname',
  86. 'example' => UNDEFINED,
  87. 'description' => 'The lastname of the customer.',
  88. 'type' => 'string',
  89. ];
  90. $this->assertName($lastName, $expectedValues);
  91. $this->assertSame('tags', $tags->property);
  92. $this->assertSame('array', $tags->type, 'Detect array notation: @var string[]');
  93. $this->assertSame('string', $tags->items->type);
  94. $this->assertSame('submittedBy', $submittedBy->property);
  95. $this->assertSame('#/components/schemas/Customer', $submittedBy->ref);
  96. $this->assertSame('friends', $friends->property);
  97. $this->assertSame('array', $friends->type);
  98. $this->assertSame('#/components/schemas/Customer', $friends->items->ref);
  99. $this->assertSame('bestFriend', $bestFriend->property);
  100. $this->assertTrue($bestFriend->nullable);
  101. $this->assertSame('#/components/schemas/Customer', $bestFriend->oneOf[0]->ref);
  102. }
  103. public function testTypedProperties()
  104. {
  105. $analyser = new StaticAnalyser();
  106. $analysis = $analyser->fromFile(__DIR__ . '/Fixtures/TypedProperties.php');
  107. $analysis->process(new MergeIntoOpenApi());
  108. $analysis->process(new MergeIntoComponents());
  109. $analysis->process(new AugmentSchemas());
  110. [
  111. $stringType,
  112. $intType,
  113. $nullableString,
  114. $arrayType,
  115. $dateTime,
  116. $qualified,
  117. $namespace,
  118. $importedNamespace,
  119. $nativeTrumpsVar,
  120. $annotationTrumpsNative,
  121. $annotationTrumpsAll,
  122. $undefined,
  123. $onlyAnnotated,
  124. $onlyVar,
  125. $staticUndefined,
  126. $staticString,
  127. $staticNullableString,
  128. ] = $analysis->openapi->components->schemas[0]->properties;
  129. $this->assertName($stringType, [
  130. 'property' => UNDEFINED,
  131. 'type' => UNDEFINED,
  132. ]);
  133. $this->assertName($intType, [
  134. 'property' => UNDEFINED,
  135. 'type' => UNDEFINED,
  136. ]);
  137. $this->assertName($nullableString, [
  138. 'property' => UNDEFINED,
  139. 'type' => UNDEFINED,
  140. ]);
  141. $this->assertName($arrayType, [
  142. 'property' => UNDEFINED,
  143. 'type' => UNDEFINED,
  144. ]);
  145. $this->assertName($dateTime, [
  146. 'property' => UNDEFINED,
  147. 'type' => UNDEFINED,
  148. ]);
  149. $this->assertName($qualified, [
  150. 'property' => UNDEFINED,
  151. 'type' => UNDEFINED,
  152. ]);
  153. $this->assertName($namespace, [
  154. 'property' => UNDEFINED,
  155. 'type' => UNDEFINED,
  156. ]);
  157. $this->assertName($importedNamespace, [
  158. 'property' => UNDEFINED,
  159. 'type' => UNDEFINED,
  160. ]);
  161. $this->assertName($nativeTrumpsVar, [
  162. 'property' => UNDEFINED,
  163. 'type' => UNDEFINED,
  164. ]);
  165. $this->assertName($annotationTrumpsNative, [
  166. 'property' => UNDEFINED,
  167. 'type' => 'integer',
  168. ]);
  169. $this->assertName($annotationTrumpsAll, [
  170. 'property' => UNDEFINED,
  171. 'type' => 'integer',
  172. ]);
  173. $this->assertName($undefined, [
  174. 'property' => UNDEFINED,
  175. 'type' => UNDEFINED,
  176. ]);
  177. $this->assertName($onlyAnnotated, [
  178. 'property' => UNDEFINED,
  179. 'type' => 'integer',
  180. ]);
  181. $this->assertName($onlyVar, [
  182. 'property' => UNDEFINED,
  183. 'type' => UNDEFINED,
  184. ]);
  185. $this->assertName($staticUndefined, [
  186. 'property' => UNDEFINED,
  187. 'type' => UNDEFINED,
  188. ]);
  189. $this->assertName($staticString, [
  190. 'property' => UNDEFINED,
  191. 'type' => UNDEFINED,
  192. ]);
  193. $this->assertName($staticNullableString, [
  194. 'property' => UNDEFINED,
  195. 'type' => UNDEFINED,
  196. ]);
  197. $analysis->process(new AugmentProperties());
  198. $this->assertName($stringType, [
  199. 'property' => 'stringType',
  200. 'type' => 'string',
  201. ]);
  202. $this->assertName($intType, [
  203. 'property' => 'intType',
  204. 'type' => 'integer',
  205. ]);
  206. $this->assertName($nullableString, [
  207. 'property' => 'nullableString',
  208. 'type' => 'string',
  209. ]);
  210. $this->assertName($arrayType, [
  211. 'property' => 'arrayType',
  212. 'type' => 'array',
  213. ]);
  214. $this->assertObjectHasAttribute(
  215. 'ref',
  216. $arrayType->items
  217. );
  218. $this->assertEquals(
  219. '#/components/schemas/TypedProperties',
  220. $arrayType->items->ref
  221. );
  222. $this->assertName($dateTime, [
  223. 'property' => 'dateTime',
  224. 'type' => 'string',
  225. 'format' => 'date-time',
  226. ]);
  227. $this->assertName($qualified, [
  228. 'property' => 'qualified',
  229. 'type' => 'string',
  230. 'format' => 'date-time',
  231. ]);
  232. $this->assertName($namespace, [
  233. 'property' => 'namespace',
  234. 'ref' => '#/components/schemas/TypedProperties',
  235. ]);
  236. $this->assertName($importedNamespace, [
  237. 'property' => 'importedNamespace',
  238. 'ref' => '#/components/schemas/TypedProperties',
  239. ]);
  240. $this->assertName($nativeTrumpsVar, [
  241. 'property' => 'nativeTrumpsVar',
  242. 'type' => 'string',
  243. ]);
  244. $this->assertName($annotationTrumpsNative, [
  245. 'property' => 'annotationTrumpsNative',
  246. 'type' => 'integer',
  247. ]);
  248. $this->assertName($annotationTrumpsAll, [
  249. 'property' => 'annotationTrumpsAll',
  250. 'type' => 'integer',
  251. ]);
  252. $this->assertName($undefined, [
  253. 'property' => 'undefined',
  254. 'type' => UNDEFINED,
  255. ]);
  256. $this->assertName($onlyAnnotated, [
  257. 'property' => 'onlyAnnotated',
  258. 'type' => 'integer',
  259. ]);
  260. $this->assertName($onlyVar, [
  261. 'property' => 'onlyVar',
  262. 'type' => 'integer',
  263. ]);
  264. $this->assertName($staticUndefined, [
  265. 'property' => 'staticUndefined',
  266. 'type' => UNDEFINED,
  267. ]);
  268. $this->assertName($staticString, [
  269. 'property' => 'staticString',
  270. 'type' => 'string',
  271. ]);
  272. $this->assertName($staticNullableString, [
  273. 'property' => 'staticNullableString',
  274. 'type' => 'string',
  275. ]);
  276. }
  277. /**
  278. * @param Property $property
  279. * @param array $expectedValues
  280. *
  281. * @return void
  282. */
  283. protected function assertName(Property $property, array $expectedValues)
  284. {
  285. foreach ($expectedValues as $key => $val) {
  286. $this->assertSame($val, $property->$key, '@OA\Property()->property based on propertyname');
  287. }
  288. }
  289. }