-
Notifications
You must be signed in to change notification settings - Fork 24
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
Showing
8 changed files
with
311 additions
and
6 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
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, | ||
]; | ||
} | ||
} |
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,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
77
tests/Feature/Game/Survey/Controllers/Api/SurveyControllerTest.php
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,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); | ||
} | ||
|
||
} |
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,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); | ||
} | ||
|
||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace Tests\Traits; | ||
|
||
use App\Flare\Models\UserLoginDuration; | ||
|
||
trait CreateUserLoginDuration | ||
{ | ||
public function createUserLoginDuration(array $options = []): UserLoginDuration | ||
{ | ||
return UserLoginDuration::factory()->create($options); | ||
} | ||
} |