From a686e8036bbdade24b44e256d60b22bd920002a7 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Tue, 30 Mar 2021 14:42:45 -0700 Subject: [PATCH] Add basic Journal Entry controller tests --- .../Controllers/JournalEntryController.php | 11 +-- .../views/journal-entries/delete.blade.php | 10 +-- routes/web.php | 2 +- .../Controllers/HttpControllerTestCase.php | 5 +- .../JournalEntryControllerTest.php | 77 +++++++++++++++++++ 5 files changed, 90 insertions(+), 15 deletions(-) create mode 100644 tests/Feature/Http/Controllers/JournalEntryControllerTest.php diff --git a/app/Http/Controllers/JournalEntryController.php b/app/Http/Controllers/JournalEntryController.php index 62b161b..6bf2dc3 100644 --- a/app/Http/Controllers/JournalEntryController.php +++ b/app/Http/Controllers/JournalEntryController.php @@ -248,20 +248,21 @@ class JournalEntryController extends Controller /** * Confirm removal of the specified resource. */ - public function delete(JournalEntry $journalEntry): View + public function delete(JournalEntry $journal_entry): View { - return view('journal-entries.delete')->with('entry', $journalEntry); + return view('journal-entries.delete') + ->with('journal_entry', $journal_entry); } /** * Remove the specified resource from storage. */ - public function destroy(JournalEntry $journalEntry): RedirectResponse + public function destroy(JournalEntry $journal_entry): RedirectResponse { - $journalEntry->delete(); + $journal_entry->delete(); session()->flash('message', 'Journal entry deleted!'); return redirect(route('journal-entries.index', [ - 'date' => $journalEntry->date->toDateString() + 'date' => $journal_entry->date->toDateString() ])); } } diff --git a/resources/views/journal-entries/delete.blade.php b/resources/views/journal-entries/delete.blade.php index aec7590..411f38f 100644 --- a/resources/views/journal-entries/delete.blade.php +++ b/resources/views/journal-entries/delete.blade.php @@ -2,25 +2,25 @@ Delete Entry

- Delete {{ $entry->summary }}? + Delete {{ $journal_entry->summary }}?

-
+ @method('delete') @csrf
Are you sure what to delete this journal entry?
Summay: - {{ $entry->summary }} + {{ $journal_entry->summary }}
Date: - {{ $entry->date->format('D, j M Y') }} + {{ $journal_entry->date->format('D, j M Y') }}
Meal: - {{ $entry->meal }} + {{ $journal_entry->meal }}
diff --git a/routes/web.php b/routes/web.php index 821966f..974da20 100644 --- a/routes/web.php +++ b/routes/web.php @@ -39,7 +39,7 @@ Route::get('/ingredient-picker/search', [IngredientPickerController::class, 'sea Route::get('/journal-entries/create/from-nutrients', [JournalEntryController::class, 'createFromNutrients'])->middleware(['auth'])->name('journal-entries.create.from-nutrients'); Route::post('/journal-entries/create/from-nutrients', [JournalEntryController::class, 'storeFromNutrients'])->middleware(['auth'])->name('journal-entries.store.from-nutrients'); Route::resource('journal-entries', JournalEntryController::class)->middleware(['auth']); -Route::get('/journal-entries/{journalEntry}/delete', [JournalEntryController::class, 'delete'])->middleware(['auth'])->name('journal-entries.delete'); +Route::get('/journal-entries/{journal_entry}/delete', [JournalEntryController::class, 'delete'])->middleware(['auth'])->name('journal-entries.delete'); // Recipes. Route::resource('recipes', RecipeController::class)->middleware(['auth']); diff --git a/tests/Feature/Http/Controllers/HttpControllerTestCase.php b/tests/Feature/Http/Controllers/HttpControllerTestCase.php index b4dad6d..ae353cb 100644 --- a/tests/Feature/Http/Controllers/HttpControllerTestCase.php +++ b/tests/Feature/Http/Controllers/HttpControllerTestCase.php @@ -48,7 +48,6 @@ abstract class HttpControllerTestCase extends LoggedInTestCase $response = $this->followingRedirects()->post($store_url, $instance->toArray()); $response->assertOk(); $response->assertSessionHasNoErrors(); - $response->assertViewHas($this->routeKey()); } public function testCanViewInstance(): void @@ -87,9 +86,7 @@ abstract class HttpControllerTestCase extends LoggedInTestCase $response = $this->followingRedirects()->delete($destroy_url); $response->assertOk(); - $view_url = action([$this->class(), 'show'], [$this->routeKey() => $instance]); - $response = $this->get($view_url); - $response->assertNotFound(); + $this->assertNull($instance->fresh()); } } diff --git a/tests/Feature/Http/Controllers/JournalEntryControllerTest.php b/tests/Feature/Http/Controllers/JournalEntryControllerTest.php new file mode 100644 index 0000000..3576f0d --- /dev/null +++ b/tests/Feature/Http/Controllers/JournalEntryControllerTest.php @@ -0,0 +1,77 @@ +factory()->for($this->user)->create(); + } + + /** + * @doesNotPerformAssertions + */ + public function testCanViewInstance(): void { + $this->setName('can *not* view instance'); + // Journal entries are not independently viewable. + } + + /** + * @doesNotPerformAssertions + */ + public function testCanEditInstance(): void { + $this->setName('can *not* edit instance'); + // Journal entries are not editable. + } + + public function testCanAddInstance(): void + { + $create_url = action([$this->class(), 'createFromNutrients']); + $response = $this->get($create_url); + $response->assertOk(); + $instance = $this->factory()->make(); + $store_url = action([$this->class(), 'storeFromNutrients']); + $response = $this->followingRedirects()->post($store_url, $instance->toArray()); + $response->assertOk(); + $response->assertSessionHasNoErrors(); + } + +}