fix tests of PlayerController

This commit is contained in:
Pig Fang 2018-07-13 16:14:49 +08:00
parent 4c44fa47cd
commit d1442902b2

View File

@ -20,7 +20,7 @@ class PlayerControllerTest extends TestCase
public function testIndex() public function testIndex()
{ {
$this->visit('/user/player?pid=5') $this->get('/user/player?pid=5')
->assertViewHas('players') ->assertViewHas('players')
->assertViewHas('user'); ->assertViewHas('user');
} }
@ -28,19 +28,19 @@ class PlayerControllerTest extends TestCase
public function testAdd() public function testAdd()
{ {
// Without player name // Without player name
$this->post('/user/player/add', [], ['X-Requested-With' => 'XMLHttpRequest']) $this->postJson('/user/player/add', [], ['X-Requested-With' => 'XMLHttpRequest'])
->seeJson([ ->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'Player Name']) 'msg' => trans('validation.required', ['attribute' => 'Player Name'])
]); ]);
// Only A-Za-z0-9_ are allowed // Only A-Za-z0-9_ are allowed
option(['player_name_rule' => 'official']); option(['player_name_rule' => 'official']);
$this->post( $this->postJson(
'/user/player/add', '/user/player/add',
['player_name' => '角色名'], ['player_name' => '角色名'],
['X-Requested-With' => 'XMLHttpRequest'] ['X-Requested-With' => 'XMLHttpRequest']
)->seeJson([ )->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.player_name', ['attribute' => 'Player Name']) 'msg' => trans('validation.player_name', ['attribute' => 'Player Name'])
]); ]);
@ -48,11 +48,11 @@ class PlayerControllerTest extends TestCase
// Custom player name rule (regexp) // Custom player name rule (regexp)
option(['player_name_rule' => 'custom']); option(['player_name_rule' => 'custom']);
option(['custom_player_name_regexp' => '/^([0-9]+)$/']); option(['custom_player_name_regexp' => '/^([0-9]+)$/']);
$this->post( $this->postJson(
'/user/player/add', '/user/player/add',
['player_name' => 'yjsnpi'], ['player_name' => 'yjsnpi'],
['X-Requested-With' => 'XMLHttpRequest'] ['X-Requested-With' => 'XMLHttpRequest']
)->seeJson([ )->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.player_name', ['attribute' => 'Player Name']) 'msg' => trans('validation.player_name', ['attribute' => 'Player Name'])
]); ]);
@ -60,11 +60,11 @@ class PlayerControllerTest extends TestCase
// Lack of score // Lack of score
option(['player_name_rule' => 'official']); option(['player_name_rule' => 'official']);
$user = factory(User::class)->create(['score' => 0]); $user = factory(User::class)->create(['score' => 0]);
$this->actAs($user)->post( $this->actAs($user)->postJson(
'/user/player/add', '/user/player/add',
['player_name' => 'no_score'], ['player_name' => 'no_score'],
['X-Requested-With' => 'XMLHttpRequest'] ['X-Requested-With' => 'XMLHttpRequest']
)->seeJson([ )->assertJson([
'errno' => 7, 'errno' => 7,
'msg' => trans('user.player.add.lack-score') 'msg' => trans('user.player.add.lack-score')
]); ]);
@ -74,9 +74,9 @@ class PlayerControllerTest extends TestCase
option(['player_name_rule' => 'cjk']); option(['player_name_rule' => 'cjk']);
$user = factory(User::class)->create(); $user = factory(User::class)->create();
$score = $user->score; $score = $user->score;
$this->actAs($user)->post('/user/player/add', [ $this->actAs($user)->postJson('/user/player/add', [
'player_name' => '角色名' 'player_name' => '角色名'
])->seeJson([ ])->assertJson([
'errno' => 0, 'errno' => 0,
'msg' => trans('user.player.add.success', ['name' => '角色名']) 'msg' => trans('user.player.add.success', ['name' => '角色名'])
]); ]);
@ -93,8 +93,8 @@ class PlayerControllerTest extends TestCase
); );
// Add a existed player // Add a existed player
$this->post('/user/player/add', ['player_name' => '角色名']) $this->postJson('/user/player/add', ['player_name' => '角色名'])
->seeJson([ ->assertJson([
'errno' => 6, 'errno' => 6,
'msg' => trans('user.player.add.repeated') 'msg' => trans('user.player.add.repeated')
]); ]);
@ -106,8 +106,8 @@ class PlayerControllerTest extends TestCase
$user = User::find($player->uid); $user = User::find($player->uid);
$this->expectsEvents(Events\PlayerWillBeDeleted::class); $this->expectsEvents(Events\PlayerWillBeDeleted::class);
$this->actAs($user) $this->actAs($user)
->post('/user/player/delete', ['pid' => $player->pid]) ->postJson('/user/player/delete', ['pid' => $player->pid])
->seeJson([ ->assertJson([
'errno' => 0, 'errno' => 0,
'msg' => trans('user.player.delete.success', ['name' => $player->player_name]) 'msg' => trans('user.player.delete.success', ['name' => $player->player_name])
]); ]);
@ -123,8 +123,8 @@ class PlayerControllerTest extends TestCase
$player = factory(Player::class)->create(); $player = factory(Player::class)->create();
$user = User::find($player->uid); $user = User::find($player->uid);
$this->actAs($user) $this->actAs($user)
->post('/user/player/delete', ['pid' => $player->pid]) ->postJson('/user/player/delete', ['pid' => $player->pid])
->seeJson([ ->assertJson([
'errno' => 0, 'errno' => 0,
'msg' => trans('user.player.delete.success', ['name' => $player->player_name]) 'msg' => trans('user.player.delete.success', ['name' => $player->player_name])
]); ]);
@ -138,7 +138,7 @@ class PlayerControllerTest extends TestCase
{ {
$player = factory(Player::class)->create(['last_modified' => '2017-11-11 22:51:00']); $player = factory(Player::class)->create(['last_modified' => '2017-11-11 22:51:00']);
$this->get('/user/player/show?pid='.$player->pid) $this->get('/user/player/show?pid='.$player->pid)
->seeJson($player->toArray()); ->assertJson($player->toArray());
} }
public function testRename() public function testRename()
@ -148,55 +148,55 @@ class PlayerControllerTest extends TestCase
// Without new player name // Without new player name
$this->actAs($user) $this->actAs($user)
->post('/user/player/rename', [ ->postJson('/user/player/rename', [
'pid' => $player->pid, 'pid' => $player->pid,
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'Player Name']) 'msg' => trans('validation.required', ['attribute' => 'Player Name'])
]); ]);
// Only A-Za-z0-9_ are allowed // Only A-Za-z0-9_ are allowed
option(['player_name_rule' => 'official']); option(['player_name_rule' => 'official']);
$this->post('/user/player/rename',[ $this->postJson('/user/player/rename',[
'pid' => $player->pid, 'pid' => $player->pid,
'new_player_name' => '角色名' 'new_player_name' => '角色名'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.player_name', ['attribute' => 'Player Name']) 'msg' => trans('validation.player_name', ['attribute' => 'Player Name'])
]); ]);
// Other invalid characters // Other invalid characters
option(['player_name_rule' => 'cjk']); option(['player_name_rule' => 'cjk']);
$this->post('/user/player/rename', [ $this->postJson('/user/player/rename', [
'pid' => $player->pid, 'pid' => $player->pid,
'new_player_name' => '\\' 'new_player_name' => '\\'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.player_name', ['attribute' => 'Player Name']) 'msg' => trans('validation.player_name', ['attribute' => 'Player Name'])
]); ]);
// Use a duplicated player name // Use a duplicated player name
$name = factory(Player::class)->create()->player_name; $name = factory(Player::class)->create()->player_name;
$this->post('/user/player/rename', [ $this->postJson('/user/player/rename', [
'pid' => $player->pid, 'pid' => $player->pid,
'new_player_name' => $name 'new_player_name' => $name
])->seeJson([ ])->assertJson([
'errno' => 6, 'errno' => 6,
'msg' => trans('user.player.rename.repeated') 'msg' => trans('user.player.rename.repeated')
]); ]);
// Success // Success
$this->expectsEvents(Events\PlayerProfileUpdated::class); $this->expectsEvents(Events\PlayerProfileUpdated::class);
$this->post('/user/player/rename', [ $this->postJson('/user/player/rename', [
'pid' => $player->pid, 'pid' => $player->pid,
'new_player_name' => 'new_name' 'new_player_name' => 'new_name'
])->seeJson([ ])->assertJson([
'errno' => 0, 'errno' => 0,
'msg' => trans( 'msg' => trans(
'user.player.rename.success', 'user.player.rename.success',
@ -215,19 +215,19 @@ class PlayerControllerTest extends TestCase
// Set a not-existed texture // Set a not-existed texture
$this->actAs($user) $this->actAs($user)
->post('/user/player/set', [ ->postJson('/user/player/set', [
'pid' => $player->pid, 'pid' => $player->pid,
'tid' => ['steve' => -1] 'tid' => ['steve' => -1]
])->seeJson([ ])->assertJson([
'errno' => 6, 'errno' => 6,
'msg' => trans('skinlib.un-existent') 'msg' => trans('skinlib.un-existent')
]); ]);
// Set for "steve" type // Set for "steve" type
$this->post('/user/player/set', [ $this->postJson('/user/player/set', [
'pid' => $player->pid, 'pid' => $player->pid,
'tid' => ['steve' => $steve->tid] 'tid' => ['steve' => $steve->tid]
])->seeJson([ ])->assertJson([
'errno' => 0, 'errno' => 0,
'msg' => trans('user.player.set.success', ['name' => $player->player_name]) 'msg' => trans('user.player.set.success', ['name' => $player->player_name])
]); ]);
@ -235,43 +235,43 @@ class PlayerControllerTest extends TestCase
$this->assertEquals($steve->tid, Player::find($player->pid)->tid_steve); $this->assertEquals($steve->tid, Player::find($player->pid)->tid_steve);
// Set for "alex" type // Set for "alex" type
$this->post('/user/player/set', [ $this->postJson('/user/player/set', [
'pid' => $player->pid, 'pid' => $player->pid,
'tid' => ['alex' => $alex->tid] 'tid' => ['alex' => $alex->tid]
])->seeJson([ ])->assertJson([
'errno' => 0, 'errno' => 0,
'msg' => trans('user.player.set.success', ['name' => $player->player_name]) 'msg' => trans('user.player.set.success', ['name' => $player->player_name])
]); ]);
$this->assertEquals($alex->tid, Player::find($player->pid)->tid_alex); $this->assertEquals($alex->tid, Player::find($player->pid)->tid_alex);
// Set for "cape" type // Set for "cape" type
$this->post('/user/player/set', [ $this->postJson('/user/player/set', [
'pid' => $player->pid, 'pid' => $player->pid,
'tid' => ['cape' => $cape->tid] 'tid' => ['cape' => $cape->tid]
])->seeJson([ ])->assertJson([
'errno' => 0, 'errno' => 0,
'msg' => trans('user.player.set.success', ['name' => $player->player_name]) 'msg' => trans('user.player.set.success', ['name' => $player->player_name])
]); ]);
$this->assertEquals($cape->tid, Player::find($player->pid)->tid_cape); $this->assertEquals($cape->tid, Player::find($player->pid)->tid_cape);
// Invalid texture type is acceptable // Invalid texture type is acceptable
$this->post('/user/player/set', [ $this->postJson('/user/player/set', [
'pid' => $player->pid, 'pid' => $player->pid,
'tid' => ['nope' => $steve->tid] // TID must be valid 'tid' => ['nope' => $steve->tid] // TID must be valid
])->seeJson([ ])->assertJson([
'errno' => 0, 'errno' => 0,
'msg' => trans('user.player.set.success', ['name' => $player->player_name]) 'msg' => trans('user.player.set.success', ['name' => $player->player_name])
]); ]);
// Should be OK if texture type does not match // Should be OK if texture type does not match
$this->post('/user/player/set', [ $this->postJson('/user/player/set', [
'pid' => $player->pid, 'pid' => $player->pid,
'tid' => [ 'tid' => [
'steve' => $alex->tid, 'steve' => $alex->tid,
'alex' => $cape->tid, 'alex' => $cape->tid,
'cape' => $steve->tid 'cape' => $steve->tid
] ]
])->seeJson([ ])->assertJson([
'errno' => 0, 'errno' => 0,
'msg' => trans('user.player.set.success', ['name' => $player->player_name]) 'msg' => trans('user.player.set.success', ['name' => $player->player_name])
]); ]);
@ -295,13 +295,13 @@ class PlayerControllerTest extends TestCase
$this->expectsEvents(Events\PlayerProfileUpdated::class); $this->expectsEvents(Events\PlayerProfileUpdated::class);
$this->actAs($user) $this->actAs($user)
->post('/user/player/texture/clear', [ ->postJson('/user/player/texture/clear', [
'pid' => $player->pid, 'pid' => $player->pid,
'steve' => 1, // "1" stands for "true" 'steve' => 1, // "1" stands for "true"
'alex' => 1, 'alex' => 1,
'cape' => 1, 'cape' => 1,
'nope' => 1, // Invalid texture type is acceptable 'nope' => 1, // Invalid texture type is acceptable
])->seeJson([ ])->assertJson([
'errno' => 0, 'errno' => 0,
'msg' => trans('user.player.clear.success', ['name' => $player->player_name]) 'msg' => trans('user.player.clear.success', ['name' => $player->player_name])
]); ]);
@ -315,34 +315,34 @@ class PlayerControllerTest extends TestCase
// Without `preference` field // Without `preference` field
$player = factory(Player::class)->create(); $player = factory(Player::class)->create();
$this->actAs(User::find($player->uid)) $this->actAs(User::find($player->uid))
->post('/user/player/preference', [ ->postJson('/user/player/preference', [
'pid' => $player->pid 'pid' => $player->pid
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'preference']) 'msg' => trans('validation.required', ['attribute' => 'preference'])
]); ]);
// value of `preference` is invalid // value of `preference` is invalid
$this->post('/user/player/preference', [ $this->postJson('/user/player/preference', [
'pid' => $player->pid, 'pid' => $player->pid,
'preference' => 'steve' 'preference' => 'steve'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 1, 'errno' => 1,
'msg' => trans('validation.preference', ['attribute' => 'preference']) 'msg' => trans('validation.preference', ['attribute' => 'preference'])
]); ]);
// Success // Success
$this->expectsEvents(Events\PlayerProfileUpdated::class); $this->expectsEvents(Events\PlayerProfileUpdated::class);
$this->post('/user/player/preference', [ $this->postJson('/user/player/preference', [
'pid' => $player->pid, 'pid' => $player->pid,
'preference' => 'slim' 'preference' => 'slim'
], [ ], [
'X-Requested-With' => 'XMLHttpRequest' 'X-Requested-With' => 'XMLHttpRequest'
])->seeJson([ ])->assertJson([
'errno' => 0, 'errno' => 0,
'msg' => trans( 'msg' => trans(
'user.player.preference.success', 'user.player.preference.success',