Skip to content

Commit 81eb085

Browse files
committed
feat 补充单元测试
1 parent 13200b4 commit 81eb085

File tree

13 files changed

+134
-78
lines changed

13 files changed

+134
-78
lines changed

tests/Adapter.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,40 @@
66

77
trait Adapter
88
{
9+
public function testAddOtherPolicy()
10+
{
11+
// var_dump(config('plugin.casbin.webman-permission.permission'));
12+
$this->assertTrue(Permission::client('other')->addPolicy('writer', 'articles', 'edit'));
13+
$this->assertTrue(Permission::client('other')->addPolicies([
14+
['writer', 'articles', 'list'],
15+
['writer', 'articles', 'delete'],
16+
]));
17+
18+
$this->assertFalse(Permission::client('other')->addPolicies([
19+
['writer', 'articles', 'list'],
20+
['writer', 'articles', 'delete'],
21+
]));
22+
23+
$this->assertTrue(Permission::client('other')->enforce('writer', 'articles', 'edit'));
24+
$this->assertTrue(Permission::client('other')->enforce('writer', 'articles', 'delete'));
25+
$this->assertFalse(Permission::client('other')->enforce('writer', 'articles', 'other'));
26+
27+
$this->assertTrue(Permission::client('other')->hasPolicy('writer', 'articles', 'edit'));
28+
$this->assertFalse(Permission::client('other')->hasPolicy('writer', 'articles', 'other'));
29+
30+
$this->assertTrue(Permission::client('other')->removePolicy('writer', 'articles', 'edit'));
31+
$this->assertFalse(Permission::client('other')->hasPolicy('writer', 'articles', 'edit'));
32+
$this->assertFalse(Permission::client('other')->enforce('writer', 'articles', 'edit'));
33+
}
34+
35+
public function testAddOtherRoleForUser()
36+
{
37+
$this->assertFalse(Permission::client('other')->hasRoleForUser('eve', 'data2'));
38+
Permission::client('other')->addRoleForUser('eve', 'data2');
39+
$this->assertTrue(in_array('data2', Permission::client('other')->getAllRoles()));
40+
$this->assertTrue(Permission::client('other')->hasRoleForUser('eve', 'data2'));
41+
}
42+
943
public function testAddPermissionForUser()
1044
{
1145
$this->assertFalse(Permission::enforce('eve', 'data1', 'read'));
@@ -45,4 +79,11 @@ public function testAddRoleForUser()
4579
$this->assertTrue(in_array('data2', Permission::getAllRoles()));
4680
$this->assertTrue(Permission::hasRoleForUser('eve', 'data2'));
4781
}
82+
83+
public function testOtherAddPermissionForUser()
84+
{
85+
$this->assertFalse(Permission::client('other')->enforce('eve', 'data1', 'read'));
86+
Permission::client('other')->addPermissionForUser('eve', 'data1', 'read');
87+
$this->assertTrue(Permission::client('other')->enforce('eve', 'data1', 'read'));
88+
}
4889
}

tests/LaravelDatabase/TestCase.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Casbin\WebmanPermission\Tests\LaravelDatabase;
1111

12+
use Casbin\WebmanPermission\Permission;
1213
use PHPUnit\Framework\TestCase as BaseTestCase;
1314
use support\bootstrap\LaravelDb;
1415
use support\Db;
@@ -20,11 +21,15 @@ class TestCase extends BaseTestCase
2021
{
2122
protected function setUp(): void
2223
{
24+
Config::load(dirname(__DIR__).'/config');
2325
Config::load(__DIR__.'/config');
26+
2427
LaravelDb::start(null);
2528
Worker::$globalEvent = new Select();
2629

2730
$this->initDb();
31+
$this->initOtherDb();
32+
Permission::clear();
2833
}
2934

3035
public function initDb()
@@ -52,4 +57,30 @@ public function initDb()
5257
Db::statement('drop table if exists casbin_rule');
5358
Db::statement($sql);
5459
}
60+
61+
public function initOtherDb()
62+
{
63+
$sql = <<<EOF
64+
CREATE TABLE `other_casbin_rule` (
65+
`id` BIGINT ( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT,
66+
`ptype` VARCHAR ( 128 ) NOT NULL DEFAULT '',
67+
`v0` VARCHAR ( 128 ) NOT NULL DEFAULT '',
68+
`v1` VARCHAR ( 128 ) NOT NULL DEFAULT '',
69+
`v2` VARCHAR ( 128 ) NOT NULL DEFAULT '',
70+
`v3` VARCHAR ( 128 ) NOT NULL DEFAULT '',
71+
`v4` VARCHAR ( 128 ) NOT NULL DEFAULT '',
72+
`v5` VARCHAR ( 128 ) NOT NULL DEFAULT '',
73+
PRIMARY KEY ( `id` ) USING BTREE,
74+
KEY `idx_ptype` ( `ptype` ) USING BTREE,
75+
KEY `idx_v0` ( `v0` ) USING BTREE,
76+
KEY `idx_v1` ( `v1` ) USING BTREE,
77+
KEY `idx_v2` ( `v2` ) USING BTREE,
78+
KEY `idx_v3` ( `v3` ) USING BTREE,
79+
KEY `idx_v4` ( `v4` ) USING BTREE,
80+
KEY `idx_v5` ( `v5` ) USING BTREE
81+
) ENGINE = INNODB CHARSET = utf8mb4 COMMENT = '策略规则表';
82+
EOF;
83+
Db::statement('drop table if exists other_casbin_rule');
84+
Db::statement($sql);
85+
}
5586
}

