Add User unit tests

This commit is contained in:
Phan An 2017-08-05 18:44:38 +01:00
parent 8bb547cb4d
commit 1560cf99d0
2 changed files with 88 additions and 0 deletions

View file

@ -2,6 +2,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
@ -48,11 +49,21 @@ class User extends Authenticatable
*/
protected $hidden = ['password', 'remember_token', 'created_at', 'updated_at'];
/**
* A user can have many playlists.
*
* @return HasMany
*/
public function playlists()
{
return $this->hasMany(Playlist::class);
}
/**
* A user can make multiple interactions.
*
* @return HasMany
*/
public function interactions()
{
return $this->hasMany(Interaction::class);

77
tests/Unit/UserTest.php Normal file
View file

@ -0,0 +1,77 @@
<?php
namespace Tests\Unit;
use App\Models\User;
use Tests\TestCase;
class UserTest extends TestCase
{
/** @test */
public function it_can_be_instantiated()
{
$this->assertInstanceOf(User::class, new User());
}
/** @test */
public function user_preferences_can_be_set()
{
// Given a user
/** @var User $user */
$user = factory(User::class)->create();
// When I see the user's preference
$user->setPreference('foo', 'bar');
// Then I see the preference properly set
$this->assertArraySubset(['foo' => 'bar'], $user->preferences);
}
/** @test */
public function user_preferences_can_be_retrieved()
{
// Given a user with some preferences
/** @var User $user */
$user = factory(User::class)->create([
'preferences' => ['foo' => 'bar'],
]);
// When I get a preference by its key
$value = $user->getPreference('foo');
// Then I retrieve the preference value
$this->assertEquals('bar', $value);
}
/** @test */
public function user_preferences_can_be_deleted()
{
// Given a user with some preferences
/** @var User $user */
$user = factory(User::class)->create([
'preferences' => ['foo' => 'bar'],
]);
// When I delete the preference by its key
$user->deletePreference('foo');
// Then I see the preference gets deleted
$this->assertArrayNotHasKey('foo', $user->preferences);
}
/** @test */
public function sensitive_preferences_are_hidden()
{
// Given a user with sensitive preferences
/** @var User $user */
$user = factory(User::class)->create([
'preferences' => ['lastfm_session_key' => 'foo'],
]);
// When I try to access the sensitive preferences
$value = $user->preferences['lastfm_session_key'];
// Then the sensitive preferences are replaced with "hidden"
$this->assertEquals('hidden', $value);
}
}