intervle - tests/Feature/GameControllerFeatureTest.php
<?php
namespace Tests\Feature;
use App\Models\Event;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class GameControllerFeatureTest extends TestCase
{
private static string $currentDateString;
private static string $pastDateString;
private static string $futureDateString;
private static string $easyDifficulty;
private static string $mediumDifficulty;
private static string $hardDifficulty;
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
self::$currentDateString = '/' . Carbon::now()->format('Ymd');
self::$pastDateString = '/' . Carbon::now()->subYear()->format('Ymd');
self::$futureDateString = '/' . Carbon::now()->addYear()->format('Ymd');
self::$easyDifficulty = '/easy';
self::$mediumDifficulty = '/medium';
self::$hardDifficulty = '/hard';
}
public function testItReturnsFiveEventsIfEasy(): void
{
Event::factory()->count(10)->create();
$response = $this->get(self::$easyDifficulty . self::$currentDateString);
$response->assertStatus(200);
$response->assertViewHas('events', function ($events) {
return $events->count() === 5;
});
}
public function testItReturnsSevenEventsIfMedium(): void
{
Event::factory()->count(10)->create();
$response = $this->get(self::$mediumDifficulty . self::$currentDateString);
$response->assertStatus(200);
$response->assertViewHas('events', function ($events) {
return $events->count() === 7;
});
}
public function testItReturnsTenEventsIfHard(): void
{
Event::factory()->count(10)->create();
$response = $this->get(self::$hardDifficulty . self::$currentDateString);
$response->assertStatus(200);
$response->assertViewHas('events', function ($events) {
return $events->count() === 10;
});
}
public function testItReturnsDifferentEventsForDifferentDates(): void
{
Event::factory()->count(10)->create();
$response_a = $this->get(self::$easyDifficulty . self::$currentDateString);
$response_b = $this->get(self::$easyDifficulty . self::$pastDateString);
$response_a->assertStatus(200);
$response_b->assertStatus(200);
$events_a = $response_a->viewData('events')->pluck('id')->toArray();
$events_b = $response_b->viewData('events')->pluck('id')->toArray();
$this->assertNotEquals($events_a, $events_b, 'Events do not differ for different dates');
}
public function testItReturnsDifferentEventsForDifferentDifficulties(): void
{
Event::factory()->count(10)->create();
$response_a = $this->get(self::$easyDifficulty . self::$currentDateString);
$response_b = $this->get(self::$mediumDifficulty . self::$currentDateString);
$response_a->assertStatus(200);
$response_b->assertStatus(200);
$events_a = $response_a->viewData('events')->pluck('id')->toArray();
$events_b = $response_b->viewData('events')->pluck('id')->toArray();
$this->assertNotEquals($events_a, $events_b, 'Events do not differ for different dates');
}
public function testItReturnsSameEventsAfterEventsTableGrows(): void
{
Event::factory()->count(10)->create();
$response_a = $this->get(self::$easyDifficulty . self::$currentDateString);
Event::factory()->count(10)->create();
$response_b = $this->get(self::$easyDifficulty . self::$currentDateString);
$response_a->assertStatus(200);
$response_b->assertStatus(200);
$events_a = $response_a->viewData('events')->pluck('id')->toArray();
$events_b = $response_b->viewData('events')->pluck('id')->toArray();
$this->assertEquals($events_a, $events_b, 'Events do not match after more events added to table');
}
public function testItFailsIfADateAfterTodayIsRequested(): void
{
Event::factory()->count(10)->create();
$response = $this->get(self::$easyDifficulty . self::$futureDateString);
$response->assertStatus(400);
}
}