Use formal event assertion

This commit is contained in:
Pig Fang 2019-05-01 10:38:50 +08:00
parent 777e786873
commit 4348cd7020
6 changed files with 38 additions and 17 deletions

View File

@ -30,10 +30,12 @@ class CheckPlayerExist
$responses = event(new CheckPlayerExists($player_name));
foreach ($responses as $r) {
if (is_array($responses)) {
// @codeCoverageIgnoreStart
if ($r) {
return $next($request);
foreach ($responses as $r) {
if ($r) {
return $next($request);
}
}
// @codeCoverageIgnoreEnd
}

View File

@ -3,6 +3,7 @@
namespace Tests;
use Cache;
use Event;
use App\Events;
use App\Models\User;
use App\Models\Player;
@ -39,8 +40,7 @@ class AuthControllerTest extends TestCase
public function testHandleLogin()
{
$this->expectsEvents(Events\UserTryToLogin::class);
$this->expectsEvents(Events\UserLoggedIn::class);
Event::fake();
$user = factory(User::class)->create();
$user->changePassword('12345678');
@ -124,6 +124,9 @@ class AuthControllerTest extends TestCase
);
$this->assertFalse(Cache::has($loginFailsCacheKey));
Event::assertDispatched(Events\UserTryToLogin::class);
Event::assertDispatched(Events\UserLoggedIn::class);
Cache::flush();
$this->flushSession();
@ -170,7 +173,7 @@ class AuthControllerTest extends TestCase
public function testHandleRegister()
{
$this->expectsEvents(Events\UserRegistered::class);
Event::fake();
// Should return a warning if `email` is empty
$this->postJson('/auth/register')->assertJsonValidationErrors('email');
@ -365,6 +368,7 @@ class AuthControllerTest extends TestCase
'permission' => User::NORMAL,
]);
$this->assertAuthenticated();
Event::assertDispatched(Events\UserRegistered::class);
// Require player name
option(['register_with_player_name' => true]);

View File

@ -91,6 +91,8 @@ class MiddlewareTest extends TestCase
public function testCheckPlayerExist()
{
Event::fake();
$this->getJson('/nope.json')
->assertStatus(404)
->assertSee(trans('general.unexistent-player'));
@ -106,8 +108,8 @@ class MiddlewareTest extends TestCase
$this->getJson("/{$player->name}.json")
->assertJson(['username' => $player->name]); // Default is CSL API
$this->expectsEvents(\App\Events\CheckPlayerExists::class);
$this->getJson("/{$player->name}.json");
Event::assertDispatched(\App\Events\CheckPlayerExists::class);
$player = factory(\App\Models\Player::class)->create();
$user = $player->user;

View File

@ -40,6 +40,8 @@ class PlayerControllerTest extends TestCase
public function testAdd()
{
Event::fake();
// Without player name
$this->postJson('/user/player/add')->assertJsonValidationErrors('name');
@ -68,7 +70,7 @@ class PlayerControllerTest extends TestCase
'code' => 7,
'message' => trans('user.player.add.lack-score'),
]);
$this->expectsEvents(Events\CheckPlayerExists::class);
Event::assertDispatched(Events\CheckPlayerExists::class);
// Allowed to use CJK characters
option(['player_name_rule' => 'cjk']);
@ -80,8 +82,8 @@ class PlayerControllerTest extends TestCase
'code' => 0,
'message' => trans('user.player.add.success', ['name' => '角色名']),
]);
$this->expectsEvents(Events\PlayerWillBeAdded::class);
$this->expectsEvents(Events\PlayerWasAdded::class);
Event::assertDispatched(Events\PlayerWillBeAdded::class);
Event::assertDispatched(Events\PlayerWasAdded::class);
$player = Player::where('name', '角色名')->first();
$this->assertNotNull($player);
$this->assertEquals($user->uid, $player->uid);
@ -109,10 +111,11 @@ class PlayerControllerTest extends TestCase
public function testDelete()
{
Event::fake();
$user = factory(User::class)->create();
$player = factory(Player::class)->create(['uid' => $user->uid]);
$score = $user->score;
$this->expectsEvents(Events\PlayerWillBeDeleted::class);
$this->actingAs($user)
->postJson('/user/player/delete/'.$player->pid)
->assertJson([
@ -120,7 +123,8 @@ class PlayerControllerTest extends TestCase
'message' => trans('user.player.delete.success', ['name' => $player->name]),
]);
$this->assertNull(Player::find($player->pid));
$this->expectsEvents(Events\PlayerWasDeleted::class);
Event::assertDispatched(Events\PlayerWillBeDeleted::class);
Event::assertDispatched(Events\PlayerWasDeleted::class);
$this->assertEquals(
$score + option('score_per_player'),
User::find($user->uid)->score

View File

@ -2,6 +2,7 @@
namespace Tests;
use Event;
use Mockery;
use Exception;
use App\Models\User;
@ -185,6 +186,8 @@ class TextureControllerTest extends TestCase
public function testAvatar()
{
Event::fake();
Storage::fake('textures');
$base64_email = base64_encode('a@b.c');
$this->get("/avatar/$base64_email.png")
@ -201,9 +204,9 @@ class TextureControllerTest extends TestCase
->once()
->andReturn(imagecreatefromstring($png));
$this->expectsEvents(\App\Events\GetAvatarPreview::class);
$this->get('/avatar/'.base64_encode($user->email).'.png')
->assertHeader('Content-Type', 'image/png');
Event::assertDispatched(\App\Events\GetAvatarPreview::class);
Storage::shouldReceive('disk')->with('textures')->andThrow(new Exception);
$this->get('/avatar/'.base64_encode($user->email).'.png')
@ -212,7 +215,9 @@ class TextureControllerTest extends TestCase
public function testAvatarWithSize()
{
Event::fake();
Storage::fake('textures');
$steve = factory(Texture::class)->create();
$png = base64_decode(\App\Http\Controllers\TextureController::getDefaultSteveSkin());
Storage::disk('textures')->put($steve->hash, $png);
@ -224,14 +229,16 @@ class TextureControllerTest extends TestCase
->once()
->andReturn(imagecreatefromstring($png));
$this->expectsEvents(\App\Events\GetAvatarPreview::class);
$this->get('/avatar/50/'.base64_encode($user->email).'.png')
->assertHeader('Content-Type', 'image/png');
Event::fake(\App\Events\GetAvatarPreview::class);
}
public function testPreview()
{
Event::fake();
Storage::fake('textures');
$steve = factory(Texture::class)->create();
$cape = factory(Texture::class, 'cape')->create();
@ -249,9 +256,9 @@ class TextureControllerTest extends TestCase
$mock->shouldReceive('generatePreviewFromSkin')
->once()
->andReturn(imagecreatefromstring($png));
$this->expectsEvents(\App\Events\GetSkinPreview::class);
$this->get("/preview/{$steve->tid}.png")
->assertHeader('Content-Type', 'image/png');
Event::fake(\App\Events\GetSkinPreview::class);
$mock->shouldReceive('generatePreviewFromCape')
->once()

View File

@ -2,6 +2,7 @@
namespace Tests;
use Event;
use Parsedown;
use App\Events;
use App\Models\User;
@ -219,6 +220,7 @@ class UserControllerTest extends TestCase
public function testHandleProfile()
{
Event::fake();
$user = factory(User::class)->create();
$user->changePassword('12345678');
@ -254,7 +256,6 @@ class UserControllerTest extends TestCase
option(['single_player' => false]);
// Change nickname successfully
$this->expectsEvents(Events\UserProfileUpdated::class);
$this->postJson('/user/profile', [
'action' => 'nickname',
'new_nickname' => 'nickname',
@ -263,6 +264,7 @@ class UserControllerTest extends TestCase
'message' => trans('user.profile.nickname.success', ['nickname' => 'nickname']),
]);
$this->assertEquals('nickname', User::find($user->uid)->nickname);
Event::assertDispatched(Events\UserProfileUpdated::class);
// Change password without `current_password` field
$this->postJson('/user/profile', ['action' => 'password'])
@ -307,7 +309,6 @@ class UserControllerTest extends TestCase
]);
// Change password successfully
$this->expectsEvents(Events\EncryptUserPassword::class);
$this->postJson('/user/profile', [
'action' => 'password',
'current_password' => '12345678',
@ -316,6 +317,7 @@ class UserControllerTest extends TestCase
'code' => 0,
'message' => trans('user.profile.password.success'),
]);
Event::assertDispatched(Events\EncryptUserPassword::class);
$this->assertTrue(User::find($user->uid)->verifyPassword('87654321'));
// After changed password, user should re-login.
$this->assertGuest();