Skip to content

Commit

Permalink
More survey tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKyle committed Aug 27, 2024
1 parent eeb41de commit a97e30e
Show file tree
Hide file tree
Showing 8 changed files with 311 additions and 6 deletions.
7 changes: 7 additions & 0 deletions app/Flare/Models/UserLoginDuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Flare\Models;

use Database\Factories\UserLoginDurationFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -40,4 +41,10 @@ class UserLoginDuration extends Model
public function user(): BelongsTo {
return $this->belongsTo(User::class);
}

protected static function newFactory()
{
return UserLoginDurationFactory::new();
}

}
10 changes: 5 additions & 5 deletions database/factories/SubmittedSurveyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Database\Factories;

use App\Flare\Models\Survey;
use App\Flare\Models\SubmittedSurvey;
use Illuminate\Database\Eloquent\Factories\Factory;

class SubmittedSurveyFactory extends Factory
Expand All @@ -12,7 +12,7 @@ class SubmittedSurveyFactory extends Factory
*
* @var string
*/
protected $model = Survey::class;
protected $model = SubmittedSurvey::class;

/**
* Define the model's default state.
Expand All @@ -27,9 +27,9 @@ public function definition()
'survey_id' => null,
'survey_response' => [
[
'Some Radio label' => ['value' => 'Option 1', 'type' => 'radio'],
'Some Checkbox label' => ['value' => 'Option 1', 'type' => 'checkbox'],
'Some markdown label' => ['value' => 'Some content', 'type' => 'markdown'],
'Some Radio Label' => ['value' => 'Option 1', 'type' => 'radio'],
'Some Checkbox Label' => ['value' => ['Option 1'], 'type' => 'checkbox'],
'Some markdown Label' => ['value' => 'Some content', 'type' => 'markdown'],
]
]
];
Expand Down
33 changes: 33 additions & 0 deletions database/factories/UserLoginDurationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Database\Factories;

use App\Flare\Models\UserLoginDuration;
use Illuminate\Database\Eloquent\Factories\Factory;

class UserLoginDurationFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = UserLoginDuration::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'user_id' => null,
'logged_in_at' => null,
'logged_out_at' => null,
'last_activity' => null,
'duration_in_seconds' => null,
'last_heart_beat' => null,
];
}
}
159 changes: 159 additions & 0 deletions tests/Console/Survey/StartSurveyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

namespace Tests\Console\Survey;

use App\Game\Events\Values\EventType;
use App\Game\Survey\Events\ShowSurvey;
use Artisan;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Tests\Setup\Character\CharacterFactory;
use Tests\TestCase;
use Tests\Traits\CreateEvent;
use Tests\Traits\CreateScheduledEvent;
use Tests\Traits\CreateSubmittedSurvey;
use Tests\Traits\CreateSurvey;
use Tests\Traits\CreateUserLoginDuration;

class StartSurveyTest extends TestCase {
use CreateEvent, RefreshDatabase, CreateScheduledEvent, CreateUserLoginDuration, CreateSubmittedSurvey, CreateSurvey;

private ?CharacterFactory $characterFactory;


public function setUp(): void {
parent::setUp();

$this->characterFactory = (new CharacterFactory())->createBaseCharacter();
}

public function tearDown(): void {
parent::tearDown();

$this->characterFactory = null;
}

public function testCannotStartSurveyWhenNoScheduledEventIsRunning() {

Event::fake();

Artisan::call('start:survey');

Event::assertNotDispatched(ShowSurvey::class);
}

public function testCannotStartSurveyWhenFlagIsAlreadyFlippedToShowTheSurvey() {
Event::fake();

$character = $this->characterFactory->getCharacter();

$this->createScheduledEvent([
'event_type' => EventType::FEEDBACK_EVENT,
'currently_running' => true,
]);

$character->user()->update(['is_showing_survey' => true]);

Artisan::call('start:survey');

Event::assertNotDispatched(ShowSurvey::class);
}

public function testCannotStartSurveyWhenThereIsNoLoggedInDurationRecord() {
Event::fake();


$this->createScheduledEvent([
'event_type' => EventType::FEEDBACK_EVENT,
'currently_running' => true,
]);

Artisan::call('start:survey');

Event::assertNotDispatched(ShowSurvey::class);
}

public function testCannotStartSurveyWhenUserLoginDurationIsBelowOneHour() {
Event::fake();


$character = $this->characterFactory->getCharacter();


$this->createScheduledEvent([
'event_type' => EventType::FEEDBACK_EVENT,
'currently_running' => true,
]);

$this->createUserLoginDuration([
'user_id' => $character->user_id,
'logged_in_at' => now(),
'logged_out_at' => now(),
'last_activity' => now(),
'duration_in_seconds' => 0,
'last_heart_beat' => now(),
]);

Artisan::call('start:survey');

Event::assertNotDispatched(ShowSurvey::class);
}

public function testCannotStartSurveyWhenYouSubmittedASurvey() {
Event::fake();


$character = $this->characterFactory->getCharacter();


$this->createScheduledEvent([
'event_type' => EventType::FEEDBACK_EVENT,
'currently_running' => true,
]);

$this->createUserLoginDuration([
'user_id' => $character->user_id,
'logged_in_at' => now(),
'logged_out_at' => now()->addHours(3),
'last_activity' => now()->addHours(3),
'duration_in_seconds' => now()->addHours(3)->diffInSeconds(now()),
'last_heart_beat' => now()->addHours(3),
]);

$this->createSubmittedSurvey([
'character_id' => $character->id,
'survey_id' => 1000,
]);

Artisan::call('start:survey');

Event::assertNotDispatched(ShowSurvey::class);
}

public function testCanStartSurvey() {

$character = $this->characterFactory->getCharacter();

$this->createSurvey();

$this->createScheduledEvent([
'event_type' => EventType::FEEDBACK_EVENT,
'currently_running' => true,
]);

$this->createUserLoginDuration([
'user_id' => $character->user_id,
'logged_in_at' => now(),
'logged_out_at' => now()->addHours(3),
'last_activity' => now()->addHours(3),
'duration_in_seconds' => now()->addHours(3)->diffInSeconds(now()),
'last_heart_beat' => now()->addHours(3),
]);

Artisan::call('start:survey');

$character = $character->refresh();

$this->assertTrue($character->user->is_showing_survey);
}
}
77 changes: 77 additions & 0 deletions tests/Feature/Game/Survey/Controllers/Api/SurveyControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Tests\Feature\Game\Survey\Controllers\Api;

use App\Flare\Models\Character;
use App\Flare\Models\Survey;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Setup\Character\CharacterFactory;
use Tests\TestCase;
use Tests\Traits\CreateItem;
use Tests\Traits\CreateSurvey;

class SurveyControllerTest extends TestCase
{
use CreateSurvey, CreateItem, RefreshDatabase;

private ?Character $character = null;

private ?Survey $survey = null;

public function setUp(): void
{
parent::setUp();

$this->survey = $this->createSurvey();

$this->character = (new CharacterFactory)->createBaseCharacter()->getCharacter();
}

public function tearDown(): void
{
parent::tearDown();

$this->character = null;
$this->survey = null;
}

public function testFetchSurvey() {
$response = $this->actingAs($this->character->user)
->call('GET', '/api/survey/'.$this->survey->id,);

$jsonData = json_decode($response->getContent(), true);

$this->assertEquals($jsonData['title'], $this->survey->title);
$this->assertEquals($jsonData['description'], $this->survey->description);
$this->assertEquals($jsonData['sections'], $this->survey->sections);
}

public function testSubmitSurveyResponse() {

$this->createItem();

$response = $this->actingAs($this->character->user)
->call('POST', '/api/survey/submit/'.$this->survey->id . '/' . $this->character->id, [
[
'Some Radio Label' => ['value' => 'Option 1', 'type' => 'radio'],
'Some Checkbox Label' => ['value' => ['Option 1'], 'type' => 'checkbox'],
'Some markdown Label' => ['value' => 'Some content', 'type' => 'markdown'],
]
]);

$jsonData = json_decode($response->getContent(), true);

$this->assertEquals($jsonData['message'], 'Survey submitted. Thank you and enjoy your new mythical item! You can find it in your character inventory.
Click character sheet and either select inventory management for mobile or see inventory bottom right. This item has been
rewarded regardless of your current inventory amount.');

$character = $this->character->refresh();

$inventorySlot = $character->inventory->slots->filter(function ($slot) {
return $slot->item->is_mythic;
})->first();

$this->assertNotNull($inventorySlot);
}

}
16 changes: 16 additions & 0 deletions tests/Traits/CreateSubmittedSurvey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Tests\Traits;

use App\Flare\Models\SubmittedSurvey;
use App\Flare\Models\Survey;

trait CreateSubmittedSurvey
{
public function createSubmittedSurvey(array $options = []): SubmittedSurvey
{

return SubmittedSurvey::factory()->create($options);
}

}
2 changes: 1 addition & 1 deletion tests/Traits/CreateSurvey.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

trait CreateSurvey
{
public function CreateSurvey(array $options = []): Survey
public function createSurvey(array $options = []): Survey
{

return Survey::factory()->create($options);
Expand Down
13 changes: 13 additions & 0 deletions tests/Traits/CreateUserLoginDuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Tests\Traits;

use App\Flare\Models\UserLoginDuration;

trait CreateUserLoginDuration
{
public function createUserLoginDuration(array $options = []): UserLoginDuration
{
return UserLoginDuration::factory()->create($options);
}
}

0 comments on commit a97e30e

Please sign in to comment.