Add new events for renaming player

This commit is contained in:
Pig Fang 2019-08-31 12:14:39 +08:00
parent 347399c944
commit 8f350b206f
2 changed files with 20 additions and 2 deletions

View File

@ -17,6 +17,7 @@ use App\Events\PlayerWillBeDeleted;
use Illuminate\Support\Facades\Auth;
use App\Http\Middleware\CheckPlayerExist;
use App\Http\Middleware\CheckPlayerOwner;
use Illuminate\Contracts\Events\Dispatcher;
class PlayerController extends Controller
{
@ -116,13 +117,15 @@ class PlayerController extends Controller
return json(trans('user.player.delete.success', ['name' => $playerName]), 0);
}
public function rename(Request $request, $pid)
public function rename(Request $request, Dispatcher $dispatcher, $pid)
{
$newName = $this->validate($request, [
'name' => 'required|player_name|min:'.option('player_name_length_min').'|max:'.option('player_name_length_max'),
])['name'];
$player = Player::find($pid);
$dispatcher->dispatch('player.renaming', $player, $newName);
if (! Player::where('name', $newName)->get()->isEmpty()) {
return json(trans('user.player.rename.repeated'), 6);
}
@ -137,6 +140,8 @@ class PlayerController extends Controller
$user->save();
}
$dispatcher->dispatch('player.renamed', $player, $oldName);
return json(trans('user.player.rename.success', ['old' => $oldName, 'new' => $newName]), 0, $player->toArray());
}

View File

@ -185,9 +185,12 @@ class PlayerControllerTest extends TestCase
'code' => 6,
'message' => trans('user.player.rename.repeated'),
]);
Event::assertDispatched('player.renaming');
// Success
$this->postJson('/user/player/rename/'.$player->pid, ['name' => 'new_name'])
Event::fake();
$pid = $player->pid;
$this->postJson('/user/player/rename/'.$pid, ['name' => 'new_name'])
->assertJson([
'code' => 0,
'message' => trans(
@ -196,6 +199,16 @@ class PlayerControllerTest extends TestCase
),
]);
Event::assertDispatched(Events\PlayerProfileUpdated::class);
Event::assertDispatched('player.renaming', function ($event, $player, $newName) use ($pid) {
$this->assertEquals($pid, $player->pid);
$this->assertEquals('new_name', $newName);
return true;
});
Event::assertDispatched('player.renamed', function ($event, $player, $oldName) use ($pid) {
$this->assertEquals($pid, $player->pid);
$this->assertNotEquals('new_name', $oldName);
return true;
});
// Single player
option(['single_player' => true]);