-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Fernando Piovezan
committed
Jun 8, 2024
1 parent
349075c
commit 8c2833f
Showing
9 changed files
with
307 additions
and
105 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
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 |
---|---|---|
|
@@ -510,4 +510,98 @@ public function testPathWithGlobalPattern() | |
|
||
$this->assertSame($expected, $pathParameter); | ||
} | ||
|
||
|
||
public function testGenerateExampleToFactory() | ||
{ | ||
config(['request-docs.factory_path' => 'Rakutentech\LaravelRequestDocs\Tests\Factories']); | ||
config(['request-docs.use_factory' => true]); | ||
config(['request-docs.pattern_model_from_controller_name' => '/Controller$/']); | ||
Route::post('api/v1/users/store', [UserController::class, 'store']); | ||
|
||
$response = $this->get(route('request-docs.api')) | ||
->assertStatus(Response::HTTP_OK); | ||
$docs = collect($response->json()); | ||
$userRoute = $docs | ||
->filter(fn(array $item) => Str::startsWith($item['uri'], ['api'])); | ||
$examples = $userRoute->pluck('examples')->toArray(); | ||
$expected = [\Rakutentech\LaravelRequestDocs\Tests\Factories\UserFactory::new()->make()->toArray()]; | ||
|
||
$this->assertSame($expected, $examples); | ||
} | ||
|
||
public function testGenerateFieldDescriptionAndExamples() | ||
{ | ||
config(['request-docs.factory_path' => 'Rakutentech\LaravelRequestDocs\Tests\Factories']); | ||
config(['request-docs.use_factory' => true]); | ||
config(['request-docs.pattern_model_from_controller_name' => '/Controller$/']); | ||
Route::post('api/v1/users/store', [UserController::class, 'store']); | ||
|
||
$response = $this->get(route('request-docs.api')) | ||
->assertStatus(Response::HTTP_OK); | ||
$docs = collect($response->json()); | ||
$userRoute = $docs | ||
->filter(fn(array $item) => Str::startsWith($item['uri'], ['api'])); | ||
$fieldInfo = $userRoute->pluck('field_info')->get(0); | ||
$expected = [ | ||
'name' => [ | ||
'description' => 'User Name', | ||
'example' => 'John Doe' | ||
], | ||
'email' => [ | ||
'description' => 'User email', | ||
'example' => '[email protected]' | ||
] | ||
]; | ||
|
||
$this->assertSame($expected, $fieldInfo); | ||
} | ||
|
||
|
||
public function testSummaryAndDescriptionMethod() | ||
{ | ||
Route::post('api/v1/users/store', [UserController::class, 'store']); | ||
|
||
$response = $this->get(route('request-docs.api')) | ||
->assertStatus(Response::HTTP_OK); | ||
$docs = collect($response->json()); | ||
$userRoute = $docs | ||
->filter(fn(array $item) => Str::startsWith($item['uri'], ['api'])); | ||
$summary = $userRoute->pluck('summary')->get(0); | ||
$description = $userRoute->pluck('description')->get(0); | ||
|
||
$expectedSummary = 'Store a newly created resource in storage.'; | ||
$expectedDescription = 'This method creates a user when validations are met.'; | ||
|
||
$this->assertSame($expectedSummary, $summary); | ||
$this->assertSame($description, $expectedDescription); | ||
} | ||
|
||
public function testRuleOrder() | ||
{ | ||
Route::post('api/v1/users/store', [UserController::class, 'store']); | ||
|
||
$response = $this->get(route('request-docs.api')) | ||
->assertStatus(Response::HTTP_OK); | ||
$docs = collect($response->json()); | ||
$userRoute = $docs | ||
->filter(fn(array $item) => Str::startsWith($item['uri'], ['api'])); | ||
$rulesOrder = $userRoute->pluck('rules_order')->get(0); | ||
|
||
$this->assertSame(config('request-docs.rules_order'), $rulesOrder); | ||
} | ||
|
||
public function testTag() | ||
{ | ||
Route::post('api/v1/users/store', [UserController::class, 'store']); | ||
|
||
$response = $this->get(route('request-docs.api')) | ||
->assertStatus(Response::HTTP_OK); | ||
$docs = collect($response->json()); | ||
$userRoute = $docs | ||
->filter(fn(array $item) => Str::startsWith($item['uri'], ['api'])); | ||
$tag = $userRoute->pluck('tag')->get(0); | ||
|
||
$this->assertSame('User', $tag); | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
namespace Rakutentech\LaravelRequestDocs\Tests\Factories; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use Rakutentech\LaravelRequestDocs\Tests\Models\User; | ||
|
||
class UserFactory extends Factory | ||
{ | ||
/** | ||
* The name of the factory's corresponding model. | ||
* | ||
* @var class-string<User> | ||
*/ | ||
protected $model = User::class; | ||
|
||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function definition() | ||
{ | ||
return [ | ||
'name' => 'John Doe', | ||
'email' => '[email protected]', | ||
]; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Rakutentech\LaravelRequestDocs\Tests\Stubs\TestRequests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class UserStoreRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
protected function prepareForValidation() | ||
{ | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'name' => 'required|string', | ||
'email' => 'required|email', | ||
]; | ||
} | ||
|
||
/** | ||
* Provides a detailed description of the expected parameters | ||
* in the body of an HTTP request. | ||
*/ | ||
public function fieldDescriptions(): array | ||
{ | ||
return [ | ||
'name' => 'User Name', | ||
'email' => 'User email', | ||
]; | ||
} | ||
} |
Oops, something went wrong.