tests/LaravelDatabase/config/plugin/casbin/webman-permission/permission.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,20 @@
1919
'rules_name' => null,
2020
],
2121
],
22-
// 其他扩展配置,只需要按照基础配置一样,复制一份,指定相关策略模型和适配器即可
22+
'other' => [
23+
// 策略模型Model设置
24+
'model' => [
25+
'config_type' => 'file',
26+
'config_file_path' => __DIR__.'/rbac-model.conf',
27+
'config_text' => '',
28+
],
29+
// 适配器
30+
'adapter' => Casbin\WebmanPermission\Adapter\LaravelDatabaseAdapter::class, // Laravel 适配器
31+
// 数据库设置
32+
'database' => [
33+
'connection' => '',
34+
'rules_table' => 'other_casbin_rule',
35+
'rules_name' => null,
36+
],
37+
],
2338
];

tests/ThinkphpDatabase/TestCase.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Casbin\WebmanPermission\Tests\ThinkphpDatabase;
1111

12+
use Casbin\WebmanPermission\Permission;
1213
use PHPUnit\Framework\TestCase as BaseTestCase;
1314
use think\facade\Db;
1415
use Webman\Config;
@@ -19,11 +20,14 @@ class TestCase extends BaseTestCase
1920
{
2021
protected function setUp(): void
2122
{
23+
Config::load(dirname(__DIR__).'/config');
2224
Config::load(__DIR__.'/config');
2325
Db::setConfig(config('thinkorm'));
2426
Worker::$globalEvent = new Select();
2527

2628
$this->initDb();
29+
$this->initOtherDb();
30+
Permission::clear();
2731
}
2832

2933
public function initDb()
@@ -51,4 +55,30 @@ public function initDb()
5155
Db::execute('drop table if exists casbin_rule');
5256
Db::execute($sql);
5357
}
58+
59+
public function initOtherDb()
60+
{
61+
$sql = <<<EOF
62+
CREATE TABLE `other_casbin_rule` (
63+
`id` BIGINT ( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT,
64+
`ptype` VARCHAR ( 128 ) NOT NULL DEFAULT '',
65+
`v0` VARCHAR ( 128 ) NOT NULL DEFAULT '',
66+
`v1` VARCHAR ( 128 ) NOT NULL DEFAULT '',
67+
`v2` VARCHAR ( 128 ) NOT NULL DEFAULT '',
68+
`v3` VARCHAR ( 128 ) NOT NULL DEFAULT '',
69+
`v4` VARCHAR ( 128 ) NOT NULL DEFAULT '',
70+
`v5` VARCHAR ( 128 ) NOT NULL DEFAULT '',
71+
PRIMARY KEY ( `id` ) USING BTREE,
72+
KEY `idx_ptype` ( `ptype` ) USING BTREE,
73+
KEY `idx_v0` ( `v0` ) USING BTREE,
74+
KEY `idx_v1` ( `v1` ) USING BTREE,
75+
KEY `idx_v2` ( `v2` ) USING BTREE,
76+
KEY `idx_v3` ( `v3` ) USING BTREE,
77+
KEY `idx_v4` ( `v4` ) USING BTREE,
78+
KEY `idx_v5` ( `v5` ) USING BTREE
79+
) ENGINE = INNODB CHARSET = utf8mb4 COMMENT = '策略规则表';
80+
EOF;
81+
Db::execute('drop table if exists other_casbin_rule');
82+
Db::execute($sql);
83+
}
5484
}

tests/ThinkphpDatabase/config/app.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

tests/ThinkphpDatabase/config/container.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/ThinkphpDatabase/config/plugin/casbin/webman-permission/permission.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,20 @@
1919
'rules_name' => null,
2020
],
2121
],
22-
// 其他扩展配置,只需要按照基础配置一样,复制一份,指定相关策略模型和适配器即可
22+
'other' => [
23+
// 策略模型Model设置
24+
'model' => [
25+
'config_type' => 'file',
26+
'config_file_path' => __DIR__.'/rbac-model.conf',
27+
'config_text' => '',
28+
],
29+
// 适配器
30+
'adapter' => Casbin\WebmanPermission\Adapter\DatabaseAdapter::class, // ThinkORM 适配器
31+
// 数据库设置
32+
'database' => [
33+
'connection' => '',
34+
'rules_table' => 'other_casbin_rule',
35+
'rules_name' => null,
36+
],
37+
],
2338
];

tests/ThinkphpDatabase/config/redis.php

Lines changed: 0 additions & 25 deletions
This file was deleted.
File renamed without changes.

0 commit comments

Comments
 (0)