-
-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
7 changed files
with
316 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Knuckles\Scribe\Tests\Fixtures; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class TestPet extends Model | ||
{ | ||
|
||
public function owners() | ||
{ | ||
return $this->belongsToMany(TestUser::class)->withPivot('duration'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Knuckles\Scribe\Tests\Fixtures; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class TestPetApiResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* | ||
* @return array | ||
*/ | ||
public function toArray($request) | ||
{ | ||
$result = [ | ||
'id' => $this->id, | ||
'name' => $this->name, | ||
'species' => $this->species, | ||
'owners' => $this->whenLoaded('owners', function () { | ||
return TestUserApiResource::collection($this->owners); | ||
}), | ||
'ownership' => $this->whenPivotLoaded('pet_user', function () { | ||
return $this->pivot; | ||
}) | ||
]; | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Knuckles\Scribe\Tests\Fixtures; | ||
|
||
use Illuminate\Http\Resources\Json\ResourceCollection; | ||
|
||
class TestPetApiResourceCollection extends ResourceCollection | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* | ||
* @return array | ||
*/ | ||
public function toArray($request) | ||
{ | ||
$data = [ | ||
'data' => $this->collection, | ||
'links' => [ | ||
'self' => 'link-value', | ||
], | ||
]; | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
use Knuckles\Scribe\ScribeServiceProvider; | ||
use Knuckles\Scribe\Tests\BaseLaravelTest; | ||
use Knuckles\Scribe\Tests\Fixtures\TestController; | ||
use Knuckles\Scribe\Tests\Fixtures\TestPet; | ||
use Knuckles\Scribe\Tests\Fixtures\TestUser; | ||
use Knuckles\Scribe\Tools\DocumentationConfig; | ||
use Knuckles\Scribe\Tools\Utils; | ||
|
@@ -49,6 +50,13 @@ public function setUp(): void | |
}); | ||
$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 */ | ||
|
@@ -221,6 +229,209 @@ public function loads_specified_relations_for_generated_model() | |
], $results); | ||
} | ||
|
||
/** @test */ | ||
public function loads_specified_nested_relations_for_generated_model() | ||
{ | ||
$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])); | ||
|
||
$grandchild = Utils::getModelFactory(TestUser::class)->make(['id' => 6, 'parent_id' => 5]); | ||
$child->setRelation('children', collect([$grandchild])); | ||
} | ||
}); | ||
|
||
$config = new DocumentationConfig([]); | ||
|
||
$route = new Route(['POST'], "/somethingRandom", ['uses' => [TestController::class, 'dummy']]); | ||
|
||
$strategy = new UseApiResourceTags($config); | ||
$tags = [ | ||
new Tag('apiResource', '\Knuckles\Scribe\Tests\Fixtures\TestUserApiResource'), | ||
new Tag('apiResourceModel', '\Knuckles\Scribe\Tests\Fixtures\TestUser with=children.children'), | ||
]; | ||
$results = $strategy->getApiResourceResponse($strategy->getApiResourceTag($tags), $tags, ExtractedEndpointData::fromRoute($route)); | ||
|
||
$this->assertArraySubset([ | ||
[ | ||
'status' => 200, | ||
'content' => json_encode([ | ||
'data' => [ | ||
'id' => 4, | ||
'name' => 'Tested Again', | ||
'email' => '[email protected]', | ||
'children' => [ | ||
[ | ||
'id' => 5, | ||
'name' => 'Tested Again', | ||
'email' => '[email protected]', | ||
'children' => [ | ||
[ | ||
'id' => 6, | ||
'name' => 'Tested Again', | ||
'email' => '[email protected]', | ||
] | ||
] | ||
], | ||
], | ||
], | ||
]), | ||
], | ||
], $results); | ||
} | ||
|
||
/** @test */ | ||
public function loads_specified_many_to_many_relations_for_generated_model() | ||
{ | ||
$factory = app(\Illuminate\Database\Eloquent\Factory::class); | ||
$factory->afterMaking(TestUser::class, function (TestUser $user, $faker) { | ||
$pet = Utils::getModelFactory(TestPet::class)->make(['id' => 1]); | ||
$user->setRelation('pets', collect([$pet])); | ||
}); | ||
|
||
$config = new DocumentationConfig([]); | ||
|
||
$route = new Route(['POST'], "/somethingRandom", ['uses' => [TestController::class, 'dummy']]); | ||
|
||
$strategy = new UseApiResourceTags($config); | ||
$tags = [ | ||
new Tag('apiResource', '\Knuckles\Scribe\Tests\Fixtures\TestUserApiResource'), | ||
new Tag('apiResourceModel', '\Knuckles\Scribe\Tests\Fixtures\TestUser with=pets'), | ||
]; | ||
$results = $strategy->getApiResourceResponse($strategy->getApiResourceTag($tags), $tags, ExtractedEndpointData::fromRoute($route)); | ||
|
||
$this->assertArraySubset([ | ||
[ | ||
'status' => 200, | ||
'content' => json_encode([ | ||
'data' => [ | ||
'id' => 4, | ||
'name' => 'Tested Again', | ||
'email' => '[email protected]', | ||
'pets' => [ | ||
[ | ||
'id' => 1, | ||
'name' => 'Mephistopheles', | ||
'species' => 'dog' | ||
], | ||
], | ||
], | ||
]), | ||
], | ||
], $results); | ||
} | ||
|
||
/** @test */ | ||
public function loads_specified_many_to_many_and_nested_relations_for_generated_model() | ||
{ | ||
$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])); | ||
|
||
$pet = Utils::getModelFactory(TestPet::class)->make(['id' => 1]); | ||
$child->setRelation('pets', collect([$pet])); | ||
} | ||
}); | ||
|
||
$config = new DocumentationConfig([]); | ||
|
||
$route = new Route(['POST'], "/somethingRandom", ['uses' => [TestController::class, 'dummy']]); | ||
|
||
$strategy = new UseApiResourceTags($config); | ||
$tags = [ | ||
new Tag('apiResource', '\Knuckles\Scribe\Tests\Fixtures\TestUserApiResource'), | ||
new Tag('apiResourceModel', '\Knuckles\Scribe\Tests\Fixtures\TestUser with=children.pets'), | ||
]; | ||
$results = $strategy->getApiResourceResponse($strategy->getApiResourceTag($tags), $tags, ExtractedEndpointData::fromRoute($route)); | ||
|
||
$this->assertArraySubset([ | ||
[ | ||
'status' => 200, | ||
'content' => json_encode([ | ||
'data' => [ | ||
'id' => 4, | ||
'name' => 'Tested Again', | ||
'email' => '[email protected]', | ||
'children' => [ | ||
[ | ||
'id' => 5, | ||
'name' => 'Tested Again', | ||
'email' => '[email protected]', | ||
'pets' => [ | ||
[ | ||
'id' => 1, | ||
'name' => 'Mephistopheles', | ||
'species' => 'dog' | ||
], | ||
], | ||
] | ||
] | ||
|
||
], | ||
]), | ||
], | ||
], $results); | ||
} | ||
|
||
/** @test */ | ||
public function loads_specified_many_to_many_relations_for_generated_model_with_pivot() | ||
{ | ||
$factory = app(\Illuminate\Database\Eloquent\Factory::class); | ||
$factory->afterMaking(TestUser::class, function (TestUser $user, $faker) { | ||
$pet = Utils::getModelFactory(TestPet::class)->make(['id' => 1]); | ||
|
||
$pivot = $pet->newPivot($user, [ | ||
'pet_id' => $pet->id, | ||
'user_id' => $user->id, | ||
'duration' => 2 | ||
], 'pet_user', true); | ||
|
||
$pet->setRelation('pivot', $pivot); | ||
|
||
$user->setRelation('pets', collect([$pet])); | ||
}); | ||
|
||
$config = new DocumentationConfig([]); | ||
|
||
$route = new Route(['POST'], "/somethingRandom", ['uses' => [TestController::class, 'dummy']]); | ||
|
||
$strategy = new UseApiResourceTags($config); | ||
$tags = [ | ||
new Tag('apiResource', '\Knuckles\Scribe\Tests\Fixtures\TestUserApiResource'), | ||
new Tag('apiResourceModel', '\Knuckles\Scribe\Tests\Fixtures\TestUser with=pets'), | ||
]; | ||
$results = $strategy->getApiResourceResponse($strategy->getApiResourceTag($tags), $tags, ExtractedEndpointData::fromRoute($route)); | ||
|
||
$this->assertArraySubset([ | ||
[ | ||
'status' => 200, | ||
'content' => json_encode([ | ||
'data' => [ | ||
'id' => 4, | ||
'name' => 'Tested Again', | ||
'email' => '[email protected]', | ||
'pets' => [ | ||
[ | ||
'id' => 1, | ||
'name' => 'Mephistopheles', | ||
'species' => 'dog', | ||
'ownership' => [ | ||
'pet_id' => 1, | ||
'user_id' => 4, | ||
'duration' => 2 | ||
] | ||
], | ||
], | ||
], | ||
]), | ||
], | ||
], $results); | ||
} | ||
|
||
/** @test */ | ||
public function can_parse_apiresourcecollection_tags() | ||
{ | ||
|