Skip to content

Commit ed98597

Browse files
committed
Add sample tests to show example of test-driven documentation workflow
1 parent 4223672 commit ed98597

File tree

5 files changed

+228
-2
lines changed

5 files changed

+228
-2
lines changed

app/Http/Controllers/SideProjectController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function store(Request $request)
6868
* @transformer App\Http\Transformers\SideProjectTransformer
6969
* @transformerModel App\Models\SideProject with=owner
7070
*/
71-
public function show(SideProject $id)
71+
public function show(SideProject $sideProject)
7272
{
7373
$fractal = new Manager();
7474
$resource = new Item($sideProject, new SideProjectTransformer());

app/Http/Controllers/UserController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function store(CreateUserRequest $request)
3737
/** @var User $user */
3838
$user = User::create($request->validated());
3939
$token = $user->createToken('default');
40-
return ['user' => $user, 'token' => $token->plainTextToken];
40+
return response(['user' => $user, 'token' => $token->plainTextToken], 201);
4141
}
4242

4343
/**

app/Models/SideProject.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ class SideProject extends Model
99
{
1010
use HasFactory;
1111

12+
protected $fillable = [
13+
'name',
14+
'description',
15+
'due_at',
16+
'user_id',
17+
];
18+
1219
public function owner()
1320
{
1421
return $this->belongsTo(User::class, 'user_id');
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace Tests\Feature\Http\Controllers;
4+
5+
use App\Models\SideProject;
6+
use App\Models\User;
7+
use Tests\TestCase;
8+
9+
/**
10+
* @group Side Projects
11+
*/
12+
class SideProjectControllerTest extends TestCase
13+
{
14+
public function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
$this->actingAs(User::factory()->create());
19+
}
20+
21+
/**
22+
* View all side projects
23+
*
24+
* This endpoint's response was gotten via a "response call"—
25+
* Scribe called our API in a test environment to get a sample response.
26+
*/
27+
public function test_side_projects_index()
28+
{
29+
$response = $this->get(route('side_projects.index'));
30+
31+
$response->assertOk();
32+
}
33+
34+
/**
35+
* Start a new side project
36+
*
37+
* _Even though we both know you'll never finish it._
38+
*
39+
* This endpoint's body parameters were automatically generated by Scribe
40+
* from the controller's code. Check out the source! </aside>
41+
*
42+
* @authenticated
43+
*/
44+
public function test_side_projects_store()
45+
{
46+
$data = SideProject::factory()->make()->toArray();
47+
48+
$response = $this->post(route('side_projects.store'), $data);
49+
50+
$response->assertCreated();
51+
}
52+
53+
/**
54+
* View a side project
55+
*
56+
* This endpoint's response uses a Fractal transformer, so we tell Scribe that using an annotation,
57+
* and it figures out how to generate a sample. The 404 sample is gotten from a "response file".
58+
*
59+
* <aside class="success">Also, pretty cool: this endpoint's (and many others') URL parameters were figured out entirely by Scribe!</aside>
60+
*
61+
* @transformer App\Http\Transformers\SideProjectTransformer
62+
* @transformerModel App\Models\SideProject with=owner
63+
*/
64+
public function test_side_projects_show()
65+
{
66+
$user = SideProject::factory()->create();
67+
68+
$response = $this->get(route('side_projects.show', $user));
69+
70+
$response->assertOk();
71+
}
72+
73+
/**
74+
* Update a side project
75+
*
76+
*/
77+
public function test_side_projects_update()
78+
{
79+
$user = SideProject::factory()->create();
80+
$data = SideProject::factory()->make()->toArray();
81+
82+
$response = $this->put(route('side_projects.update', $user), $data);
83+
84+
$response->assertOk();
85+
}
86+
87+
/**
88+
* Delete a side project
89+
*
90+
*/
91+
public function test_side_projects_destroy()
92+
{
93+
$user = SideProject::factory()->create();
94+
95+
$response = $this->put(route('side_projects.destroy', $user));
96+
97+
$response->assertOk();
98+
}
99+
100+
/**
101+
* Finish a side project
102+
*
103+
* Hmmm.🤔
104+
*/
105+
public function test_side_projects_finish()
106+
{
107+
//
108+
}
109+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace Tests\Feature\Http\Controllers;
4+
5+
use App\Models\User;
6+
use Tests\TestCase;
7+
8+
/**
9+
* @group Users
10+
*/
11+
class UserControllerTest extends TestCase
12+
{
13+
/**
14+
* View all users
15+
*
16+
* This endpoint uses a custom Scribe strategy that parses a
17+
* `@usesPagination` annotation to add some query parameters.
18+
*
19+
* The sample response is gotten by Scribe making a test API call (aka "response call").
20+
*
21+
* @usesPagination
22+
*/
23+
public function test_users_index()
24+
{
25+
$response = $this->get('api/users');
26+
27+
$response->assertOk();
28+
}
29+
30+
/**
31+
* Create a user
32+
*
33+
* This endpoint's body parameters are automatically generated from a FormRequest.
34+
*/
35+
public function test_users_store()
36+
{
37+
$data = User::factory()->make()->toArray()+['password' => 'as123as35y'];
38+
39+
$response = $this->post('api/users', $data);
40+
41+
$response
42+
->assertCreated();
43+
}
44+
45+
/**
46+
* Authenticate
47+
*
48+
* Get a new API token.
49+
*
50+
* <aside>Yes, we know you can impersonate any user.🙄</aside>
51+
*
52+
* @response {"token": "2|KLDoUXc68Ko0JaFDZoX9qYkUqWglwdGxQsvTGBCg"}
53+
* @responseField token The new API token. Valid forever.
54+
*/
55+
public function test_users_authenticate()
56+
{
57+
$user = User::factory()->create();
58+
59+
$response = $this->post("api/users/{$user->id}/auth");
60+
61+
$response
62+
->assertOk()
63+
->assertJsonStructure([
64+
'token',
65+
]);
66+
}
67+
68+
/**
69+
* Fetch a user
70+
*
71+
* This endpoint's response uses an Eloquent API resource, so we tell Scribe that using an annotation,
72+
* and it figures out how to generate a sample. The 404 sample is gotten from a "response file".
73+
*
74+
* @apiResource App\Http\Resources\UserResource
75+
* @apiResourceModel App\Models\User with=sideProjects
76+
* @responseFile 404 scenario="User not found" responses/not_found.json {"resource": "user"}
77+
*/
78+
public function test_users_show()
79+
{
80+
$user = User::factory()->create();
81+
82+
$response = $this->get("api/users/{$user->id}");
83+
84+
$response
85+
->assertOk();
86+
}
87+
88+
/**
89+
* Update the specified resource in storage.
90+
*
91+
* @param \Illuminate\Http\Request $request
92+
* @param int $id
93+
* @return \Illuminate\Http\Response
94+
*/
95+
public function test_users_update()
96+
{
97+
//
98+
}
99+
100+
/**
101+
* Remove the specified resource from storage.
102+
*
103+
* @param int $id
104+
* @return \Illuminate\Http\Response
105+
*/
106+
public function test_users_destroy()
107+
{
108+
//
109+
}
110+
}

0 commit comments

Comments
 (0)