first commit
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Repositories\Country;
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
use Vanguard\Country;
|
||||
use Vanguard\Repositories\Country\EloquentCountry;
|
||||
|
||||
class EloquentCountryTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @var EloquentCountry
|
||||
*/
|
||||
protected $repo;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->repo = app(EloquentCountry::class);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function lists()
|
||||
{
|
||||
$countries = Country::factory()->times(8)->create();
|
||||
$countries = $countries->sortBy(function ($country) {
|
||||
return $country->name;
|
||||
})->pluck('name', 'id');
|
||||
|
||||
$this->assertEquals($countries->toArray(), $this->repo->lists()->toArray());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Repositories\Permission;
|
||||
|
||||
use Cache;
|
||||
use Event;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Str;
|
||||
use Tests\TestCase;
|
||||
use Vanguard\Events\Permission\Created;
|
||||
use Vanguard\Permission;
|
||||
use Vanguard\Repositories\Permission\EloquentPermission;
|
||||
|
||||
class EloquentPermissionTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @var EloquentPermission
|
||||
*/
|
||||
protected $repo;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->repo = app(EloquentPermission::class);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function all()
|
||||
{
|
||||
$permissions = Permission::factory()->times(4)->create();
|
||||
|
||||
$this->assertEquals($permissions->toArray(), $this->repo->all()->toArray());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function create_permission()
|
||||
{
|
||||
Event::fake([
|
||||
Created::class,
|
||||
]);
|
||||
|
||||
$data = $this->getPermissionStubData();
|
||||
|
||||
$perm = $this->repo->create($data);
|
||||
|
||||
$this->assertDatabaseHas('permissions', $data + ['id' => $perm->id]);
|
||||
|
||||
Event::assertDispatched(Created::class);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function update_permission()
|
||||
{
|
||||
Event::fake([
|
||||
\Vanguard\Events\Permission\Updated::class,
|
||||
]);
|
||||
|
||||
Cache::put('foo', 'bar');
|
||||
|
||||
$data = $this->getPermissionStubData();
|
||||
|
||||
$perm = Permission::factory()->create();
|
||||
|
||||
$this->repo->update($perm->id, $data);
|
||||
|
||||
$this->assertDatabaseHas('permissions', $data + ['id' => $perm->id])
|
||||
->assertNull(Cache::get('foo'));
|
||||
|
||||
Event::assertDispatched(\Vanguard\Events\Permission\Updated::class);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function delete_permission()
|
||||
{
|
||||
Event::fake([
|
||||
\Vanguard\Events\Permission\Deleted::class,
|
||||
]);
|
||||
|
||||
Cache::put('foo', 'bar');
|
||||
|
||||
$perm = Permission::factory()->create();
|
||||
|
||||
$this->repo->delete($perm->id);
|
||||
|
||||
$this->assertDatabaseMissing('permissions', ['id' => $perm->id])
|
||||
->assertNull(Cache::get('foo'));
|
||||
|
||||
Event::assertDispatched(\Vanguard\Events\Permission\Deleted::class);
|
||||
}
|
||||
|
||||
private function getPermissionStubData(): array
|
||||
{
|
||||
return [
|
||||
'name' => Str::random(5),
|
||||
'display_name' => Str::random(5),
|
||||
'description' => 'foo',
|
||||
'removable' => true,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Repositories\Role;
|
||||
|
||||
use Event;
|
||||
use Facades\Tests\Setup\RoleFactory;
|
||||
use Facades\Tests\Setup\UserFactory;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
use Vanguard\Events\Role\Created;
|
||||
use Vanguard\Repositories\Role\EloquentRole;
|
||||
use Vanguard\Role;
|
||||
|
||||
class EloquentRoleTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @var EloquentRole
|
||||
*/
|
||||
protected $repo;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->repo = app(EloquentRole::class);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function all()
|
||||
{
|
||||
$roles = Role::factory()->times(4)->create();
|
||||
|
||||
$this->assertEquals(
|
||||
$roles->toArray(),
|
||||
$this->repo->all()->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function get_all_with_users_count()
|
||||
{
|
||||
$roleA = RoleFactory::create();
|
||||
$roleB = RoleFactory::create();
|
||||
$roleC = RoleFactory::create();
|
||||
|
||||
UserFactory::role($roleA)->create();
|
||||
UserFactory::role($roleA)->create();
|
||||
UserFactory::role($roleB)->create();
|
||||
|
||||
$roleA->users_count = 2;
|
||||
$roleB->users_count = 1;
|
||||
$roleC->users_count = 0;
|
||||
|
||||
$this->assertEquals(
|
||||
[$roleA->toArray(), $roleB->toArray(), $roleC->toArray()],
|
||||
$this->repo->getAllWithUsersCount()->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function create()
|
||||
{
|
||||
Event::fake([
|
||||
Created::class,
|
||||
]);
|
||||
|
||||
$data = ['name' => 'foo', 'display_name' => 'Foo'];
|
||||
$role = $this->repo->create($data);
|
||||
|
||||
$this->assertDatabaseHas('roles', $data + ['id' => $role->id]);
|
||||
|
||||
Event::assertDispatched(Created::class);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function update()
|
||||
{
|
||||
Event::fake([
|
||||
\Vanguard\Events\Role\Updated::class,
|
||||
]);
|
||||
|
||||
$role = Role::factory()->create();
|
||||
|
||||
$data = ['name' => 'foo', 'display_name' => 'Foo'];
|
||||
|
||||
$this->repo->update($role->id, $data);
|
||||
|
||||
$this->assertDatabaseHas('roles', $data + ['id' => $role->id]);
|
||||
|
||||
Event::assertDispatched(\Vanguard\Events\Role\Updated::class);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function delete_role()
|
||||
{
|
||||
Event::fake([
|
||||
\Vanguard\Events\Role\Deleted::class,
|
||||
]);
|
||||
|
||||
$role = Role::factory()->create();
|
||||
|
||||
$this->repo->delete($role->id);
|
||||
|
||||
$this->assertDatabaseMissing('roles', ['id' => $role->id]);
|
||||
|
||||
Event::assertDispatched(\Vanguard\Events\Role\Deleted::class);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function updatePermissions()
|
||||
{
|
||||
$role = Role::factory()->create();
|
||||
$permissions = \Vanguard\Permission::factory()->times(2)->create();
|
||||
|
||||
$this->repo->updatePermissions($role->id, $permissions->pluck('id')->toArray());
|
||||
|
||||
$this->assertDatabaseHas('permission_role', ['role_id' => $role->id, 'permission_id' => $permissions[0]->id]);
|
||||
$this->assertDatabaseHas('permission_role', ['role_id' => $role->id, 'permission_id' => $permissions[1]->id]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function lists()
|
||||
{
|
||||
$roles = Role::factory()->times(4)->create();
|
||||
$roles = $roles->pluck('display_name', 'id');
|
||||
|
||||
$this->assertEquals($roles->toArray(), $this->repo->lists()->toArray());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Repositories\Session;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use DB;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Str;
|
||||
use Tests\TestCase;
|
||||
use Vanguard\Repositories\Session\DbSession;
|
||||
use Vanguard\User;
|
||||
|
||||
class DbSessionTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @var DbSession
|
||||
*/
|
||||
protected $repo;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->repo = app(DbSession::class);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function get_user_session()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
Carbon::setTestNow(Carbon::now());
|
||||
|
||||
$data1 = $this->getSessionStubData($user);
|
||||
$data2 = $this->getSessionStubData($user);
|
||||
|
||||
DB::table('sessions')->insert($data1);
|
||||
DB::table('sessions')->insert($data2);
|
||||
|
||||
$expected = collect([
|
||||
(object) $this->addAddMissingFields($data1),
|
||||
(object) $this->addAddMissingFields($data2),
|
||||
]);
|
||||
$expected = $expected->sortBy('id')->keyBy('id')->toArray();
|
||||
|
||||
$actual = $this->repo->getUserSessions($user->id)
|
||||
->sortBy('id')
|
||||
->keyBy('id')
|
||||
->toArray();
|
||||
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
private function addAddMissingFields(array $data)
|
||||
{
|
||||
$agent = app('agent');
|
||||
$agent->setUserAgent($data['user_agent']);
|
||||
|
||||
return array_merge($data, [
|
||||
'browser' => $agent->browser(),
|
||||
'platform' => $agent->platform(),
|
||||
'device' => $agent->device(),
|
||||
'last_activity' => Carbon::createFromTimestamp($data['last_activity']),
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function if_get_user_sessions_will_return_active_sessions_only()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
Carbon::setTestNow(Carbon::now());
|
||||
|
||||
$data1 = $this->getSessionStubData($user);
|
||||
$data2 = $this->getSessionStubData($user);
|
||||
$data2['last_activity'] = Carbon::now()->subMinutes(config('session.lifetime') + 1)->timestamp;
|
||||
|
||||
DB::table('sessions')->insert($data1);
|
||||
DB::table('sessions')->insert($data2);
|
||||
|
||||
$expected = collect([
|
||||
(object) $this->addAddMissingFields($data1),
|
||||
]);
|
||||
|
||||
$expected = $expected->sortBy('id')->keyBy('id')->toArray();
|
||||
|
||||
$actual = $this->repo->getUserSessions($user->id)
|
||||
->sortBy('id')
|
||||
->keyBy('id')
|
||||
->toArray();
|
||||
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function invalidate_user_session()
|
||||
{
|
||||
$user = User::factory()->create([
|
||||
'remember_token' => Str::random(60),
|
||||
]);
|
||||
|
||||
$data = $this->getSessionStubData($user);
|
||||
DB::table('sessions')->insert($data);
|
||||
|
||||
$this->repo->invalidateSession($data['id']);
|
||||
|
||||
$this->assertDatabaseMissing('sessions', $data)
|
||||
->assertDatabaseHas('users', ['remember_token' => null]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function invalidate_all_sessions_for_user()
|
||||
{
|
||||
$user = User::factory()->create([
|
||||
'remember_token' => Str::random(60),
|
||||
]);
|
||||
|
||||
$data = $this->getSessionStubData($user);
|
||||
DB::table('sessions')->insert($data);
|
||||
|
||||
$this->repo->invalidateAllSessionsForUser($user->id);
|
||||
|
||||
$this->assertDatabaseMissing('sessions', ['user_id' => $user->id])
|
||||
->assertDatabaseHas('users', ['remember_token' => null]);
|
||||
}
|
||||
|
||||
private function getSessionStubData($user)
|
||||
{
|
||||
$faker = app(\Faker\Generator::class);
|
||||
|
||||
return [
|
||||
'id' => Str::random(),
|
||||
'user_id' => $user->id,
|
||||
'ip_address' => $faker->ipv4,
|
||||
'user_agent' => $faker->userAgent,
|
||||
'payload' => 'foo',
|
||||
'last_activity' => Carbon::now()->timestamp,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,450 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Repositories\User;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use DB;
|
||||
use Facades\Tests\Setup\UserFactory;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Testing\Assert;
|
||||
use Tests\TestCase;
|
||||
use Vanguard\Repositories\User\EloquentUser;
|
||||
use Vanguard\Role;
|
||||
use Vanguard\Support\Enum\UserStatus;
|
||||
use Vanguard\User;
|
||||
|
||||
class EloquentUserTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @var EloquentUser
|
||||
*/
|
||||
protected $repo;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->repo = app(EloquentUser::class);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function find()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->assertTrue($user->is($this->repo->findByEmail($user->email)));
|
||||
|
||||
$this->assertNull($this->repo->find(123));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function find_by_email()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->assertTrue($user->is($this->repo->findByEmail($user->email)));
|
||||
|
||||
$this->assertNull($this->repo->findByEmail('foo@bar.com'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function find_by_social_id()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
DB::table('social_logins')->insert([
|
||||
'user_id' => $user->id,
|
||||
'provider' => 'foo',
|
||||
'provider_id' => '123',
|
||||
'avatar' => '',
|
||||
'created_at' => Carbon::now(),
|
||||
]);
|
||||
|
||||
Assert::assertArraySubset(
|
||||
$user->toArray(),
|
||||
$this->repo->findBySocialId('foo', '123')->toArray()
|
||||
);
|
||||
|
||||
$this->assertNull($this->repo->findBySocialId('bar', '111'));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function find_by_session_id()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$sessionId = Str::random(40);
|
||||
|
||||
DB::table('sessions')->insert([
|
||||
'id' => $sessionId,
|
||||
'user_id' => $user->id,
|
||||
'ip_address' => '127.0.0.1',
|
||||
'user_agent' => 'foo',
|
||||
'payload' => Str::random(),
|
||||
'last_activity' => Carbon::now(),
|
||||
]);
|
||||
|
||||
Assert::assertArraySubset(
|
||||
$user->toArray(),
|
||||
$this->repo->findBySessionId($sessionId)->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function create()
|
||||
{
|
||||
$data = User::factory()->make()->toArray();
|
||||
|
||||
$this->repo->create($data + ['password' => 'foo']);
|
||||
|
||||
$this->assertDatabaseHas('users', $data);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function associate_facebook_account_to_a_user()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
Carbon::setTestNow(Carbon::now());
|
||||
|
||||
$socialUser = new \Laravel\Socialite\One\User();
|
||||
$socialUser->map([
|
||||
'id' => '123',
|
||||
'avatar' => 'foo',
|
||||
'avatar_original' => 'foo?width=1920',
|
||||
]);
|
||||
|
||||
$this->repo->associateSocialAccountForUser($user->id, 'facebook', $socialUser);
|
||||
|
||||
$this->assertDatabaseHas('social_logins', [
|
||||
'user_id' => $user->id,
|
||||
'provider' => 'facebook',
|
||||
'provider_id' => '123',
|
||||
'avatar' => 'foo?width=150',
|
||||
'created_at' => Carbon::now(),
|
||||
]);
|
||||
|
||||
Carbon::setTestNow(null);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function associate_twitter_account_to_a_user()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
Carbon::setTestNow(Carbon::now());
|
||||
|
||||
$socialUser = new \Laravel\Socialite\One\User();
|
||||
$socialUser->map([
|
||||
'id' => '123',
|
||||
'avatar' => 'foo_normal.jpg',
|
||||
]);
|
||||
|
||||
$this->repo->associateSocialAccountForUser($user->id, 'twitter', $socialUser);
|
||||
|
||||
$this->assertDatabaseHas('social_logins', [
|
||||
'user_id' => $user->id,
|
||||
'provider' => 'twitter',
|
||||
'provider_id' => '123',
|
||||
'avatar' => 'foo_200x200.jpg',
|
||||
'created_at' => Carbon::now(),
|
||||
]);
|
||||
|
||||
Carbon::setTestNow(null);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function associate_google_account_to_a_user()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
Carbon::setTestNow(Carbon::now());
|
||||
|
||||
$socialUser = new \Laravel\Socialite\One\User();
|
||||
$socialUser->map([
|
||||
'id' => '123',
|
||||
'avatar' => 'avatar.jpg',
|
||||
'avatar_original' => 'avatar_original.jpg',
|
||||
]);
|
||||
|
||||
$this->repo->associateSocialAccountForUser($user->id, 'google', $socialUser);
|
||||
|
||||
$this->assertDatabaseHas('social_logins', [
|
||||
'user_id' => $user->id,
|
||||
'provider' => 'google',
|
||||
'provider_id' => '123',
|
||||
'avatar' => 'avatar_original.jpg?sz=150',
|
||||
'created_at' => Carbon::now(),
|
||||
]);
|
||||
|
||||
Carbon::setTestNow(null);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function paginate()
|
||||
{
|
||||
$users = User::factory()->times(5)->create();
|
||||
$users = $users->sortByDesc('id')->values();
|
||||
|
||||
$result = $this->repo->paginate(2)->toArray();
|
||||
|
||||
$this->assertEquals(2, count($result['data']));
|
||||
$this->assertEquals(5, $result['total']);
|
||||
Assert::assertArraySubset($users[0]->toArray(), $result['data'][0]);
|
||||
Assert::assertArraySubset($users[1]->toArray(), $result['data'][1]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function paginate_with_status()
|
||||
{
|
||||
User::factory()->times(3)->create();
|
||||
User::factory()->create(['status' => UserStatus::BANNED]);
|
||||
|
||||
$active = $this->repo->paginate(2, null, UserStatus::ACTIVE->value)->toArray();
|
||||
$banned = $this->repo->paginate(2, null, UserStatus::BANNED->value)->toArray();
|
||||
|
||||
$this->assertEquals(2, count($active['data']));
|
||||
$this->assertEquals(3, $active['total']);
|
||||
|
||||
$this->assertEquals(1, count($banned['data']));
|
||||
$this->assertEquals(1, $banned['total']);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function paginate_with_search()
|
||||
{
|
||||
User::factory()->create([
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'username' => 'jdoe',
|
||||
'email' => 'joe@test.com',
|
||||
]);
|
||||
|
||||
User::factory()->create([
|
||||
'first_name' => 'Jane',
|
||||
'last_name' => 'Doe',
|
||||
'username' => 'janedoe',
|
||||
'email' => 'jane@doe.com',
|
||||
]);
|
||||
|
||||
User::factory()->create([
|
||||
'first_name' => 'Milos',
|
||||
'last_name' => 'Stojanovic',
|
||||
'email' => 'test@test.com',
|
||||
]);
|
||||
|
||||
$this->assertEquals(2, $this->repo->paginate(25, 'doe')->total());
|
||||
$this->assertEquals(1, $this->repo->paginate(25, 'Milos')->total());
|
||||
$this->assertEquals(2, $this->repo->paginate(25, 'test')->total());
|
||||
$this->assertEquals(2, $this->repo->paginate(25, 'an')->total());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function update_user()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$data = [
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'username' => 'foo',
|
||||
'email' => 'test@test.com',
|
||||
];
|
||||
|
||||
$this->repo->update($user->id, $data);
|
||||
|
||||
$this->assertDatabaseHas('users', $data + ['id' => $user->id]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function update_when_provided_country_is_zero()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$data = [
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'username' => 'foo',
|
||||
'email' => 'test@test.com',
|
||||
'country_id' => 0,
|
||||
];
|
||||
|
||||
$this->repo->update($user->id, $data);
|
||||
|
||||
$this->assertDatabaseHas('users', array_merge($data, ['id' => $user->id, 'country_id' => null]));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function delete_user()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->repo->delete($user->id);
|
||||
|
||||
$this->assertDatabaseMissing('users', ['id' => $user->id]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function count_users()
|
||||
{
|
||||
User::factory()->times(7)->create();
|
||||
|
||||
$this->assertEquals(7, $this->repo->count());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function new_users_count()
|
||||
{
|
||||
Carbon::setTestNow(Carbon::now()->startOfMonth()->subMonth());
|
||||
User::factory()->times(3)->create();
|
||||
|
||||
Carbon::setTestNow(null);
|
||||
User::factory()->times(5)->create();
|
||||
|
||||
$this->assertEquals(5, $this->repo->newUsersCount());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function count_by_status()
|
||||
{
|
||||
User::factory()->times(3)->create();
|
||||
User::factory()->create(['status' => UserStatus::BANNED]);
|
||||
User::factory()->times(2)->create(['status' => UserStatus::UNCONFIRMED]);
|
||||
|
||||
$this->assertEquals(3, $this->repo->countByStatus(UserStatus::ACTIVE));
|
||||
$this->assertEquals(1, $this->repo->countByStatus(UserStatus::BANNED));
|
||||
$this->assertEquals(2, $this->repo->countByStatus(UserStatus::UNCONFIRMED));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function latest()
|
||||
{
|
||||
Carbon::setTestNow(now()->subDay());
|
||||
$user4 = User::factory()->create();
|
||||
|
||||
Carbon::setTestNow(now()->subMinutes(3));
|
||||
$user3 = User::factory()->create();
|
||||
|
||||
Carbon::setTestNow(now()->subMinutes(2));
|
||||
$user2 = User::factory()->create();
|
||||
|
||||
Carbon::setTestNow(now()->subMinutes(1));
|
||||
$user1 = User::factory()->create();
|
||||
|
||||
$latestTwo = $this->repo->latest(2);
|
||||
$latestFour = $this->repo->latest(4);
|
||||
|
||||
$this->assertEquals(2, count($latestTwo));
|
||||
$this->assertEquals(4, count($latestFour));
|
||||
|
||||
Assert::assertArraySubset($user4->toArray(), $latestTwo[0]->toArray());
|
||||
Assert::assertArraySubset($user3->toArray(), $latestTwo[1]->toArray());
|
||||
Assert::assertArraySubset($user1->toArray(), $latestFour[3]->toArray());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function count_of_new_users_per_month()
|
||||
{
|
||||
$from = now()->startOfYear();
|
||||
|
||||
Carbon::setTestNow($from);
|
||||
User::factory()->times(2)->create();
|
||||
|
||||
Carbon::setTestNow($from->copy()->addMonths(2));
|
||||
User::factory()->times(4)->create();
|
||||
|
||||
Carbon::setTestNow($from->copy()->addMonths(6));
|
||||
User::factory()->times(2)->create();
|
||||
|
||||
Carbon::setTestNow($from->copy()->addMonths(7));
|
||||
User::factory()->times(1)->create();
|
||||
|
||||
Carbon::setTestNow($from->copy()->addMonths(10));
|
||||
User::factory()->times(4)->create();
|
||||
|
||||
Carbon::setTestNow(null);
|
||||
|
||||
$currentYear = now()->year;
|
||||
|
||||
$expected = [
|
||||
"January {$currentYear}" => 2,
|
||||
"February {$currentYear}" => 0,
|
||||
"March {$currentYear}" => 4,
|
||||
"April {$currentYear}" => 0,
|
||||
"May {$currentYear}" => 0,
|
||||
"June {$currentYear}" => 0,
|
||||
"July {$currentYear}" => 2,
|
||||
"August {$currentYear}" => 1,
|
||||
"September {$currentYear}" => 0,
|
||||
"October {$currentYear}" => 0,
|
||||
"November {$currentYear}" => 4,
|
||||
"December {$currentYear}" => 0,
|
||||
];
|
||||
|
||||
$usersPerMonth = $this->repo->countOfNewUsersPerMonthPerRole(
|
||||
now()->startOfYear(),
|
||||
now()->endOfYear()
|
||||
);
|
||||
|
||||
$this->assertEquals($expected, $usersPerMonth);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function get_users_with_specific_role()
|
||||
{
|
||||
$this->artisan('db:seed --class=RolesSeeder');
|
||||
|
||||
$admin = UserFactory::admin()->create();
|
||||
$user = UserFactory::user()->create();
|
||||
|
||||
$result = $this->repo->getUsersWithRole('Admin');
|
||||
|
||||
$this->assertEquals(1, $result->count());
|
||||
$this->assertTrue($admin->is($result[0]));
|
||||
|
||||
$result = $this->repo->getUsersWithRole('User');
|
||||
$this->assertEquals(1, $result->count());
|
||||
$this->assertTrue($user->is($result[0]));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function set_role()
|
||||
{
|
||||
$this->artisan('db:seed --class=RolesSeeder');
|
||||
|
||||
$user = UserFactory::user()->create();
|
||||
$role = Role::where('name', 'Admin')->first();
|
||||
|
||||
$this->repo->setRole($user->id, $role->id);
|
||||
|
||||
$this->assertDatabaseHas('users', [
|
||||
'role_id' => $role->id,
|
||||
'id' => $user->id,
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function switch_roles_for_a_user()
|
||||
{
|
||||
$this->artisan('db:seed --class=RolesSeeder');
|
||||
|
||||
$adminRole = Role::where('name', 'Admin')->first();
|
||||
|
||||
$userA = UserFactory::user()->create();
|
||||
$userB = UserFactory::user()->create();
|
||||
|
||||
$this->repo->switchRolesForUsers($userA->role_id, $adminRole->id);
|
||||
|
||||
$this->assertDatabaseHas('users', [
|
||||
'role_id' => $adminRole->id,
|
||||
'id' => $userA->id,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('users', [
|
||||
'role_id' => $adminRole->id,
|
||||
'id' => $userB->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user