From 0700c55717fad93721e06fa14762aaac87633fe9 Mon Sep 17 00:00:00 2001 From: fokosun Date: Mon, 21 Aug 2023 19:15:19 -0400 Subject: [PATCH] more coverage --- app/Http/Controllers/RecipeController.php | 6 -- app/Services/RecipeService.php | 2 +- routes/api.php | 2 +- tests/Feature/RecipeTest.php | 97 +++++++++++++++++++++++ 4 files changed, 99 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/RecipeController.php b/app/Http/Controllers/RecipeController.php index 03df8bb7..21ea34a6 100755 --- a/app/Http/Controllers/RecipeController.php +++ b/app/Http/Controllers/RecipeController.php @@ -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()) { diff --git a/app/Services/RecipeService.php b/app/Services/RecipeService.php index 99ee4d71..e34a8774 100755 --- a/app/Services/RecipeService.php +++ b/app/Services/RecipeService.php @@ -219,7 +219,7 @@ public function addClap($recipeId) return response( [ 'updated' => true, - 'claps' => $recipe->claps, + 'claps' => $recipe->refresh()->claps, ], Response::HTTP_OK ); } diff --git a/routes/api.php b/routes/api.php index 6469ef54..5b597e6f 100755 --- a/routes/api.php +++ b/routes/api.php @@ -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']); diff --git a/tests/Feature/RecipeTest.php b/tests/Feature/RecipeTest.php index 0c78c022..9c831d09 100755 --- a/tests/Feature/RecipeTest.php +++ b/tests/Feature/RecipeTest.php @@ -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 { @@ -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' => 'evan.reid@123.com', + 'password' => 'pass123' + ]); + $user->save(); + + $token = Auth::attempt([ + 'email' => 'evan.reid@123.com', + '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); + } }