From afc112c9567ff94b207a3fa0ba3fc3f55cd9da75 Mon Sep 17 00:00:00 2001 From: s3b4stian Date: Mon, 11 Dec 2023 12:10:35 +0100 Subject: [PATCH] testa updated --- .../Authorization/PermissionExtendedTest.php | 41 ++-- tests/Linna/Authorization/PermissionTest.php | 68 +++---- .../Linna/Authorization/RoleExtendedTest.php | 75 +++---- tests/Linna/Authorization/RoleTest.php | 187 +----------------- .../Linna/Authorization/UserExtendedTest.php | 40 ++-- 5 files changed, 100 insertions(+), 311 deletions(-) diff --git a/tests/Linna/Authorization/PermissionExtendedTest.php b/tests/Linna/Authorization/PermissionExtendedTest.php index f8d8f65..9b92652 100644 --- a/tests/Linna/Authorization/PermissionExtendedTest.php +++ b/tests/Linna/Authorization/PermissionExtendedTest.php @@ -12,6 +12,7 @@ namespace Linna\Authorization; +use Linna\Authentication\Password; use Linna\Storage\ExtendedPDO; use Linna\Storage\StorageFactory; use PHPUnit\Framework\TestCase; @@ -20,10 +21,10 @@ class PermissionExtendedTest extends TestCase { /** @var PermissionExtendedMapper The permission mapper */ - //protected static PermissionExtendedMapper $permissionMapper; + protected static PermissionExtendedMapper $permissionExtendedMapper; /** @var ExtendedPDO Database connection. */ - //protected static ExtendedPDO $pdo; + protected static ExtendedPDO $pdo; /** * Set up before class. @@ -44,21 +45,17 @@ public static function setUpBeforeClass(): void ], ];*/ - //$pdo = (new StorageFactory('pdo', PdoOptionsFactory::getOptions()))->get(); + $pdo = (new StorageFactory('pdo', PdoOptionsFactory::getOptions()))->get(); - //self::$pdo = $pdo; - //self::$permissionMapper = new PermissionExtendedMapper($pdo); - } + $password = new Password(); - /** - * Tear down after class. - * - * @return void - */ - public static function tearDownAfterClass(): void - { - //self::$pdo = null; - //self::$permissionMapper = null; + $roleMapper = new RoleMapper($pdo); + $userMapper = new UserMapper($pdo, $password); + $permissionExtendedMapper = new PermissionExtendedMapper($pdo, $roleMapper, $userMapper); + + //declared in trait + self::$pdo = $pdo; + self::$permissionExtendedMapper = $permissionExtendedMapper; } /** @@ -68,7 +65,7 @@ public static function tearDownAfterClass(): void */ public function testNewRoleInstance(): void { - //$this->assertInstanceOf(Permission::class, self::$permissionMapper->create()); + $this->assertInstanceOf(Permission::class, self::$permissionExtendedMapper->create()); } /** @@ -78,13 +75,13 @@ public function testNewRoleInstance(): void */ public function testConstructorTypeCasting(): void { - //$permission = self::$permissionMapper->fetchById(1); + $permission = self::$permissionExtendedMapper->fetchById(1); - //$this->assertIsInt($permission->getId()); - //$this->assertIsInt($permission->id); - //$this->assertIsInt($permission->inherited); + $this->assertIsInt($permission->getId()); + $this->assertIsInt($permission->id); + $this->assertIsInt($permission->inherited); - //$this->assertGreaterThan(0, $permission->getId()); - //$this->assertGreaterThan(0, $permission->id); + $this->assertGreaterThan(0, $permission->getId()); + $this->assertGreaterThan(0, $permission->id); } } diff --git a/tests/Linna/Authorization/PermissionTest.php b/tests/Linna/Authorization/PermissionTest.php index be91f37..23f9250 100644 --- a/tests/Linna/Authorization/PermissionTest.php +++ b/tests/Linna/Authorization/PermissionTest.php @@ -12,18 +12,13 @@ namespace Linna\Authorization; -use Linna\Storage\ExtendedPDO; -use Linna\Storage\StorageFactory; +use DateTimeImmutable; use PHPUnit\Framework\TestCase; -use Linna\TestHelper\Pdo\PdoOptionsFactory; class PermissionTest extends TestCase { - /** @var PermissionMapper The permission mapper */ - //protected static PermissionMapper $permissionMapper; - - /** @var ExtendedPDO Database connection. */ - //protected static ExtendedPDO $pdo; + /** @var Permission The permission instance */ + protected static Permission $permission; /** * Set up before class. @@ -32,43 +27,33 @@ class PermissionTest extends TestCase */ public static function setUpBeforeClass(): void { - /*$options = [ - 'dsn' => $GLOBALS['pdo_mysql_dsn'], - 'user' => $GLOBALS['pdo_mysql_user'], - 'password' => $GLOBALS['pdo_mysql_password'], - 'options' => [ - PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, - PDO::ATTR_PERSISTENT => false, - PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci', - ], - ];*/ - - //$pdo = (new StorageFactory('pdo', PdoOptionsFactory::getOptions()))->get(); - - //self::$pdo = $pdo; - //self::$permissionMapper = new PermissionMapper($pdo); - } - - /** - * Tear down after class. - * - * @return void - */ - public static function tearDownAfterClass(): void - { - //self::$pdo = null; - //self::$permissionMapper = null; + self::$permission = new Permission( + name: 'test_permission', + description: 'test_permission_description', + inherited: 0, + created: new DateTimeImmutable(), + lastUpdate: new DateTimeImmutable() + ); } /** - * Test new role instance. - * + * Test new permission instance. + * * @return void - */ + */ public function testNewRoleInstance(): void { - //$this->assertInstanceOf(Permission::class, self::$permissionMapper->create()); + $permission = self::$permission; + + $this->assertInstanceOf(Permission::class, $permission); + $this->assertInstanceOf(DateTimeImmutable::class, $permission->created); + $this->assertInstanceOf(DateTimeImmutable::class, $permission->lastUpdate); + + //id null because not saved into persistent storage + $this->assertSame(null, $permission->id); + $this->assertSame('test_permission', $permission->name); + $this->assertSame('test_permission_description', $permission->description); + $this->assertSame(0, $permission->inherited); } /** @@ -78,13 +63,14 @@ public function testNewRoleInstance(): void */ public function testConstructorTypeCasting(): void { - /*$permission = self::$permissionMapper->fetchById(1); + $permission = self::$permission; + $permission->setId(1); $this->assertIsInt($permission->getId()); $this->assertIsInt($permission->id); $this->assertIsInt($permission->inherited); $this->assertGreaterThan(0, $permission->getId()); - $this->assertGreaterThan(0, $permission->id);*/ + $this->assertGreaterThan(0, $permission->id); } } diff --git a/tests/Linna/Authorization/RoleExtendedTest.php b/tests/Linna/Authorization/RoleExtendedTest.php index 3dd55f7..2a88fe2 100644 --- a/tests/Linna/Authorization/RoleExtendedTest.php +++ b/tests/Linna/Authorization/RoleExtendedTest.php @@ -13,7 +13,6 @@ namespace Linna\Authorization; use Linna\Authentication\Password; -use Linna\Authentication\UserMapper; use Linna\Storage\ExtendedPDO; use Linna\Storage\StorageFactory; use PHPUnit\Framework\TestCase; @@ -21,17 +20,17 @@ class RoleExtendedTest extends TestCase { - /** @var PermissionMapper The permission mapper */ - //protected static PermissionMapper $permissionMapper; + /** @var RoleExtendedMapper The role extended mapper */ + protected static RoleExtendedMapper $roleExtendedMapper; - /** @var UserMapper The enhanced user mapper */ - //protected static UserMapper $userMapper; + /** @var UserMapper The user mapper */ + protected static UserMapper $userMapper; - /** @var RoleExtendedMapper The role mapper */ - //protected static RoleExtendedMapper $roleExtendedMapper; + /** @var PermissionMapper The permission mapper */ + protected static PermissionMapper $permissionMapper; /** @var ExtendedPDO Database connection. */ - //protected static ExtendedPDO $pdo; + protected static ExtendedPDO $pdo; /** * Set up before class. @@ -58,27 +57,13 @@ public static function setUpBeforeClass(): void $permissionMapper = new PermissionMapper($pdo); $userMapper = new UserMapper($pdo, $password); - // fix here - $roleToUserMapper = new RoleToUserMapper($pdo, $password); - $UserExtendedMapper = new UserExtendedMapper($pdo, $password, $permissionMapper, $roleToUserMapper); + + $roleExtendedMapper = new RoleExtendedMapper($pdo, $permissionMapper, $userMapper); self::$pdo = $pdo; - self::$roleMapper = new RoleMapper($pdo, $permissionMapper, $userMapper, $roleToUserMapper); + self::$userMapper = $userMapper; self::$permissionMapper = $permissionMapper; - self::$UserExtendedMapper = $UserExtendedMapper; - } - - /** - * Tear down after class. - * - * @return void - */ - public static function tearDownAfterClass(): void - { - //self::$pdo = null; - //self::$roleMapper = null; - //self::$permissionMapper = null; - //self::$UserExtendedMapper = null; + self::$roleExtendedMapper = $roleExtendedMapper; } /** @@ -88,7 +73,7 @@ public static function tearDownAfterClass(): void */ public function testNewRoleInstance(): void { - $this->assertInstanceOf(Role::class, self::$roleMapper->create()); + $this->assertInstanceOf(Role::class, self::$roleExtendedMapper->create()); } /** @@ -98,7 +83,7 @@ public function testNewRoleInstance(): void */ public function testConstructorTypeCasting(): void { - $role = self::$roleMapper->fetchById(1); + $role = self::$roleExtendedMapper->fetchById(1); $this->assertIsInt($role->getId()); $this->assertIsInt($role->id); @@ -153,12 +138,12 @@ public static function userRoleProvider(): array */ public function testIsUserInRole(int $roleId, int $userId, bool $result): void { - /** @var Role Role Class. */ - $role = self::$roleMapper->fetchById($roleId); + /** @var RoleExtended Role Class. */ + $role = self::$roleExtendedMapper->fetchById($roleId); /** @var UserExtended Enhanced User Class. */ - $user = self::$UserExtendedMapper->fetchById($userId); - $this->assertEquals($result, $role->isUserInRole($user)); + $user = self::$userMapper->fetchById($userId); + $this->assertEquals($result, $role->hasUser($user)); } /** @@ -174,10 +159,10 @@ public function testIsUserInRole(int $roleId, int $userId, bool $result): void */ public function testIsUserInRoleById(int $roleId, int $userId, bool $result): void { - /** @var Role Role Class. */ - $role = self::$roleMapper->fetchById($roleId); + /** @var RoleExtended Role Class. */ + $role = self::$roleExtendedMapper->fetchById($roleId); - $this->assertEquals($result, $role->isUserInRoleById($userId)); + $this->assertEquals($result, $role->hasUserById($userId)); } /** @@ -193,12 +178,12 @@ public function testIsUserInRoleById(int $roleId, int $userId, bool $result): vo */ public function testIsUserInRoleByName(int $roleId, int $userId, bool $result): void { - /** @var Role Role Class. */ - $role = self::$roleMapper->fetchById($roleId); + /** @var RoleExtended Role Class. */ + $role = self::$roleExtendedMapper->fetchById($roleId); /** @var UserExtended Enhanced User Class. */ - $user = self::$UserExtendedMapper->fetchById($userId); - $this->assertEquals($result, $role->isUserInRoleByName($user->name)); + $user = self::$userMapper->fetchById($userId); + $this->assertEquals($result, $role->hasUserByName($user->name)); } /** @@ -244,8 +229,8 @@ public static function rolePermissionProvider(): array */ public function testRoleCan(int $roleId, int $permissionId, bool $result): void { - /** @var Role Role Class. */ - $role = self::$roleMapper->fetchById($roleId); + /** @var RoleExtended Role Class. */ + $role = self::$roleExtendedMapper->fetchById($roleId); /** @var Permission Permission Class. */ $permission = self::$permissionMapper->fetchById($permissionId); @@ -265,8 +250,8 @@ public function testRoleCan(int $roleId, int $permissionId, bool $result): void */ public function testRoleCanById(int $roleId, int $permissionId, bool $result): void { - /** @var Role Role Class. */ - $role = self::$roleMapper->fetchById($roleId); + /** @var RoleExtended Role Class. */ + $role = self::$roleExtendedMapper->fetchById($roleId); $this->assertEquals($result, $role->canById($permissionId)); } @@ -284,8 +269,8 @@ public function testRoleCanById(int $roleId, int $permissionId, bool $result): v */ public function testRoleCanByName(int $roleId, int $permissionId, bool $result): void { - /** @var Role Role Class. */ - $role = self::$roleMapper->fetchById($roleId); + /** @var RoleExtended Role Class. */ + $role = self::$roleExtendedMapper->fetchById($roleId); /** @var Permission Permission Class. */ $permission = self::$permissionMapper->fetchById($permissionId); diff --git a/tests/Linna/Authorization/RoleTest.php b/tests/Linna/Authorization/RoleTest.php index 312513c..2e66b5a 100644 --- a/tests/Linna/Authorization/RoleTest.php +++ b/tests/Linna/Authorization/RoleTest.php @@ -64,9 +64,10 @@ public function testNewRoleInstance(): void * * @return void */ - /*public function testConstructorTypeCasting(): void + public function testConstructorTypeCasting(): void { - $role = self::$roleMapper->fetchById(1); + $role = self::$role; + $role->setId(1); $this->assertIsInt($role->getId()); $this->assertIsInt($role->id); @@ -74,187 +75,5 @@ public function testNewRoleInstance(): void $this->assertGreaterThan(0, $role->getId()); $this->assertGreaterThan(0, $role->id); - }*/ - - /** - * User Role data provider. - * - * @return array - */ - /*public static function userRoleProvider(): array - { - return [ - [1, 1, true], - [1, 2, false], - [1, 3, false], - [1, 4, false], - [1, 5, false], - [1, 6, false], - [1, 7, false], - [2, 1, false], - [2, 2, true], - [2, 3, true], - [2, 4, false], - [2, 5, false], - [2, 6, false], - [2, 7, false], - [3, 1, false], - [3, 2, false], - [3, 3, false], - [3, 4, true], - [3, 5, true], - [3, 6, true], - [3, 7, true], - ]; - } - - /** - * Test is user in role. - * - * @dataProvider userRoleProvider - * - * @param int $roleId - * @param int $userId - * @param bool $result - * - * @return void - */ - //public function testIsUserInRole(int $roleId, int $userId, bool $result): void - //{ - /** @var Role Role Class. */ - // $role = self::$roleMapper->fetchById($roleId); - - /** @var EnhancedUser Enhanced User Class. */ - // $user = self::$enhancedUserMapper->fetchById($userId); - // $this->assertEquals($result, $role->isUserInRole($user)); - //} - - /** - * Test is user in role by id. - * - * @dataProvider userRoleProvider - * - * @param int $roleId - * @param int $userId - * @param bool $result - * - * @return void - */ - /*public function testIsUserInRoleById(int $roleId, int $userId, bool $result): void - { - $role = self::$roleMapper->fetchById($roleId); - - $this->assertEquals($result, $role->isUserInRoleById($userId)); - } - - /** - * Test is user in role by name. - * - * @dataProvider userRoleProvider - * - * @param int $roleId - * @param int $userId - * @param bool $result - * - * @return void - */ - /*public function testIsUserInRoleByName(int $roleId, int $userId, bool $result): void - { - - $role = self::$roleMapper->fetchById($roleId); - - - $user = self::$enhancedUserMapper->fetchById($userId); - $this->assertEquals($result, $role->isUserInRoleByName($user->name)); - } - - /** - * Role Permission data provider. - * - * @return array - * @return void - */ - /*public static function rolePermissionProvider(): array - { - return [ - [1, 1, true], - [1, 2, true], - [1, 3, true], - [1, 4, true], - [1, 5, true], - [1, 6, true], - [2, 1, true], - [2, 2, true], - [2, 3, false], - [2, 4, false], - [2, 5, true], - [2, 6, true], - [3, 1, true], - [3, 2, false], - [3, 3, false], - [3, 4, false], - [3, 5, false], - [3, 6, false], - ]; - } - - /** - * Test role can. - * - * @dataProvider rolePermissionProvider - * - * @param int $roleId - * @param int $permissionId - * @param bool $result - * - * @return void - */ - /*public function testRoleCan(int $roleId, int $permissionId, bool $result): void - { - - $role = self::$roleMapper->fetchById($roleId); - - - $permission = self::$permissionMapper->fetchById($permissionId); - $this->assertEquals($result, $role->can($permission)); - } - - /** - * Test role can by id. - * - * @dataProvider rolePermissionProvider - * - * @param int $roleId - * @param int $permissionId - * @param bool $result - * - * @return void - */ - /*public function testRoleCanById(int $roleId, int $permissionId, bool $result): void - { - - $role = self::$roleMapper->fetchById($roleId); - - $this->assertEquals($result, $role->canById($permissionId)); } - - /** - * Test role can by name. - * - * @dataProvider rolePermissionProvider - * - * @param int $roleId - * @param int $permissionId - * @param bool $result - * - * @return void - */ - /*public function testRoleCanByName(int $roleId, int $permissionId, bool $result): void - { - - $role = self::$roleMapper->fetchById($roleId); - - $permission = self::$permissionMapper->fetchById($permissionId); - $this->assertEquals($result, $role->canByName($permission->name)); - }*/ } diff --git a/tests/Linna/Authorization/UserExtendedTest.php b/tests/Linna/Authorization/UserExtendedTest.php index 86b1b0e..ef3cedc 100644 --- a/tests/Linna/Authorization/UserExtendedTest.php +++ b/tests/Linna/Authorization/UserExtendedTest.php @@ -24,12 +24,12 @@ */ class UserExtendedTest extends TestCase { + /** @var UserExtendedMapper The enhanced user mapper */ + protected static UserExtendedMapper $userExtendedMapper; + /** @var PermissionMapper The permission mapper */ protected static PermissionMapper $permissionMapper; - /** @var UserExtendedMapper The enhanced user mapper */ - protected static UserExtendedMapper $UserExtendedMapper; - /** @var RoleMapper The role mapper */ protected static RoleMapper $roleMapper; @@ -60,14 +60,13 @@ public static function setUpBeforeClass(): void $password = new Password(); $permissionMapper = new PermissionMapper($pdo); - $userMapper = new UserMapper($pdo, $password); - $roleToUserMapper = new RoleToUserMapper($pdo, $password); - $UserExtendedMapper = new UserExtendedMapper($pdo, $password, $permissionMapper, $roleToUserMapper); + $roleMapper = new RoleMapper($pdo); + $userExtendedMapper = new UserExtendedMapper($pdo, $password, $permissionMapper, $roleMapper); self::$pdo = $pdo; - self::$roleMapper = new RoleMapper($pdo, $permissionMapper, $userMapper, $roleToUserMapper); + self::$roleMapper = $roleMapper; self::$permissionMapper = $permissionMapper; - self::$UserExtendedMapper = $UserExtendedMapper; + self::$userExtendedMapper = $userExtendedMapper; } /** @@ -90,7 +89,7 @@ public static function tearDownAfterClass(): void */ public function testNewUserExtendedInstance(): void { - $this->assertInstanceOf(UserExtended::class, self::$UserExtendedMapper->create()); + $this->assertInstanceOf(UserExtended::class, self::$userExtendedMapper->create()); } /** @@ -100,7 +99,7 @@ public function testNewUserExtendedInstance(): void */ public function testConstructorTypeCasting(): void { - $user = self::$UserExtendedMapper->fetchByName('root'); + $user = self::$userExtendedMapper->fetchByName('root'); $this->assertIsInt($user->getId()); $this->assertIsInt($user->id); @@ -146,8 +145,8 @@ public static function userPermissionProvider(): array */ public function testUserCan(int $userId, int $permissionId, bool $result): void { - /** @var UserExtendedMapper Enhanced user mapper Class. */ - $user = self::$UserExtendedMapper->fetchById($userId); + /** @var UserExtended UserExtended Class. */ + $user = self::$userExtendedMapper->fetchById($userId); /** @var Permission Permission Class. */ $permission = self::$permissionMapper->fetchById($permissionId); @@ -167,8 +166,8 @@ public function testUserCan(int $userId, int $permissionId, bool $result): void */ public function testUserCanById(int $userId, int $permissionId, bool $result): void { - /** @var UserExtendedMapper Enhanced user mapper Class. */ - $user = self::$UserExtendedMapper->fetchById($userId); + /** @var UserExtended UserExtended Class. */ + $user = self::$userExtendedMapper->fetchById($userId); $this->assertEquals($result, $user->canById($permissionId)); } @@ -186,8 +185,8 @@ public function testUserCanById(int $userId, int $permissionId, bool $result): v */ public function testUserCanByName(int $userId, int $permissionId, bool $result): void { - /** @var UserExtendedMapper Enhanced user mapper Class. */ - $user = self::$UserExtendedMapper->fetchById($userId); + /** @var UserExtended UserExtended Class. */ + $user = self::$userExtendedMapper->fetchById($userId); /** @var Permission Permission Class. */ $permission = self::$permissionMapper->fetchById($permissionId); @@ -239,7 +238,8 @@ public static function userRoleProvider(): array */ public function testUserHasRole(int $roleId, int $userId, bool $result): void { - $user = self::$UserExtendedMapper->fetchById($userId); + /** @var UserExtended UserExtended Class. */ + $user = self::$userExtendedMapper->fetchById($userId); $role = self::$roleMapper->fetchById($roleId); $this->assertEquals($result, $user->hasRole($role)); @@ -258,7 +258,8 @@ public function testUserHasRole(int $roleId, int $userId, bool $result): void */ public function testUserHasRoleById(int $roleId, int $userId, bool $result): void { - $user = self::$UserExtendedMapper->fetchById($userId); + /** @var UserExtended UserExtended Class. */ + $user = self::$userExtendedMapper->fetchById($userId); $this->assertEquals($result, $user->hasRoleById($roleId)); } @@ -276,7 +277,8 @@ public function testUserHasRoleById(int $roleId, int $userId, bool $result): voi */ public function testUserHasRoleByName(int $roleId, int $userId, bool $result): void { - $user = self::$UserExtendedMapper->fetchById($userId); + /** @var UserExtended UserExtended Class. */ + $user = self::$userExtendedMapper->fetchById($userId); $role = self::$roleMapper->fetchById($roleId); $this->assertEquals($result, $user->hasRoleByName($role->name));