Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat. add unit test
  • Loading branch information
lyt8384 committed Feb 24, 2023
commit 13200b4eaefa21c5582c36d3dbbbe5c270eb82e3
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ vendor
.idea
.vscode
.phpunit*
composer.lock
composer.lock
.DS_Store
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"casbin/casbin": "^3.20",
"topthink/think-orm": "^2.0",
"php-di/php-di": "^6.3",
"doctrine/annotations": "^1.13",
"workerman/redis": "^1.0"
},
"autoload": {
Expand All @@ -41,7 +42,12 @@
"phpunit/phpunit": "~7.0|~8.0|~9.0",
"php-coveralls/php-coveralls": "^2.1",
"workerman/webman": "^1.0",
"vlucas/phpdotenv": "^5.5",
"psr/container": "^1.1.1",
"illuminate/database": "^8.83",
"illuminate/cache": "^8.83"
"illuminate/pagination": "^8.83",
"illuminate/events": "^8.83",
"symfony/var-dumper": "^6.2",
"webman/think-orm": "^1.0"
}
}
48 changes: 48 additions & 0 deletions tests/Adapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Casbin\WebmanPermission\Tests;

use Casbin\WebmanPermission\Permission;

trait Adapter
{
public function testAddPermissionForUser()
{
$this->assertFalse(Permission::enforce('eve', 'data1', 'read'));
Permission::addPermissionForUser('eve', 'data1', 'read');
$this->assertTrue(Permission::enforce('eve', 'data1', 'read'));
}

public function testAddPolicy()
{
$this->assertTrue(Permission::addPolicy('writer', 'articles', 'edit'));
$this->assertTrue(Permission::addPolicies([
['writer', 'articles', 'list'],
['writer', 'articles', 'delete'],
]));

$this->assertFalse(Permission::addPolicies([
['writer', 'articles', 'list'],
['writer', 'articles', 'delete'],
]));

$this->assertTrue(Permission::enforce('writer', 'articles', 'edit'));
$this->assertTrue(Permission::enforce('writer', 'articles', 'delete'));
$this->assertFalse(Permission::enforce('writer', 'articles', 'other'));

$this->assertTrue(Permission::hasPolicy('writer', 'articles', 'edit'));
$this->assertFalse(Permission::hasPolicy('writer', 'articles', 'other'));

$this->assertTrue(Permission::removePolicy('writer', 'articles', 'edit'));
$this->assertFalse(Permission::hasPolicy('writer', 'articles', 'edit'));
$this->assertFalse(Permission::enforce('writer', 'articles', 'edit'));
}

public function testAddRoleForUser()
{
$this->assertFalse(Permission::hasRoleForUser('eve', 'data2'));
Permission::addRoleForUser('eve', 'data2');
$this->assertTrue(in_array('data2', Permission::getAllRoles()));
$this->assertTrue(Permission::hasRoleForUser('eve', 'data2'));
}
}
10 changes: 10 additions & 0 deletions tests/LaravelDatabase/LaravelDatabaseAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Casbin\WebmanPermission\Tests\LaravelDatabase;

use Casbin\WebmanPermission\Tests\Adapter;

class LaravelDatabaseAdapterTest extends TestCase
{
use Adapter;
}
55 changes: 55 additions & 0 deletions tests/LaravelDatabase/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* @desc TestCase.php
*
* @author Tinywan(ShaoBo Wan)
*
* @date 2022/1/13 11:07
*/

namespace Casbin\WebmanPermission\Tests\LaravelDatabase;

use PHPUnit\Framework\TestCase as BaseTestCase;
use support\bootstrap\LaravelDb;
use support\Db;
use Webman\Config;
use Workerman\Events\Select;
use Workerman\Worker;

