Skip to content

Commit

Permalink
Added more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKyle committed Dec 8, 2024
1 parent 3b15c68 commit a4f99d5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public function generateServerMessage(ServerMessageRequest $request): JsonRespon

if ($request->has('type')) {
try {
$this->assignMessageType->assignType($request->type);
$type = $this->assignMessageType->assignType($request->type);

$this->serverMessage->generateServerMessage($type);

return response()->json();
} catch (Exception $e) {
Expand Down
12 changes: 6 additions & 6 deletions app/Game/Messages/Factories/AssignMessageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use Exception;
use Illuminate\Support\Facades\Log;
use App\Game\Messages\Types\ChatMessageTypes;
use App\Game\Messages\Types\Concerns\BaseMessageType;
use App\Game\Messages\Types\MapMessageTypes;
use InvalidArgumentException;
use ValueError;

class AssignMessageType
{
Expand All @@ -18,18 +20,16 @@ class AssignMessageType
*
* @param string $messageType
* @throws InvalidArgumentException
* @return void
* @return BaseMessageType
*/
public function assignType(string $messageType): void
public function assignType(string $messageType): BaseMessageType
{
$messageTypeClasses = [ChatMessageTypes::class, MapMessageTypes::class];

foreach ($messageTypeClasses as $messageTypeClass) {
try {
$messageTypeClass::from($messageType);

return;
} catch (Exception $e) {
return $messageTypeClass::from($messageType);
} catch (ValueError $e) {
continue;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

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

use App\Game\Messages\Events\ServerMessageEvent;
use App\Game\Messages\Types\ChatMessageTypes;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use App\Game\Messages\Events\ServerMessageEvent;
use App\Game\Messages\Types\MapMessageTypes;
use Tests\Setup\Character\CharacterFactory;
use Tests\TestCase;

Expand Down Expand Up @@ -71,4 +71,42 @@ public function testFailToGenerateServerMessage()

$this->assertEquals('Cannot generate server message for either type or custom message.', $jsonData['message']);
}

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

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

$response = $this->actingAs($character->user)
->call('GET', '/api/server-message', [
'_token' => csrf_token(),
'type' => MapMessageTypes::CANNOT_MOVE_DOWN->getValue()
]);

Event::assertDispatched(ServerMessageEvent::class);

$this->assertEquals(200, $response->status());
}

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

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

$response = $this->actingAs($character->user)
->call('GET', '/api/server-message', [
'_token' => csrf_token(),
'type' => 'skjfhf'
]);

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

Event::assertNotDispatched(ServerMessageEvent::class);

$this->assertEquals(422, $response->status());

$this->assertEquals('Invalid message type was passed when trying to generate server message', $jsonData['message']);
}
}
17 changes: 14 additions & 3 deletions tests/Unit/Game/Messages/Handlers/ServerMessageHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Tests\TestCase;
use Tests\Traits\CreateUser;
use App\Game\Messages\Events\ServerMessageEvent;
use App\Game\Messages\Handlers\ServerMessageHandler;
use App\Game\Messages\Types\CharacterMessageTypes;

use App\Game\Messages\Types\CurrenciesMessageTypes;
use Tests\TestCase;
use Tests\Traits\CreateUser;

class ServerMessageHandlerTest extends TestCase
{
Expand Down Expand Up @@ -52,4 +52,15 @@ public function testSendBasicMessage()

Event::assertDispatched(ServerMessageEvent::class);
}

public function testHandleMessageWithNewValue()
{
$user = $this->createUser();

Event::fake();

$this->serverMessageHandler->handleMessageWithNewValue($user, CurrenciesMessageTypes::GOLD, 200, 500);

Event::assertDispatched(ServerMessageEvent::class);
}
}

0 comments on commit a4f99d5

Please sign in to comment.