test(model): add tests for "player" model

This commit is contained in:
Pig Fang 2017-12-26 20:07:26 +08:00
parent a5615eb090
commit f4c21a0cb2
2 changed files with 51 additions and 8 deletions

View File

@ -241,12 +241,12 @@ class Player extends Model
// if listeners return nothing
if (isset($responses[0]) && $responses[0] !== null) {
return $responses[0];
return $responses[0]; // @codeCoverageIgnore
} else {
return $this->generateJsonProfile($api_type);
}
} else {
throw new InvalidArgumentException('The given api type should be Player::CSL_API or Player::USM_API.');
throw new \InvalidArgumentException('The given api type should be Player::CSL_API or Player::USM_API.');
}
}
@ -288,21 +288,17 @@ class Player extends Model
{
// @see http://stackoverflow.com/questions/2215354/php-date-format-when-inserting-into-datetime-in-mysql
$this->update(['last_modified' => Utils::getTimeFormatted()]);
return Event::fire(new PlayerProfileUpdated($this));
return event(new PlayerProfileUpdated($this));
}
/**
* Get time of last modified.
*
* @return timestamp
* @return int|false
*/
public function getLastModified()
{
return strtotime($this['last_modified']);
}
public function scopeLike($query, $field, $value)
{
return $query->where($field, 'LIKE', "%$value%");
}
}

View File

@ -0,0 +1,47 @@
<?php
use App\Models\Player;
use App\Models\Texture;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class PlayerTest extends TestCase
{
use DatabaseTransactions;
public function testGetTexture()
{
$steve = factory(Texture::class)->create();
$alex = factory(Texture::class, 'alex')->create();
$player = factory(Player::class)->create([
'tid_steve' => $steve->tid,
'tid_alex' => $alex->tid
]);
$player = Player::find($player->pid);
$this->assertEquals($steve->hash, $player->getTexture('skin'));
$player->setPreference('slim');
$player = Player::find($player->pid);
$this->assertEquals($alex->hash, $player->getTexture('skin'));
$this->assertFalse($player->getTexture('invalid_type'));
}
/**
* @expectedException InvalidArgumentException
*/
public function testGetJsonProfile()
{
$player = factory(Player::class)->make();
$this->assertNull($player->getJsonProfile(-1));
}
public function testUpdateLastModified()
{
$player = factory(Player::class)->make();
$this->expectsEvents(\App\Events\PlayerProfileUpdated::class);
$player->updateLastModified();
}
}