|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Events; |
| 4 | + |
| 5 | +use App\Enums\GameLocations; |
| 6 | +use App\Events\DiplomacyUpdated; |
| 7 | +use App\Http\Resources\DiplomacyResource; |
| 8 | +use App\Models\CityRelation; |
| 9 | +use App\Models\Diplomacy; |
| 10 | +use App\Services\DiplomacyService; |
| 11 | +use Illuminate\Support\Facades\Event; |
| 12 | +use Tests\TestCase; |
| 13 | + |
| 14 | +class DiplomacyUpdatedTest extends TestCase |
| 15 | +{ |
| 16 | + public function test_set_new_diplomacy_broadcasts_updated_diplomacy(): void |
| 17 | + { |
| 18 | + Event::fake([DiplomacyUpdated::class]); |
| 19 | + $this->actingAs($this->TestUser); |
| 20 | + |
| 21 | + $location = GameLocations::HIRTAM_LOCATION->value; |
| 22 | + |
| 23 | + $CityRelation = CityRelation::where('city', $location)->first(); |
| 24 | + if (! $CityRelation instanceof CityRelation) { |
| 25 | + $CityRelation = new CityRelation(); |
| 26 | + $CityRelation->city = $location; |
| 27 | + } |
| 28 | + |
| 29 | + $CityRelation->hirtam = 1; |
| 30 | + $CityRelation->pvitul = 1.1; |
| 31 | + $CityRelation->khanz = 0.9; |
| 32 | + $CityRelation->ter = 1; |
| 33 | + $CityRelation->fansal_plains = 1.1; |
| 34 | + $CityRelation->save(); |
| 35 | + |
| 36 | + $Diplomacy = Diplomacy::where('user_id', $this->TestUser->id)->first(); |
| 37 | + if (! $Diplomacy instanceof Diplomacy) { |
| 38 | + $Diplomacy = Diplomacy::factory()->create([ |
| 39 | + 'user_id' => $this->TestUser->id, |
| 40 | + 'username' => $this->TestUser->username, |
| 41 | + ]); |
| 42 | + } |
| 43 | + |
| 44 | + $Diplomacy->hirtam = 1; |
| 45 | + $Diplomacy->pvitul = 1; |
| 46 | + $Diplomacy->khanz = 1; |
| 47 | + $Diplomacy->ter = 1; |
| 48 | + $Diplomacy->fansal_plains = 1; |
| 49 | + $Diplomacy->save(); |
| 50 | + |
| 51 | + $service = app(DiplomacyService::class); |
| 52 | + $service->setNewDiplomacy($location, 10, $this->TestUser->id); |
| 53 | + |
| 54 | + $updatedDiplomacy = Diplomacy::where('user_id', $this->TestUser->id)->firstOrFail(); |
| 55 | + $expectedPayload = (new DiplomacyResource($updatedDiplomacy))->resolve(); |
| 56 | + |
| 57 | + Event::assertDispatched(DiplomacyUpdated::class, function (DiplomacyUpdated $event) use ($expectedPayload) { |
| 58 | + return $event->userId === $this->TestUser->id |
| 59 | + && $event->Diplomacy === $expectedPayload; |
| 60 | + }); |
| 61 | + } |
| 62 | +} |
0 commit comments