Skip to content

Commit daa05b1

Browse files
committed
first commit
0 parents  commit daa05b1

File tree

6 files changed

+354
-0
lines changed

6 files changed

+354
-0
lines changed

Readme.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Laravel Stubs
2+
3+
This repository contains some stubs to simplify your life with Laravel.
4+
5+
Is required that your application has L5-Swagger setuped to correct run. If necessary, you can check this repository with all configuration, fully works and video demonstrations:
6+
7+
https://github.com/sr2ds/laravel-9-tutorial
8+
9+
# How to usage
10+
11+
## Download this files to your laravel application
12+
13+
```
14+
mkdir stubs
15+
cd stubs
16+
git clone https://github.com/sr2ds/laravel-api-stubs .
17+
```
18+
19+
## To generate resource api
20+
21+
```
22+
php artisan make:model -c -f -m --api -R --test Product
23+
```
24+
25+
## Additional configuration - After files generated
26+
27+
1. Is necessary change 3 `//@todo` lines in your tests files generated;
28+
2. Write your attributes in `migrations` and `Model`(fillable and on swagger block) and `Requests`;
29+
3. Create the route on `routes/api.php`

controller.model.api.stub

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
namespace {{ namespace }};
3+
4+
use {{ namespacedModel }};
5+
use {{ rootNamespace }}Http\Controllers\Controller;
6+
use {{ namespacedRequests }}
7+
use Illuminate\Http\Response;
8+
9+
/**
10+
* Class {{ class }}Controller
11+
* @package {{ namespace }}
12+
*/
13+
class {{ class }} extends Controller
14+
{
15+
/**
16+
* @OA\Get(
17+
* path="/{{ modelVariable }}s",
18+
* operationId="index{{ model }}",
19+
* tags={"{{ model }}s"},
20+
* summary="Get list of {{ model }}",
21+
* description="Returns list of {{ model }}",
22+
* @OA\Response(response=200, description="Successful operation",
23+
* @OA\JsonContent(ref="#/components/schemas/{{ model }}s"),
24+
* ),
25+
* )
26+
*
27+
* Display a listing of {{ model }}.
28+
* @return JsonResponse
29+
*/
30+
public function index()
31+
{
32+
${{ modelVariable }}s = {{ model }}::all();
33+
return response()->json(['data' => ${{ modelVariable }}s]);
34+
}
35+
36+
/**
37+
* @OA\Post(
38+
* operationId="store{{ model }}",
39+
* summary="Insert a new {{ model }}",
40+
* description="Insert a new {{ model }}",
41+
* tags={"{{ model }}s"},
42+
* path="/{{ modelVariable }}s",
43+
* @OA\RequestBody(
44+
* description="{{ model }} to create",
45+
* required=true,
46+
* @OA\MediaType(
47+
* mediaType="application/json",
48+
* @OA\Schema(
49+
* @OA\Property(
50+
* title="data",
51+
* property="data",
52+
* type="object",
53+
* ref="#/components/schemas/{{ model }}")
54+
* )
55+
* )
56+
* ),
57+
* @OA\Response(response="201",description="{{ model }} created",
58+
* @OA\JsonContent(
59+
* @OA\Property(
60+
* title="data",
61+
* property="data",
62+
* type="object",
63+
* ref="#/components/schemas/{{ model }}"
64+
* ),
65+
* ),
66+
* ),
67+
* @OA\Response(response=422,description="Validation exception"),
68+
* )
69+
*
70+
* @param {{ model }}Request $request
71+
* @return JsonResponse
72+
*/
73+
public function store({{ storeRequest }} $request)
74+
{
75+
$data = {{ model }}::create($request->validated('data'));
76+
return response()->json(['data' => $data], RESPONSE::HTTP_CREATED);
77+
}
78+
79+
/**
80+
* @OA\Get(
81+
* path="/{{ modelVariable }}s/{{{ modelVariable }}_id}",
82+
* summary="Show a {{ model }} from his Id",
83+
* description="Show a {{ model }} from his Id",
84+
* operationId="show{{ model }}",
85+
* tags={"{{ model }}s"},
86+
* @OA\Parameter(ref="#/components/parameters/{{ model }}--id"),
87+
* @OA\Response(
88+
* response=200,
89+
* description="Successful operation",
90+
* @OA\JsonContent(
91+
* @OA\Property(
92+
* title="data",
93+
* property="data",
94+
* type="object",
95+
* ref="#/components/schemas/{{ model }}"
96+
* ),
97+
* ),
98+
* ),
99+
* @OA\Response(response="404",description="{{ model }} not found"),
100+
* )
101+
*
102+
* @param {{ model }} ${{ model }}
103+
* @return JsonResponse
104+
*/
105+
public function show({{ model }} ${{ modelVariable }})
106+
{
107+
return response()->json(['data' => ${{ modelVariable }}]);
108+
}
109+
110+
/**
111+
* @OA\Patch(
112+
* operationId="update{{ model }}",
113+
* summary="Update an existing {{ model }}",
114+
* description="Update an existing {{ model }}",
115+
* tags={"{{ model }}s"},
116+
* path="/{{ modelVariable }}s/{{{ modelVariable }}_id}",
117+
* @OA\Parameter(ref="#/components/parameters/{{ model }}--id"),
118+
* @OA\Response(response="204",description="No content"),
119+
* @OA\RequestBody(
120+
* description="{{ model }} to update",
121+
* required=true,
122+
* @OA\MediaType(
123+
* mediaType="application/json",
124+
* @OA\Schema(
125+
* @OA\Property(
126+
* title="data",
127+
* property="data",
128+
* type="object",
129+
* ref="#/components/schemas/{{ model }}")
130+
* )
131+
* )
132+
* )
133+
* )
134+
*
135+
* @param Request $request
136+
* @param {{ model }} ${{ model }}
137+
* @return Response|JsonResponse
138+
*/
139+
public function update({{ updateRequest }} $request, {{ model }} ${{ modelVariable }})
140+
{
141+
${{ modelVariable }}->update($request->validated('data'));
142+
return response()->json(['data' => ${{ modelVariable }}->refresh()]);
143+
}
144+
145+
/**
146+
* @OA\Delete(
147+
* path="/{{ modelVariable }}s/{{{ modelVariable }}_id}",
148+
* summary="Delete a {{ model }}",
149+
* description="Delete a {{ model }}",
150+
* operationId="destroy{{ model }}",
151+
* tags={"{{ model }}s"},
152+
* @OA\Parameter(ref="#/components/parameters/{{ model }}--id"),
153+
* @OA\Response(response=204,description="No content"),
154+
* @OA\Response(response=404,description="{{ model }} not found"),
155+
* )
156+
*
157+
* @param {{ model }} ${{ model }}
158+
* @return Response|JsonResponse
159+
*/
160+
public function destroy({{ model }} ${{ modelVariable }})
161+
{
162+
${{ modelVariable }}->delete();
163+
return response()->noContent();
164+
}
165+
}

