. */ namespace SP\Tests\Services\UserGroup; 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\UserToUserGroupData; use SP\Repositories\NoSuchItemException; use SP\Services\UserGroup\UserToUserGroupService; use SP\Tests\DatabaseTestCase; use function SP\Tests\setupContext; /** * Class UserToUserGroupServiceTest * * @package SP\Tests\SP\Services\UserGroup */ class UserToUserGroupServiceTest extends DatabaseTestCase { /** * @var UserToUserGroupService */ 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(UserToUserGroupService::class); } /** * @throws ConstraintException * @throws QueryException */ public function testCheckUserInGroup() { $this->assertTrue(self::$service->checkUserInGroup(1, 2)); $this->assertTrue(self::$service->checkUserInGroup(2, 3)); $this->assertFalse(self::$service->checkUserInGroup(3, 3)); } /** * @throws ConstraintException * @throws QueryException */ public function testGetGroupsForUser() { $data = self::$service->getGroupsForUser(3); $this->assertCount(1, $data); $this->assertEquals(2, $data[0]->userGroupId); $data = self::$service->getGroupsForUser(2); $this->assertCount(1, $data); $this->assertEquals(1, $data[0]->userGroupId); $data = self::$service->getGroupsForUser(10); $this->assertCount(0, $data); } /** * @throws ConstraintException * @throws QueryException */ public function testAdd() { $data = [3, 4]; self::$service->add(1, $data); $this->assertEquals([2, 3, 4], self::$service->getUsersByGroupId(1)); $this->expectException(ConstraintException::class); self::$service->add(10, $data); } /** * @throws ConstraintException * @throws QueryException */ public function testAddDuplicated() { $data = [2, 3, 4]; $this->expectException(ConstraintException::class); self::$service->add(1, $data); } /** * @throws ConstraintException * @throws QueryException */ public function testUpdate() { $data = [3, 4]; self::$service->update(1, $data); $this->assertEquals($data, self::$service->getUsersByGroupId(1)); $this->expectException(ConstraintException::class); self::$service->update(10, $data); } /** * @throws ConstraintException * @throws QueryException * @throws NoSuchItemException */ public function testGetById() { $data = self::$service->getById(2); $this->assertCount(2, $data); $this->assertInstanceOf(UserToUserGroupData::class, $data[0]); $this->assertEquals(2, $data[0]->getUserGroupId()); $this->assertEquals(1, $data[0]->getUserId()); $this->assertEquals(2, $data[1]->getUserGroupId()); $this->assertEquals(3, $data[1]->getUserId()); $data = self::$service->getById(1); $this->assertCount(1, $data); $this->assertEquals(1, $data[0]->getUserGroupId()); $this->assertEquals(2, $data[0]->getUserId()); $this->expectException(NoSuchItemException::class); self::$service->getById(10); } /** * @throws ConstraintException * @throws QueryException */ public function testGetUsersByGroupId() { $data = self::$service->getUsersByGroupId(2); $this->assertCount(2, $data); $this->assertEquals([1, 3], $data); } }