class TestCase extends BaseTestCase
{
protected function setUp(): void
{
Config::load(__DIR__.'/config');
LaravelDb::start(null);
Worker::$globalEvent = new Select();

$this->initDb();
}

public function initDb()
{
$sql = <<<EOF
CREATE TABLE `casbin_rule` (
`id` BIGINT ( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT,
`ptype` VARCHAR ( 128 ) NOT NULL DEFAULT '',
`v0` VARCHAR ( 128 ) NOT NULL DEFAULT '',
`v1` VARCHAR ( 128 ) NOT NULL DEFAULT '',
`v2` VARCHAR ( 128 ) NOT NULL DEFAULT '',
`v3` VARCHAR ( 128 ) NOT NULL DEFAULT '',
`v4` VARCHAR ( 128 ) NOT NULL DEFAULT '',
`v5` VARCHAR ( 128 ) NOT NULL DEFAULT '',
PRIMARY KEY ( `id` ) USING BTREE,
KEY `idx_ptype` ( `ptype` ) USING BTREE,
KEY `idx_v0` ( `v0` ) USING BTREE,
KEY `idx_v1` ( `v1` ) USING BTREE,
KEY `idx_v2` ( `v2` ) USING BTREE,
KEY `idx_v3` ( `v3` ) USING BTREE,
KEY `idx_v4` ( `v4` ) USING BTREE,
KEY `idx_v5` ( `v5` ) USING BTREE
) ENGINE = INNODB CHARSET = utf8mb4 COMMENT = '策略规则表';
EOF;
Db::statement('drop table if exists casbin_rule');
Db::statement($sql);
}
}
29 changes: 29 additions & 0 deletions tests/LaravelDatabase/config/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<[email protected]>
*
* @copyright walkor<[email protected]>
*
* @see http://www.workerman.net/
*
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/

use support\Request;

return [
'debug' => true,
'error_reporting' => E_ALL,
'default_timezone' => 'Asia/Shanghai',
'request_class' => Request::class,
'public_path' => base_path().DIRECTORY_SEPARATOR.'public',
'runtime_path' => base_path(false).DIRECTORY_SEPARATOR.'runtime',
'controller_suffix' => 'Controller',
'controller_reuse' => false,
];
22 changes: 22 additions & 0 deletions tests/LaravelDatabase/config/container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<[email protected]>
*
* @copyright walkor<[email protected]>
*
* @see http://www.workerman.net/
*
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
$builder = new \DI\ContainerBuilder();
$builder->addDefinitions(config('dependence', []));
$builder->useAutowiring(true);
$builder->useAnnotations(true);

return $builder->build();
39 changes: 39 additions & 0 deletions tests/LaravelDatabase/config/database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<[email protected]>
*
* @copyright walkor<[email protected]>
*
* @see http://www.workerman.net/
*
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/

return [
// 默认数据库
'default' => 'mysql',

// 各种数据库配置
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', 3306),
'database' => env('DB_NAME', 'test'),
'username' => env('DB_USER', 'root'),
'password' => env('DB_PASSWORD', 'test'),
'unix_socket' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
return [
'enable' => true,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

return [
'default' => 'basic',
// 基础配置
'basic' => [
// 策略模型Model设置
'model' => [
'config_type' => 'file',
'config_file_path' => __DIR__.'/rbac-model.conf',
'config_text' => '',
],
// 适配器
'adapter' => Casbin\WebmanPermission\Adapter\LaravelDatabaseAdapter::class, // Laravel 适配器
// 数据库设置
'database' => [
'connection' => '',
'rules_table' => 'casbin_rule',
'rules_name' => null,
],
],
// 其他扩展配置,只需要按照基础配置一样,复制一份,指定相关策略模型和适配器即可
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[role_definition]
g = _, _

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
25 changes: 25 additions & 0 deletions tests/LaravelDatabase/config/redis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<[email protected]>
*
* @copyright walkor<[email protected]>
*
* @see http://www.workerman.net/
*
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/

return [
'default' => [
'host' => env('REDIS_HOST') ?: '127.0.0.1',
'password' => env('REDIS_PASSWORD') ?: null,
'port' => env('REDIS_PORT') ?: 6379,
'database' => env('REDIS_DB') ?: 0,
],
];
36 changes: 0 additions & 36 deletions tests/TestCase.php

This file was deleted.

10 changes: 10 additions & 0 deletions tests/ThinkphpDatabase/DatabaseAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Casbin\WebmanPermission\Tests\ThinkphpDatabase;

use Casbin\WebmanPermission\Tests\Adapter;

class DatabaseAdapterTest extends TestCase
{
use Adapter;
}
Loading