. */ namespace SP\Tests\Services\UserProfile; use DI\DependencyException; use DI\NotFoundException; use SP\Core\Context\ContextException; use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\QueryException; use SP\Core\Exceptions\SPException; use SP\DataModel\ItemSearchData; use SP\DataModel\ProfileData; use SP\DataModel\UserProfileData; use SP\Repositories\DuplicatedItemException; use SP\Repositories\NoSuchItemException; use SP\Services\ServiceException; use SP\Services\UserProfile\UserProfileService; use SP\Tests\DatabaseTestCase; use stdClass; use function SP\Tests\setupContext; /** * Class UserProfileServiceTest * * @package SP\Tests\SP\Services\UserProfile */ class UserProfileServiceTest extends DatabaseTestCase { /** * @var UserProfileService */ private static $service; /** * @throws NotFoundException * @throws ContextException * @throws DependencyException * @throws SPException */ public static function setUpBeforeClass(): void { $dic = setupContext(); self::$loadFixtures = true; // Inicializar el servicio self::$service = $dic->get(UserProfileService::class); } /** * @throws ConstraintException * @throws QueryException */ public function testSearch() { $itemSearchData = new ItemSearchData(); $itemSearchData->setLimitCount(10); $itemSearchData->setSeachString('Demo'); $result = self::$service->search($itemSearchData); $data = $result->getDataAsArray(); $this->assertEquals(1, $result->getNumRows()); $this->assertCount(1, $data); $this->assertInstanceOf(stdClass::class, $data[0]); $this->assertEquals(2, $data[0]->id); $this->assertEquals('Demo', $data[0]->name); // Nueva búsqueda de perfil no existente $itemSearchData->setSeachString('prueba'); $result = self::$service->search($itemSearchData); $this->assertEquals(0, $result->getNumRows()); $this->assertCount(0, $result->getDataAsArray()); } /** * @throws ConstraintException * @throws QueryException */ public function testGetAllBasic() { $data = self::$service->getAllBasic(); $this->assertCount(3, $data); $this->assertInstanceOf(UserProfileData::class, $data[0]); $this->assertEquals('Admin', $data[0]->getName()); $this->assertInstanceOf(UserProfileData::class, $data[1]); $this->assertEquals('Demo', $data[1]->getName()); } /** * @throws ConstraintException * @throws QueryException */ public function testGetUsersForProfile() { $this->assertCount(1, self::$service->getUsersForProfile(2)); $this->assertCount(0, self::$service->getUsersForProfile(3)); $this->assertCount(0, self::$service->getUsersForProfile(10)); } /** * @throws ConstraintException * @throws QueryException * @throws SPException */ public function testUpdate() { $data = new UserProfileData(); $data->setId(2); $data->setName('Test Profile'); self::$service->update($data); $this->assertTrue(true); } /** * @throws ConstraintException * @throws QueryException * @throws SPException */ public function testUpdateUnknown() { $data = new UserProfileData(); $data->setId(10); $data->setName('Test Profile'); $this->expectException(ServiceException::class); self::$service->update($data); } /** * @throws ConstraintException * @throws QueryException * @throws SPException */ public function testUpdateDuplicated() { $data = new UserProfileData(); $data->setId(2); $data->setName('Admin'); $this->expectException(DuplicatedItemException::class); self::$service->update($data); } /** * @throws ConstraintException * @throws QueryException * @throws ServiceException */ public function testDeleteByIdBatch() { $this->assertEquals(1, self::$service->deleteByIdBatch([3])); $this->assertEquals(2, self::getRowCount('UserProfile')); } /** * @throws ConstraintException * @throws QueryException * @throws ServiceException */ public function testDeleteByIdBatchUsed() { $this->expectException(ConstraintException::class); self::$service->deleteByIdBatch([1, 2]); $this->assertEquals(3, self::getRowCount('UserProfile')); } /** * @throws ConstraintException * @throws QueryException * @throws ServiceException */ public function testDeleteByIdBatchUnknown() { $this->expectException(ServiceException::class); self::$service->deleteByIdBatch([3, 10]); $this->assertEquals(2, self::getRowCount('UserProfile')); } /** * @throws ConstraintException * @throws QueryException * @throws NoSuchItemException */ public function testGetById() { $result = self::$service->getById(2); $this->assertInstanceOf(UserProfileData::class, $result); $this->assertInstanceOf(ProfileData::class, $result->getProfile()); $this->expectException(NoSuchItemException::class); self::$service->getById(10); } /** * @throws ConstraintException * @throws NoSuchItemException * @throws QueryException */ public function testDelete() { self::$service->delete(3); $this->assertEquals(2, self::getRowCount('UserProfile')); $this->expectException(ConstraintException::class); self::$service->delete(1); } /** * @throws SPException */ public function testCreate() { $profileData = new ProfileData(); $profileData->setAccAdd(true); $profileData->setAccDelete(true); $profileData->setConfigBackup(true); $data = new UserProfileData(); $data->setId(4); $data->setName('Prueba'); $data->setProfile($profileData); $result = self::$service->create($data); $this->assertEquals($data->getId(), $result); $this->assertEquals(4, self::getRowCount('UserProfile')); $this->assertEquals($data, self::$service->getById($result)); } /** * @throws SPException */ public function testCreateDuplicated() { $data = new UserProfileData(); $data->setName('Admin'); $data->setProfile(new ProfileData()); $this->expectException(DuplicatedItemException::class); self::$service->create($data); } }