123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- namespace Knuckles\Scribe\Tests\Strategies\Responses;
- use Illuminate\Routing\Route;
- use Knuckles\Camel\Extraction\ExtractedEndpointData;
- use Knuckles\Scribe\Attributes\Response;
- use Knuckles\Scribe\Attributes\ResponseFromApiResource;
- use Knuckles\Scribe\Attributes\ResponseFromFile;
- use Knuckles\Scribe\Attributes\ResponseFromTransformer;
- use Knuckles\Scribe\Extracting\Strategies\Responses\UseResponseAttributes;
- use Knuckles\Scribe\ScribeServiceProvider;
- use Knuckles\Scribe\Tests\BaseLaravelTest;
- use Knuckles\Scribe\Tests\Fixtures\TestModel;
- use Knuckles\Scribe\Tests\Fixtures\TestPet;
- use Knuckles\Scribe\Tests\Fixtures\TestTransformer;
- use Knuckles\Scribe\Tests\Fixtures\TestUser;
- use Knuckles\Scribe\Tests\Fixtures\TestUserApiResource;
- use Knuckles\Scribe\Tools\DocumentationConfig;
- use Knuckles\Scribe\Tools\Utils;
- use League\Fractal\Pagination\IlluminatePaginatorAdapter;
- use ReflectionClass;
- class UseResponseAttributesTest extends BaseLaravelTest
- {
- protected function getPackageProviders($app)
- {
- $providers = parent::getPackageProviders($app);
- if (class_exists(\Illuminate\Database\Eloquent\LegacyFactoryServiceProvider::class)) {
- $providers[] = \Illuminate\Database\Eloquent\LegacyFactoryServiceProvider ::class;
- }
- return $providers;
- }
- public function setUp(): void
- {
- parent::setUp();
- $this->setConfig(['database_connections_to_transact' => []]);
- $factory = app(\Illuminate\Database\Eloquent\Factory::class);
- $factory->define(TestUser::class, function () {
- return [
- 'id' => 4,
- 'first_name' => 'Tested',
- 'last_name' => 'Again',
- 'email' => 'a@b.com',
- ];
- });
- $factory->state(TestUser::class, 'state1', ["state1" => true]);
- $factory->state(TestUser::class, 'random-state', ["random-state" => true]);
- $factory->define(TestPet::class, function () {
- return [
- 'id' => 1,
- 'name' => 'Mephistopheles',
- 'species' => 'dog',
- ];
- });
- }
- /** @test */
- public function can_parse_plain_response_attributes()
- {
- $results = $this->fetch($this->endpoint("plainResponseAttributes"));
- $this->assertArraySubset([
- [
- 'status' => 200,
- 'content' => json_encode(["all" => "good"]),
- "description" => "Success"
- ],
- [
- 'status' => 201,
- 'content' => json_encode(["all" => "good"]),
- ],
- ], $results);
- }
- /** @test */
- public function can_parse_responsefile_attributes()
- {
- $results = $this->fetch($this->endpoint("responseFileAttributes"));
- $this->assertArraySubset([
- [
- 'status' => 401,
- 'content' => json_encode(["message" => "Unauthorized", "merge" => "this"]),
- ],
- ], $results);
- }
- /** @test */
- public function can_parse_apiresource_attributes()
- {
- $factory = app(\Illuminate\Database\Eloquent\Factory::class);
- $factory->afterMaking(TestUser::class, function (TestUser $user, $faker) {
- if ($user->id === 4) {
- $child = Utils::getModelFactory(TestUser::class)->make(['id' => 5, 'parent_id' => 4]);
- $user->setRelation('children', collect([$child]));
- }
- });
- $results = $this->fetch($this->endpoint("apiResourceAttributes"));
- $this->assertArraySubset([
- [
- 'status' => 200,
- 'content' => json_encode([
- 'data' => [
- [
- 'id' => 4,
- 'name' => 'Tested Again',
- 'email' => 'a@b.com',
- 'children' => [
- [
- 'id' => 5,
- 'name' => 'Tested Again',
- 'email' => 'a@b.com',
- ],
- ],
- 'state1' => true,
- 'random-state' => true,
- ],
- ],
- 'links' => [
- "first" => '/?page=1',
- "last" => null,
- "prev" => null,
- "next" => '/?page=2',
- ],
- "meta" => [
- "current_page" => 1,
- "from" => 1,
- "path" => '/',
- "per_page" => 1,
- "to" => 1,
- ],
- "a" => "b",
- ]),
- ],
- ], $results);
- }
- /** @test */
- public function can_parse_apiresource_attributes_with_no_model_specified()
- {
- $factory = app(\Illuminate\Database\Eloquent\Factory::class);
- $factory->afterMaking(TestUser::class, function (TestUser $user, $faker) {
- if ($user->id === 4) {
- $child = Utils::getModelFactory(TestUser::class)->make(['id' => 5, 'parent_id' => 4]);
- $user->setRelation('children', collect([$child]));
- }
- });
- $results = $this->fetch($this->endpoint("apiResourceAttributesWithNoModel"));
- $this->assertArraySubset([
- [
- 'status' => 200,
- 'content' => json_encode([
- 'data' => [
- [
- 'id' => 4,
- 'name' => 'Tested Again',
- 'email' => 'a@b.com',
- 'children' => [
- [
- 'id' => 5,
- 'name' => 'Tested Again',
- 'email' => 'a@b.com',
- ],
- ],
- 'state1' => true,
- 'random-state' => true,
- ],
- ],
- 'links' => [
- "first" => '/?page=1',
- "last" => null,
- "prev" => null,
- "next" => '/?page=2',
- ],
- "meta" => [
- "current_page" => 1,
- "from" => 1,
- "path" => '/',
- "per_page" => 1,
- "to" => 1,
- ],
- "a" => "b",
- ]),
- ],
- ], $results);
- }
- /** @test */
- public function can_parse_transformer_attributes()
- {
- $results = $this->fetch($this->endpoint("transformerAttributes"));
- $this->assertArraySubset([
- [
- 'status' => 200,
- 'content' => json_encode([
- "data" => [
- [
- "id" => 1,
- "description" => "Welcome on this test versions",
- "name" => "TestName",
- ],
- ],
- 'meta' => [
- "pagination" => [
- "total" => 2,
- "count" => 1,
- "per_page" => 1,
- "current_page" => 1,
- "total_pages" => 2,
- "links" => ["next" => "/?page=2"],
- ],
- ],
- ]),
- ],
- ], $results);
- }
- protected function fetch($endpoint): array
- {
- $strategy = new UseResponseAttributes(new DocumentationConfig([]));
- return $strategy($endpoint, []);
- }
- protected function endpoint(string $method): ExtractedEndpointData
- {
- $endpoint = new class extends ExtractedEndpointData {
- public function __construct(array $parameters = []) {}
- };
- $endpoint->controller = new ReflectionClass(ResponseAttributesTestController::class);
- $endpoint->method = $endpoint->controller->getMethod($method);
- $endpoint->route = new Route(['POST'], "/somethingRandom", ['uses' => [ResponseAttributesTestController::class, $method]]);
- return $endpoint;
- }
- }
- class ResponseAttributesTestController
- {
- #[Response(["all" => "good"], 200, "Success")]
- #[Response('{"all":"good"}', 201)]
- public function plainResponseAttributes()
- {
- }
- #[ResponseFromFile("tests/Fixtures/response_error_test.json", 401, ["merge" => "this"])]
- public function responseFileAttributes()
- {
- }
- #[ResponseFromApiResource(TestUserApiResource::class, TestUser::class, collection: true,
- factoryStates: ["state1", "random-state"], simplePaginate: 1, additional: ["a" => "b"])]
- public function apiResourceAttributes()
- {
- }
- #[ResponseFromApiResource(TestUserApiResource::class, collection: true,
- factoryStates: ["state1", "random-state"], simplePaginate: 1, additional: ["a" => "b"])]
- public function apiResourceAttributesWithNoModel()
- {
- }
- #[ResponseFromTransformer(TestTransformer::class, TestModel::class, collection: true,
- paginate: [IlluminatePaginatorAdapter::class, 1])]
- public function transformerAttributes()
- {
- }
- }
|