-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
34cc58c
commit da6b458
Showing
6 changed files
with
202 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
models: | ||
Date: | ||
born_at: date | ||
expires_at: datetime | ||
published_at: timestamp | ||
|
||
controllers: | ||
Date: | ||
resource: web |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
|
||
namespace Tests\Feature\Http\Controllers; | ||
|
||
use App\Models\Date; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Foundation\Testing\WithFaker; | ||
use Illuminate\Support\Carbon; | ||
use JMac\Testing\Traits\AdditionalAssertions; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Tests\TestCase; | ||
|
||
/** | ||
* @see \App\Http\Controllers\DateController | ||
*/ | ||
final class DateControllerTest extends TestCase | ||
{ | ||
use AdditionalAssertions, RefreshDatabase, WithFaker; | ||
|
||
#[Test] | ||
public function index_displays_view(): void | ||
{ | ||
$dates = Date::factory()->count(3)->create(); | ||
|
||
$response = $this->get(route('date.index')); | ||
|
||
$response->assertOk(); | ||
$response->assertViewIs('date.index'); | ||
$response->assertViewHas('dates'); | ||
} | ||
|
||
|
||
#[Test] | ||
public function create_displays_view(): void | ||
{ | ||
$response = $this->get(route('date.create')); | ||
|
||
$response->assertOk(); | ||
$response->assertViewIs('date.create'); | ||
} | ||
|
||
|
||
#[Test] | ||
public function store_uses_form_request_validation(): void | ||
{ | ||
$this->assertActionUsesFormRequest( | ||
\App\Http\Controllers\DateController::class, | ||
'store', | ||
\App\Http\Requests\DateStoreRequest::class | ||
); | ||
} | ||
|
||
#[Test] | ||
public function store_saves_and_redirects(): void | ||
{ | ||
$born_at = Carbon::parse($this->faker->date()); | ||
$expires_at = Carbon::parse($this->faker->dateTime()); | ||
$published_at = Carbon::parse($this->faker->dateTime()); | ||
|
||
$response = $this->post(route('date.store'), [ | ||
'born_at' => $born_at->toDateString(), | ||
'expires_at' => $expires_at->toDateTimeString(), | ||
'published_at' => $published_at->toDateTimeString(), | ||
]); | ||
|
||
$dates = Date::query() | ||
->where('born_at', $born_at) | ||
->where('expires_at', $expires_at) | ||
->where('published_at', $published_at) | ||
->get(); | ||
$this->assertCount(1, $dates); | ||
$date = $dates->first(); | ||
|
||
$response->assertRedirect(route('date.index')); | ||
$response->assertSessionHas('date.id', $date->id); | ||
} | ||
|
||
|
||
#[Test] | ||
public function show_displays_view(): void | ||
{ | ||
$date = Date::factory()->create(); | ||
|
||
$response = $this->get(route('date.show', $date)); | ||
|
||
$response->assertOk(); | ||
$response->assertViewIs('date.show'); | ||
$response->assertViewHas('date'); | ||
} | ||
|
||
|
||
#[Test] | ||
public function edit_displays_view(): void | ||
{ | ||
$date = Date::factory()->create(); | ||
|
||
$response = $this->get(route('date.edit', $date)); | ||
|
||
$response->assertOk(); | ||
$response->assertViewIs('date.edit'); | ||
$response->assertViewHas('date'); | ||
} | ||
|
||
|
||
#[Test] | ||
public function update_uses_form_request_validation(): void | ||
{ | ||
$this->assertActionUsesFormRequest( | ||
\App\Http\Controllers\DateController::class, | ||
'update', | ||
\App\Http\Requests\DateUpdateRequest::class | ||
); | ||
} | ||
|
||
#[Test] | ||
public function update_redirects(): void | ||
{ | ||
$date = Date::factory()->create(); | ||
$born_at = Carbon::parse($this->faker->date()); | ||
$expires_at = Carbon::parse($this->faker->dateTime()); | ||
$published_at = Carbon::parse($this->faker->dateTime()); | ||
|
||
$response = $this->put(route('date.update', $date), [ | ||
'born_at' => $born_at->toDateString(), | ||
'expires_at' => $expires_at->toDateTimeString(), | ||
'published_at' => $published_at->toDateTimeString(), | ||
]); | ||
|
||
$date->refresh(); | ||
|
||
$response->assertRedirect(route('date.index')); | ||
$response->assertSessionHas('date.id', $date->id); | ||
|
||
$this->assertEquals($born_at, $date->born_at); | ||
$this->assertEquals($expires_at, $date->expires_at); | ||
$this->assertEquals($published_at->timestamp, $date->published_at); | ||
} | ||
|
||
|
||
#[Test] | ||
public function destroy_deletes_and_redirects(): void | ||
{ | ||
$date = Date::factory()->create(); | ||
|
||
$response = $this->delete(route('date.destroy', $date)); | ||
|
||
$response->assertRedirect(route('date.index')); | ||
|
||
$this->assertModelMissing($date); | ||
} | ||
} |