Skip to content

Commit

Permalink
testa updated
Browse files Browse the repository at this point in the history
  • Loading branch information
s3b4stian committed Dec 11, 2023
1 parent 6f0137b commit afc112c
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 311 deletions.
41 changes: 19 additions & 22 deletions tests/Linna/Authorization/PermissionExtendedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Linna\Authorization;

use Linna\Authentication\Password;
use Linna\Storage\ExtendedPDO;
use Linna\Storage\StorageFactory;
use PHPUnit\Framework\TestCase;
Expand All @@ -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.
Expand All @@ -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;
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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);
}
}
68 changes: 27 additions & 41 deletions tests/Linna/Authorization/PermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}

/**
Expand All @@ -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);
}
}
75 changes: 30 additions & 45 deletions tests/Linna/Authorization/RoleExtendedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,24 @@
namespace Linna\Authorization;

use Linna\Authentication\Password;
use Linna\Authentication\UserMapper;
use Linna\Storage\ExtendedPDO;
use Linna\Storage\StorageFactory;
use PHPUnit\Framework\TestCase;
use Linna\TestHelper\Pdo\PdoOptionsFactory;

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.
Expand All @@ -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;
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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);
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand Down Expand Up @@ -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);
Expand All @@ -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));
}
Expand All @@ -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);
Expand Down
Loading

0 comments on commit afc112c

Please sign in to comment.