UseResponseAttributesTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Strategies\Responses;
  3. use Illuminate\Routing\Route;
  4. use Knuckles\Camel\Extraction\ExtractedEndpointData;
  5. use Knuckles\Scribe\Attributes\Response;
  6. use Knuckles\Scribe\Attributes\ResponseFromApiResource;
  7. use Knuckles\Scribe\Attributes\ResponseFromFile;
  8. use Knuckles\Scribe\Attributes\ResponseFromTransformer;
  9. use Knuckles\Scribe\Extracting\Strategies\Responses\UseResponseAttributes;
  10. use Knuckles\Scribe\ScribeServiceProvider;
  11. use Knuckles\Scribe\Tests\BaseLaravelTest;
  12. use Knuckles\Scribe\Tests\Fixtures\TestModel;
  13. use Knuckles\Scribe\Tests\Fixtures\TestPet;
  14. use Knuckles\Scribe\Tests\Fixtures\TestTransformer;
  15. use Knuckles\Scribe\Tests\Fixtures\TestUser;
  16. use Knuckles\Scribe\Tests\Fixtures\TestUserApiResource;
  17. use Knuckles\Scribe\Tools\DocumentationConfig;
  18. use Knuckles\Scribe\Tools\Utils;
  19. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  20. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  21. use ReflectionClass;
  22. class UseResponseAttributesTest extends BaseLaravelTest
  23. {
  24. use ArraySubsetAsserts;
  25. protected function getPackageProviders($app)
  26. {
  27. $providers = parent::getPackageProviders($app);
  28. if (class_exists(\Illuminate\Database\Eloquent\LegacyFactoryServiceProvider::class)) {
  29. $providers[] = \Illuminate\Database\Eloquent\LegacyFactoryServiceProvider ::class;
  30. }
  31. return $providers;
  32. }
  33. public function setUp(): void
  34. {
  35. parent::setUp();
  36. config(['scribe.database_connections_to_transact' => []]);
  37. $factory = app(\Illuminate\Database\Eloquent\Factory::class);
  38. $factory->define(TestUser::class, function () {
  39. return [
  40. 'id' => 4,
  41. 'first_name' => 'Tested',
  42. 'last_name' => 'Again',
  43. 'email' => 'a@b.com',
  44. ];
  45. });
  46. $factory->state(TestUser::class, 'state1', ["state1" => true]);
  47. $factory->state(TestUser::class, 'random-state', ["random-state" => true]);
  48. $factory->define(TestPet::class, function () {
  49. return [
  50. 'id' => 1,
  51. 'name' => 'Mephistopheles',
  52. 'species' => 'dog',
  53. ];
  54. });
  55. }
  56. /** @test */
  57. public function can_parse_plain_response_attributes()
  58. {
  59. $results = $this->fetch($this->endpoint("plainResponseAttributes"));
  60. $this->assertArraySubset([
  61. [
  62. 'status' => 200,
  63. 'content' => json_encode(["all" => "good"]),
  64. "description" => "Success"
  65. ],
  66. [
  67. 'status' => 201,
  68. 'content' => json_encode(["all" => "good"]),
  69. ],
  70. ], $results);
  71. }
  72. /** @test */
  73. public function can_parse_responsefile_attributes()
  74. {
  75. $results = $this->fetch($this->endpoint("responseFileAttributes"));
  76. $this->assertArraySubset([
  77. [
  78. 'status' => 401,
  79. 'content' => json_encode(["message" => "Unauthorized", "merge" => "this"]),
  80. ],
  81. ], $results);
  82. }
  83. /** @test */
  84. public function can_parse_apiresource_attributes()
  85. {
  86. $factory = app(\Illuminate\Database\Eloquent\Factory::class);
  87. $factory->afterMaking(TestUser::class, function (TestUser $user, $faker) {
  88. if ($user->id === 4) {
  89. $child = Utils::getModelFactory(TestUser::class)->make(['id' => 5, 'parent_id' => 4]);
  90. $user->setRelation('children', collect([$child]));
  91. }
  92. });
  93. $results = $this->fetch($this->endpoint("apiResourceAttributes"));
  94. $this->assertArraySubset([
  95. [
  96. 'status' => 200,
  97. 'content' => json_encode([
  98. 'data' => [
  99. [
  100. 'id' => 4,
  101. 'name' => 'Tested Again',
  102. 'email' => 'a@b.com',
  103. 'children' => [
  104. [
  105. 'id' => 5,
  106. 'name' => 'Tested Again',
  107. 'email' => 'a@b.com',
  108. ],
  109. ],
  110. 'state1' => true,
  111. 'random-state' => true,
  112. ],
  113. ],
  114. 'links' => [
  115. "first" => '/?page=1',
  116. "last" => null,
  117. "prev" => null,
  118. "next" => '/?page=2',
  119. ],
  120. "meta" => [
  121. "current_page" => 1,
  122. "from" => 1,
  123. "path" => '/',
  124. "per_page" => 1,
  125. "to" => 1,
  126. ],
  127. "a" => "b",
  128. ]),
  129. ],
  130. ], $results);
  131. }
  132. /** @test */
  133. public function can_parse_transformer_attributes()
  134. {
  135. $results = $this->fetch($this->endpoint("transformerAttributes"));
  136. $this->assertArraySubset([
  137. [
  138. 'status' => 200,
  139. 'content' => json_encode([
  140. "data" => [
  141. [
  142. "id" => 1,
  143. "description" => "Welcome on this test versions",
  144. "name" => "TestName",
  145. ],
  146. ],
  147. 'meta' => [
  148. "pagination" => [
  149. "total" => 2,
  150. "count" => 1,
  151. "per_page" => 1,
  152. "current_page" => 1,
  153. "total_pages" => 2,
  154. "links" => ["next" => "/?page=2"],
  155. ],
  156. ],
  157. ]),
  158. ],
  159. ], $results);
  160. }
  161. protected function fetch($endpoint): array
  162. {
  163. $strategy = new UseResponseAttributes(new DocumentationConfig([]));
  164. return $strategy($endpoint, []);
  165. }
  166. protected function endpoint(string $method): ExtractedEndpointData
  167. {
  168. $endpoint = new class extends ExtractedEndpointData {
  169. public function __construct(array $parameters = []) {}
  170. };
  171. $endpoint->controller = new ReflectionClass(ResponseAttributesTestController::class);
  172. $endpoint->method = $endpoint->controller->getMethod($method);
  173. $endpoint->route = new Route(['POST'], "/somethingRandom", ['uses' => [ResponseAttributesTestController::class, $method]]);
  174. return $endpoint;
  175. }
  176. }
  177. class ResponseAttributesTestController
  178. {
  179. #[Response(["all" => "good"], 200, "Success")]
  180. #[Response('{"all":"good"}', 201)]
  181. public function plainResponseAttributes()
  182. {
  183. }
  184. #[ResponseFromFile("tests/Fixtures/response_error_test.json", 401, ["merge" => "this"])]
  185. public function responseFileAttributes()
  186. {
  187. }
  188. #[ResponseFromApiResource(TestUserApiResource::class, TestUser::class, collection: true,
  189. factoryStates: ["state1", "random-state"], simplePaginate: 1, additional: ["a" => "b"])]
  190. public function apiResourceAttributes()
  191. {
  192. }
  193. #[ResponseFromTransformer(TestTransformer::class, TestModel::class, collection: true,
  194. paginate: [IlluminatePaginatorAdapter::class, 1])]
  195. public function transformerAttributes()
  196. {
  197. }
  198. }