|
@@ -2,8 +2,10 @@
|
|
|
|
|
|
namespace Knuckles\Scribe\Tests\Strategies\Responses;
|
|
|
|
|
|
+use Illuminate\Database\Schema\Blueprint;
|
|
|
use Illuminate\Foundation\Application;
|
|
|
use Illuminate\Routing\Route;
|
|
|
+use Illuminate\Support\Facades\Schema;
|
|
|
use Knuckles\Camel\Extraction\ExtractedEndpointData;
|
|
|
use Knuckles\Scribe\Attributes\Response;
|
|
|
use Knuckles\Scribe\Attributes\ResponseFromApiResource;
|
|
@@ -285,7 +287,95 @@ class UseResponseAttributesTest extends BaseLaravelTest
|
|
|
], $results);
|
|
|
}
|
|
|
|
|
|
- protected function fetch($endpoint): array
|
|
|
+ /** @test */
|
|
|
+ public function can_parse_apiresource_attributes_and_load_children_using_factory_create()
|
|
|
+ {
|
|
|
+ Schema::create('test_users', function (Blueprint $table) {
|
|
|
+ $table->id();
|
|
|
+ $table->string('first_name');
|
|
|
+ $table->string('last_name');
|
|
|
+ $table->string('email');
|
|
|
+ $table->integer('parent_id')->nullable();
|
|
|
+ });
|
|
|
+
|
|
|
+ $factory = app(\Illuminate\Database\Eloquent\Factory::class);
|
|
|
+ $factory->afterCreating(TestUser::class, function (TestUser $user, $faker) {
|
|
|
+ if ($user->id === 4) {
|
|
|
+ Utils::getModelFactory(TestUser::class)->create(['id' => 5, 'parent_id' => 4]);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ $documentationConfig = ['examples' => ['models_source' => ['factoryCreate']]];
|
|
|
+
|
|
|
+ $results = $this->fetch($this->endpoint("apiResourceAttributesIncludeChildren"), $documentationConfig);
|
|
|
+ $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",
|
|
|
+ ]
|
|
|
+ ],
|
|
|
+ ],
|
|
|
+ ]),
|
|
|
+ ],
|
|
|
+ ], $results);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /** @test */
|
|
|
+ public function can_parse_apiresource_attributes_and_load_children_and_children_count_using_factory_create()
|
|
|
+ {
|
|
|
+ if (version_compare(Application::VERSION, '9', '<')) {
|
|
|
+ $this->markTestSkipped('The whenCounted method in JsonResource requires Laravel 9 or higher.');
|
|
|
+ }
|
|
|
+
|
|
|
+ Schema::create('test_users', function (Blueprint $table) {
|
|
|
+ $table->id();
|
|
|
+ $table->string('first_name');
|
|
|
+ $table->string('last_name');
|
|
|
+ $table->string('email');
|
|
|
+ $table->integer('parent_id')->nullable();
|
|
|
+ });
|
|
|
+
|
|
|
+ $factory = app(\Illuminate\Database\Eloquent\Factory::class);
|
|
|
+ $factory->afterCreating(TestUser::class, function (TestUser $user, $faker) {
|
|
|
+ if ($user->id === 4) {
|
|
|
+ Utils::getModelFactory(TestUser::class)->create(['id' => 5, 'parent_id' => 4]);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ $documentationConfig = ['examples' => ['models_source' => ['factoryCreate']]];
|
|
|
+
|
|
|
+ $results = $this->fetch($this->endpoint("apiResourceAttributesIncludeChildrenAndChildrenCount"), $documentationConfig);
|
|
|
+ $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",
|
|
|
+ ]
|
|
|
+ ],
|
|
|
+ 'children_count' => 1,
|
|
|
+ ],
|
|
|
+ ]),
|
|
|
+ ],
|
|
|
+ ], $results);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function fetch($endpoint, array $documentationConfig = []): array
|
|
|
{
|
|
|
$strategy = new UseResponseAttributes(new DocumentationConfig([]));
|
|
|
return $strategy($endpoint, []);
|
|
@@ -345,4 +435,16 @@ class ResponseAttributesTestController
|
|
|
{
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ #[ResponseFromApiResource(TestUserApiResource::class, with: ['children'], withCount: ['children'])]
|
|
|
+ public function apiResourceAttributesIncludeChildrenAndChildrenCount()
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ #[ResponseFromApiResource(TestUserApiResource::class, with: ['children'])]
|
|
|
+ public function apiResourceAttributesIncludeChildren()
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
}
|