factory.stub

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace {{ factoryNamespace }};
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
7+
/**
8+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\{{ namespacedModel }}>
9+
*/
10+
class {{ factory }}Factory extends Factory
11+
{
12+
/**
13+
* Define the model's default state.
14+
*
15+
* @return array<string, mixed>
16+
*/
17+
public function definition()
18+
{
19+
return [
20+
//
21+
];
22+
}
23+
}

model.stub

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
namespace {{ namespace }};
3+
4+
use Illuminate\Database\Eloquent\Factories\HasFactory;
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
/**
8+
* @OA\Schema(
9+
* description="{{ class }} model",
10+
* title="{{ class }}",
11+
* required={},
12+
* @OA\Property(type="integer",description="id of {{ class }}",title="id",property="id",example="1",readOnly="true"),
13+
* @OA\Property(type="dateTime",title="created_at",property="created_at",example="2022-07-04T02:41:42.336Z",readOnly="true"),
14+
* @OA\Property(type="dateTime",title="updated_at",property="updated_at",example="2022-07-04T02:41:42.336Z",readOnly="true"),
15+
* )
16+
*
17+
* @OA\Schema(
18+
* schema="{{ class }}s",
19+
* title="{{ class }}s",
20+
* @OA\Property(title="data",property="data",type="array",
21+
* @OA\Items(type="object",ref="#/components/schemas/{{ class }}"),
22+
* )
23+
* )
24+
*
25+
* @OA\Parameter(
26+
* parameter="{{ class }}--id",
27+
* in="path",
28+
* name="{{ class }}_id",
29+
* required=true,
30+
* description="Id of {{ class }}",
31+
* @OA\Schema(
32+
* type="integer",
33+
* example="1",
34+
* )
35+
* ),
36+
*/
37+
38+
class {{ class }} extends Model
39+
{
40+
use HasFactory;
41+
42+
protected $fillable = [
43+
'created_at',
44+
'updated_at',
45+
];
46+
47+
protected $casts = [];
48+
}

request.stub

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class {{ class }} extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*
12+
* @return bool
13+
*/
14+
public function authorize()
15+
{
16+
return true;
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array
23+
*/
24+
public function rules()
25+
{
26+
return [
27+
//
28+
];
29+
}
30+
}

test.stub

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Tests\TestCase;
6+
7+
class {{ class }} extends TestCase
8+
{
9+
private $path = '/api/{{ class }}'; //@todo: set correct path
10+
private $model = \App\Models\{{ class }}::class; //@todo: set correct model class
11+
private $table = '{{ class }}'; //@todo: set correct table
12+
13+
public function test_create_{{ class }}()
14+
{
15+
$data = $this->model::factory()->make();
16+
17+
$this->postJson($this->path, ['data' => $data->toArray()])
18+
->assertCreated();
19+
20+
$this->assertDatabaseHas($this->table, $data->toArray());
21+
}
22+
23+
public function test_show_{{ class }}()
24+
{
25+
$data = $this->model::factory()->create();
26+
27+
$response = $this->getJson($this->path . '/' . $data->getRouteKey());
28+
$response->assertOk();
29+
$response->assertJsonFragment($data->toArray());
30+
}
31+
32+
public function test_update_{{ class }}()
33+
{
34+
$data = $this->model::factory()->create();
35+
$newData = $this->model::factory()->make();
36+
37+
$response = $this->putJson($this->path . '/' . $data->getRouteKey(), ['data' => $newData->toArray()]);
38+
$response->assertOk();
39+
$response->assertJsonFragment($newData->toArray());
40+
}
41+
42+
public function test_list_{{ class }}()
43+
{
44+
$this->model::factory()->count(10)->create();
45+
46+
$response = $this->get($this->path);
47+
$response->assertOk();
48+
$response->assertJsonCount(10, 'data');
49+
}
50+
51+
public function test_delete_{{ class }}()
52+
{
53+
$data = $this->model::factory()->create();
54+
$this->delete($this->path . '/' . $data->getRouteKey())
55+
->assertNoContent();
56+
57+
$this->assertDatabaseCount($this->table, 0);
58+
}
59+
}

0 commit comments

Comments
 (0)