. */ namespace SP\Tests; use DI\DependencyException; use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\QueryException; use SP\DataModel\ItemSearchData; use SP\DataModel\ProfileData; use SP\DataModel\UserProfileData; use SP\Repositories\DuplicatedItemException; use SP\Repositories\UserProfile\UserProfileRepository; use SP\Storage\DatabaseConnectionData; /** * Class UserProfileRepositoryTest * * Tests de integración para comprobar las consultas a la BBDD relativas a los perfiles de usuarios * * @package SP\Tests */ class UserProfileRepositoryTest extends DatabaseTestCase { /** * @var UserProfileRepository */ private static $userProfileRepository; /** * @throws DependencyException * @throws \DI\NotFoundException * @throws \SP\Core\Context\ContextException */ public static function setUpBeforeClass() { $dic = setupContext(); // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); // Inicializar el repositorio self::$userProfileRepository = $dic->get(UserProfileRepository::class); } /** * Comprobar la obtención de perfiles */ public function testGetAll() { $profiles = self::$userProfileRepository->getAll(); $this->assertCount(3, $profiles); $this->assertInstanceOf(UserProfileData::class, $profiles[0]); $this->assertEquals('Admin', $profiles[0]->getName()); $this->assertInstanceOf(UserProfileData::class, $profiles[1]); $this->assertEquals('Demo', $profiles[1]->getName()); } /** * Comprobar la búsqueda de perfiles */ public function testSearch() { $itemSearchData = new ItemSearchData(); $itemSearchData->setLimitCount(10); $itemSearchData->setSeachString('Demo'); $search = self::$userProfileRepository->search($itemSearchData); $this->assertCount(2, $search); $this->assertArrayHasKey('count', $search); $this->assertEquals(1, $search['count']); $this->assertEquals(2, $search[0]->id); $this->assertEquals('Demo', $search[0]->name); // Nueva búsqueda de perfil no existente $itemSearchData->setSeachString('prueba'); $search = self::$userProfileRepository->search($itemSearchData); $this->assertCount(1, $search); $this->assertArrayHasKey('count', $search); $this->assertEquals(0, $search['count']); } /** * Comprobar la actualización de perfiles * * @covers \SP\Repositories\UserGroup\UserGroupRepository::checkDuplicatedOnUpdate() * @throws ConstraintException * @throws DuplicatedItemException * @throws QueryException */ public function testUpdate() { $userProfileData = new UserProfileData(); $userProfileData->setId(2); $userProfileData->setName('Perfil Demo'); $this->assertEquals(1, self::$userProfileRepository->update($userProfileData)); $this->expectException(DuplicatedItemException::class); $userProfileData->setName('Admin'); self::$userProfileRepository->update($userProfileData); } /** * Comprobar la eliminación de perfiles * * @throws ConstraintException * @throws QueryException */ public function testDelete() { $result = self::$userProfileRepository->delete(3); $this->assertEquals(1, $result); $this->assertEquals(2, $this->conn->getRowCount('UserProfile')); $this->expectException(ConstraintException::class); self::$userProfileRepository->delete(1); self::$userProfileRepository->delete(2); } /** * Comprobar si el perfil está en uso * * @throws ConstraintException * @throws QueryException */ public function testCheckInUse() { $this->assertTrue(self::$userProfileRepository->checkInUse(1)); $this->assertTrue(self::$userProfileRepository->checkInUse(2)); $this->assertFalse(self::$userProfileRepository->checkInUse(3)); } /** * Comprobar la creación de perfiles * * @throws ConstraintException * @throws DuplicatedItemException * @throws QueryException */ public function testCreate() { $profileData = new ProfileData(); $profileData->setAccAdd(true); $profileData->setAccDelete(true); $profileData->setConfigBackup(true); $userProfileData = new UserProfileData(); $userProfileData->setName('Prueba'); $userProfileData->setProfile($profileData); $result = self::$userProfileRepository->create($userProfileData); $this->assertEquals(4, $result); $this->assertEquals(4, $this->conn->getRowCount('UserProfile')); $this->expectException(DuplicatedItemException::class); $userProfileData->setName('Demo'); self::$userProfileRepository->create($userProfileData); } /** * Comprobar la obtención de perfiles por Id */ public function testGetById() { $profile = self::$userProfileRepository->getById(2); $this->assertInstanceOf(UserProfileData::class, $profile); $this->assertEquals('Demo', $profile->getName()); $this->assertNotEmpty($profile->getProfile()); $profile = self::$userProfileRepository->getById(4); $this->assertCount(0, $profile); } /** * Comprobar la obtención de los usuarios asociados a un perfil */ public function testGetUsersForProfile() { $this->assertCount(1, self::$userProfileRepository->getUsersForProfile(2)); $this->assertCount(0, self::$userProfileRepository->getUsersForProfile(3)); } /** * Comprobar la obtención de perfiles en lote */ public function testGetByIdBatch() { $profiles = self::$userProfileRepository->getByIdBatch([1, 2, 5]); $this->assertCount(2, $profiles); $this->assertInstanceOf(UserProfileData::class, $profiles[0]); $this->assertEquals(1, $profiles[0]->getId()); $this->assertEquals('Admin', $profiles[0]->getName()); $this->assertInstanceOf(UserProfileData::class, $profiles[1]); } /** * Comprobar la eliminación de perfiles en lote * * @throws ConstraintException * @throws QueryException */ public function testDeleteByIdBatch() { // Se lanza excepción en caso de restricción relacional $this->expectException(ConstraintException::class); $result = self::$userProfileRepository->deleteByIdBatch([1, 2, 3, 4]); $this->assertEquals(1, $result); } }