UserServiceTest.php
1 <?php 2 3 namespace Tests\Services; 4 5 use App\Http\Services\UserService; 6 use App\Models\Dossier; 7 use App\Models\User; 8 use Illuminate\Support\Collection; 9 use Tests\TestCase; 10 11 use function PHPUnit\Framework\assertTrue; 12 13 class UserServiceTest extends TestCase 14 { 15 public function testFind() 16 { 17 $admin = $this->getAdminUser(); 18 $this->assertInstanceOf(User::class, UserService::find($admin->id)); 19 } 20 21 public function testSearchUsers() 22 { 23 $query = UserService::searchUsers(true, '', 'id', 'asc', []); 24 $this->assertInstanceOf(\Illuminate\Database\Eloquent\Builder::class, $query); 25 $query = UserService::searchUsers(false, '', 'id', 'asc', []); 26 $this->assertInstanceOf(\Illuminate\Database\Eloquent\Builder::class, $query); 27 } 28 29 public function testSearchUsersByDossierId() 30 { 31 $dossier = Dossier::factory()->create(); 32 $query = UserService::searchUsersByDossierId($dossier->id, '', 'id', 'asc', []); 33 $this->assertInstanceOf( 34 \Illuminate\Database\Eloquent\Relations\MorphToMany::class, 35 $query 36 ); 37 } 38 39 /** 40 * @throws \Throwable 41 */ 42 public function testCreate() 43 { 44 $data = $this->exampleDataArray(); 45 $created = UserService::create($data['user'], $data['relation']); 46 $this->assertTrue($created); 47 } 48 49 /** 50 * @throws \Throwable 51 */ 52 public function testUpdate() 53 { 54 $data = $this->exampleDataArray(); 55 UserService::create($data['user'], $data['relation']); 56 $userId = User::latest()->first()->id; 57 $data['user']['name'] = "Other name"; 58 $updated = UserService::update($data,$userId); 59 $this->assertTrue($updated); 60 } 61 62 /** 63 * @throws \Throwable 64 */ 65 public function testRemove() 66 { 67 $user = User::factory()->create(); 68 UserService::remove($user->id); 69 $this->assertSoftDeleted($user->getTable(), ['id' => $user->id]); 70 } 71 72 public function testRestore() 73 { 74 $user = User::factory()->create(); 75 $user->delete(); 76 $this->assertSoftDeleted($user->getTable(), $user->getAttributes()); 77 $restore = UserService::restore($user->id); 78 $this->assertTrue($restore); 79 } 80 81 public function testGetInternalUser() 82 { 83 $query = UserService::getInternalUsers(); 84 $this->assertInstanceOf(Collection::class, $query); 85 } 86 87 public function testGetExternalUser() 88 { 89 $query = UserService::getExternalUsers(); 90 $this->assertInstanceOf(Collection::class, $query); 91 } 92 93 public function testGetAllUsers() 94 { 95 $query = UserService::getAllUsers(); 96 $this->assertInstanceOf(Collection::class, $query); 97 } 98 99 public function testChangePassword() 100 { 101 $user = $this->getAdminUser(); 102 $newPassword = "1234"; 103 $this->assertTrue( 104 UserService::changePassword($user->id, ['new_password' => $newPassword]) 105 ); 106 } 107 108 private function exampleDataArray() : array 109 { 110 return [ 111 'user' => [ 112 'user_type_id' => 1, 113 'username' => "New UserName", 114 'name' => "New Name", 115 'lastname' => "New Last Name", 116 'email' => "123@123.com", 117 'password' => "1234", 118 'phone' => "123456789", 119 'address' => "New Address", 120 'internal_identification_number' => "1234", 121 'personal_identification_number' => "123456789P", 122 'can_access_inbox' => false, 123 ], 124 'relation' => [ 125 'entity_id' => 1, 126 'role_id' => 1, 127 ], 128 ]; 129 } 130 }