Skip to content

Commit cb89652

Browse files
committed
Working tdd examples
1 parent 4223672 commit cb89652

File tree

11 files changed

+1289
-1044
lines changed

11 files changed

+1289
-1044
lines changed

app/Http/Controllers/SideProjectController.php

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,19 @@
88
use League\Fractal\Manager;
99
use League\Fractal\Resource\Item;
1010

11-
/**
12-
* @group Side Projects
13-
*/
11+
1412
class SideProjectController extends Controller
1513
{
1614
public function __construct()
1715
{
1816
$this->middleware('auth')->only('store');
1917
}
2018

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-
*/
2719
public function index()
2820
{
2921
return SideProject::all();
3022
}
3123

32-
/**
33-
* Start a new side project
34-
*
35-
* _Even though we both know you'll never finish it._
36-
*
37-
* This endpoint's body parameters were automatically generated by Scribe
38-
* from the controller's code. Check out the source! </aside>
39-
*
40-
* @authenticated
41-
*/
4224
public function store(Request $request)
4325
{
4426
$validated = $request->validate([
@@ -57,47 +39,23 @@ public function store(Request $request)
5739
return SideProject::create($validated);
5840
}
5941

60-
/**
61-
* View a side project
62-
*
63-
* This endpoint's response uses a Fractal transformer, so we tell Scribe that using an annotation,
64-
* and it figures out how to generate a sample. The 404 sample is gotten from a "response file".
65-
*
66-
* <aside class="success">Also, pretty cool: this endpoint's (and many others') URL parameters were figured out entirely by Scribe!</aside>
67-
*
68-
* @transformer App\Http\Transformers\SideProjectTransformer
69-
* @transformerModel App\Models\SideProject with=owner
70-
*/
71-
public function show(SideProject $id)
42+
public function show(SideProject $sideProject)
7243
{
7344
$fractal = new Manager();
7445
$resource = new Item($sideProject, new SideProjectTransformer());
7546
return $fractal->createData($resource)->toArray();
7647
}
7748

78-
/**
79-
* Update a side project
80-
*
81-
*/
8249
public function update(Request $request, SideProject $sideProject)
8350
{
8451
//
8552
}
8653

87-
/**
88-
* Delete a side project
89-
*
90-
*/
9154
public function destroy(SideProject $sideProject)
9255
{
9356
//
9457
}
9558

96-
/**
97-
* Finish a side project
98-
*
99-
* Hmmm.🤔
100-
*/
10159
public function finish(SideProject $sideProject)
10260
{
10361
//

app/Http/Controllers/UserController.php

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,83 +12,35 @@
1212
*/
1313
class UserController extends Controller
1414
{
15-
/**
16-
* View all users
17-
*
18-
* This endpoint uses a custom Scribe strategy that parses a
19-
* `@usesPagination` annotation to add some query parameters.
20-
*
21-
* The sample response is gotten by Scribe making a test API call (aka "response call").
22-
*
23-
* @usesPagination
24-
*/
2515
public function index()
2616
{
2717
return UserResource::collection(User::all());
2818
}
2919

30-
/**
31-
* Create a user
32-
*
33-
* This endpoint's body parameters are automatically generated from a FormRequest.
34-
*/
3520
public function store(CreateUserRequest $request)
3621
{
3722
/** @var User $user */
3823
$user = User::create($request->validated());
3924
$token = $user->createToken('default');
40-
return ['user' => $user, 'token' => $token->plainTextToken];
25+
return response(['user' => $user, 'token' => $token->plainTextToken], 201);
4126
}
4227

43-
/**
44-
* Authenticate
45-
*
46-
* Get a new API token.
47-
*
48-
* <aside>Yes, we know you can impersonate any user.🙄</aside>
49-
*
50-
* @response {"token": "2|KLDoUXc68Ko0JaFDZoX9qYkUqWglwdGxQsvTGBCg"}
51-
* @responseField token The new API token. Valid forever.
52-
*/
5328
public function authenticate($id)
5429
{
5530
$token = User::findOrFail($id)->createToken('default');
5631
return ['token' => $token->plainTextToken];
5732
}
5833

59-
/**
60-
* Fetch a user
61-
*
62-
* This endpoint's response uses an Eloquent API resource, so we tell Scribe that using an annotation,
63-
* and it figures out how to generate a sample. The 404 sample is gotten from a "response file".
64-
*
65-
* @apiResource App\Http\Resources\UserResource
66-
* @apiResourceModel App\Models\User with=sideProjects
67-
* @responseFile 404 scenario="User not found" responses/not_found.json {"resource": "user"}
68-
*/
6934
public function show($id)
7035
{
7136
return new UserResource(User::findOrFail($id));
7237
}
7338

74-
/**
75-
* Update the specified resource in storage.
76-
*
77-
* @param \Illuminate\Http\Request $request
78-
* @param int $id
79-
* @return \Illuminate\Http\Response
80-
*/
8139
public function update(Request $request, $id)
8240
{
8341
//
8442
}
8543

86-
/**
87-
* Remove the specified resource from storage.
88-
*
89-
* @param int $id
90-
* @return \Illuminate\Http\Response
91-
*/
9244
public function destroy($id)
9345
{
9446
//

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');

composer.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
"license": "MIT",
77
"require": {
88
"php": "^7.3|^8.0",
9+
"ajcastro/scribe-tdd": "dev-master",
910
"fideloper/proxy": "^4.4",
1011
"fruitcake/laravel-cors": "^2.0",
1112
"guzzlehttp/guzzle": "^7.0.1",
13+
"knuckleswtf/scribe": "dev-master",
1214
"laravel/framework": "^8.40",
1315
"laravel/sanctum": "^2.11",
1416
"laravel/tinker": "^2.5",
@@ -21,13 +23,16 @@
2123
"laravel/sail": "^1.0.1",
2224
"mockery/mockery": "^1.4.2",
2325
"nunomaduro/collision": "^5.0",
24-
"phpunit/phpunit": "^9.3.3",
25-
"knuckleswtf/scribe": "@dev"
26+
"phpunit/phpunit": "^9.3.3"
2627
},
2728
"repositories": [
2829
{
2930
"type": "path",
30-
"url": "../scribe"
31+
"url": "../ajcastro/scribe"
32+
},
33+
{
34+
"type": "path",
35+
"url": "../ajcastro/scribe-tdd"
3136
}
3237
],
3338
"autoload": {

0 commit comments

Comments
 (0)