UseResponseAttributesTest.php 7.4 KB

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