Skip to content

Commit

Permalink
more coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fokosun committed Aug 23, 2023
1 parent 368b10f commit 0700c55
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 8 deletions.
6 changes: 0 additions & 6 deletions app/Http/Controllers/RecipeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ public function addClap(Request $request)
return $this->service->addClap($request->get('recipe_id'));
}

/**
* @param Request $request
* @param JWT $jwtAuth
* @return \Illuminate\Http\JsonResponse
* @throws \Tymon\JWTAuth\Exceptions\JWTException
*/
public function myRecipes(Request $request, JWT $jwtAuth): \Illuminate\Http\JsonResponse
{
if ($jwtAuth->parseToken()->check()) {
Expand Down
2 changes: 1 addition & 1 deletion app/Services/RecipeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public function addClap($recipeId)
return response(
[
'updated' => true,
'claps' => $recipe->claps,
'claps' => $recipe->refresh()->claps,
], Response::HTTP_OK
);
}
Expand Down
2 changes: 1 addition & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@

Route::get('/categories', 'CategoryController@index');

Route::post('/add-clap', 'RecipeController@addClap');
Route::post('/add-clap', [RecipeController::class, 'addClap']);

Route::post('/comments', [CommentController::class, 'addComment']);

Expand Down
97 changes: 97 additions & 0 deletions tests/Feature/RecipeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\Recipe;
use App\Models\User;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;

class RecipeTest extends \TestCase
{
Expand Down Expand Up @@ -103,4 +104,100 @@ public function it_rejects_the_request_without_access_token()
$this->assertArrayHasKey('error', $decoded);
$this->assertSame("Your session has expired. Please login and try again.", $decoded["error"]);
}

/**
* @test
*/
public function it_can_increment_recipe_claps()
{
$user = User::factory()->make();

$user->save();

$cookbook = Cookbook::factory()->make([
'user_id' => $user->getKey()
]);

$cookbook->save();

$recipe = Recipe::factory()->make([
'cookbook_id' => $cookbook->refresh()->getKey(),
'user_id' => $user->getKey()
]);

$recipe->save();

$this->assertTrue($recipe->refresh()->claps == 0);

$this->json(
'POST',
'/api/v1/add-clap',
[
'recipe_id' => $recipe->refresh()->getKey()
]
)->assertStatus(200)
->assertExactJson([
'updated' => true,
'claps' => 1
]);

$this->assertTrue($recipe->refresh()->claps == 1);
}

/**
* @test
*/
public function it_cannot_clap_for_a_recipe_that_does_not_exist()
{
$this->json(
'POST',
'/api/v1/add-clap',
[
'recipe_id' => rand(1,10)
]
)->assertStatus(422)
->assertExactJson([
'recipe_id' => [
"The selected recipe id is invalid."
]
]);
}

public function it_can_show_my_recipes()
{
$user = User::factory()->make([
'email' => '[email protected]',
'password' => 'pass123'
]);
$user->save();

$token = Auth::attempt([
'email' => '[email protected]',
'password' => 'pass123'
]);

$cookbook = Cookbook::factory()->make([
'user_id' => $user->getKey()
]);

$cookbook->save();

$recipes = Recipe::factory()->count(3)->make([
'cookbook_id' => $cookbook->refresh()->getKey(),
'user_id' => $user->getKey()
]);

$recipes->map(function ($recipe) {
$recipe->save();
});

$this->json(
'GET',
'/api/v1/my/recipes',
[],
[
'Authorization' => 'Bearer ' . $token
]
)->assertStatus(200);
}
}

0 comments on commit 0700c55

Please sign in to comment.