Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POC] Add sample tests to show example of test-driven documentation workflow #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 2 additions & 44 deletions app/Http/Controllers/SideProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,19 @@
use League\Fractal\Manager;
use League\Fractal\Resource\Item;

/**
* @group Side Projects
*/

class SideProjectController extends Controller
{
public function __construct()
{
$this->middleware('auth')->only('store');
}

/**
* View all side projects
*
* This endpoint's response was gotten via a "response call"—
* Scribe called our API in a test environment to get a sample response.
*/
public function index()
{
return SideProject::all();
}

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

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

/**
* Update a side project
*
*/
public function update(Request $request, SideProject $sideProject)
{
//
}

/**
* Delete a side project
*
*/
public function destroy(SideProject $sideProject)
{
//
}

/**
* Finish a side project
*
* Hmmm.🤔
*/
public function finish(SideProject $sideProject)
{
//
Expand Down
50 changes: 1 addition & 49 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,83 +12,35 @@
*/
class UserController extends Controller
{
/**
* View all users
*
* This endpoint uses a custom Scribe strategy that parses a
* `@usesPagination` annotation to add some query parameters.
*
* The sample response is gotten by Scribe making a test API call (aka "response call").
*
* @usesPagination
*/
public function index()
{
return UserResource::collection(User::all());
}

/**
* Create a user
*
* This endpoint's body parameters are automatically generated from a FormRequest.
*/
public function store(CreateUserRequest $request)
{
/** @var User $user */
$user = User::create($request->validated());
$token = $user->createToken('default');
return ['user' => $user, 'token' => $token->plainTextToken];
return response(['user' => $user, 'token' => $token->plainTextToken], 201);
}

/**
* Authenticate
*
* Get a new API token.
*
* <aside>Yes, we know you can impersonate any user.🙄</aside>
*
* @response {"token": "2|KLDoUXc68Ko0JaFDZoX9qYkUqWglwdGxQsvTGBCg"}
* @responseField token The new API token. Valid forever.
*/
public function authenticate($id)
{
$token = User::findOrFail($id)->createToken('default');
return ['token' => $token->plainTextToken];
}

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

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
Expand Down
7 changes: 7 additions & 0 deletions app/Models/SideProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ class SideProject extends Model
{
use HasFactory;

protected $fillable = [
'name',
'description',
'due_at',
'user_id',
];

public function owner()
{
return $this->belongsTo(User::class, 'user_id');
Expand Down
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
"license": "MIT",
"require": {
"php": "^7.3|^8.0",
"ajcastro/scribe-tdd": "dev-master",
"fideloper/proxy": "^4.4",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
"knuckleswtf/scribe": "dev-master",
"laravel/framework": "^8.40",
"laravel/sanctum": "^2.11",
"laravel/tinker": "^2.5",
Expand All @@ -21,13 +23,16 @@
"laravel/sail": "^1.0.1",
"mockery/mockery": "^1.4.2",
"nunomaduro/collision": "^5.0",
"phpunit/phpunit": "^9.3.3",
"knuckleswtf/scribe": "@dev"
"phpunit/phpunit": "^9.3.3"
},
"repositories": [
{
"type": "path",
"url": "../scribe"
"url": "../ajcastro/scribe"
},
{
"type": "path",
"url": "../ajcastro/scribe-tdd"
}
],
"autoload": {
Expand Down
Loading