Skip to content

Commit 13200b4

Browse files
committed
feat. add unit test
1 parent 109354d commit 13200b4

File tree

22 files changed

+490
-38
lines changed

22 files changed

+490
-38
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ vendor
33
.idea
44
.vscode
55
.phpunit*
6-
composer.lock
6+
composer.lock
7+
.DS_Store

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"casbin/casbin": "^3.20",
2626
"topthink/think-orm": "^2.0",
2727
"php-di/php-di": "^6.3",
28+
"doctrine/annotations": "^1.13",
2829
"workerman/redis": "^1.0"
2930
},
3031
"autoload": {
@@ -41,7 +42,12 @@
4142
"phpunit/phpunit": "~7.0|~8.0|~9.0",
4243
"php-coveralls/php-coveralls": "^2.1",
4344
"workerman/webman": "^1.0",
45+
"vlucas/phpdotenv": "^5.5",
46+
"psr/container": "^1.1.1",
4447
"illuminate/database": "^8.83",
45-
"illuminate/cache": "^8.83"
48+
"illuminate/pagination": "^8.83",
49+
"illuminate/events": "^8.83",
50+
"symfony/var-dumper": "^6.2",
51+
"webman/think-orm": "^1.0"
4652
}
4753
}

tests/Adapter.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Casbin\WebmanPermission\Tests;
4+
5+
use Casbin\WebmanPermission\Permission;
6+
7+
trait Adapter
8+
{
9+
public function testAddPermissionForUser()
10+
{
11+
$this->assertFalse(Permission::enforce('eve', 'data1', 'read'));
12+
Permission::addPermissionForUser('eve', 'data1', 'read');
13+
$this->assertTrue(Permission::enforce('eve', 'data1', 'read'));
14+
}
15+
16+
public function testAddPolicy()
17+
{
18+
$this->assertTrue(Permission::addPolicy('writer', 'articles', 'edit'));
19+
$this->assertTrue(Permission::addPolicies([
20+
['writer', 'articles', 'list'],
21+
['writer', 'articles', 'delete'],
22+
]));
23+
24+
$this->assertFalse(Permission::addPolicies([
25+
['writer', 'articles', 'list'],
26+
['writer', 'articles', 'delete'],
27+
]));
28+
29+
$this->assertTrue(Permission::enforce('writer', 'articles', 'edit'));
30+
$this->assertTrue(Permission::enforce('writer', 'articles', 'delete'));
31+
$this->assertFalse(Permission::enforce('writer', 'articles', 'other'));
32+
33+
$this->assertTrue(Permission::hasPolicy('writer', 'articles', 'edit'));
34+
$this->assertFalse(Permission::hasPolicy('writer', 'articles', 'other'));
35+
36+
$this->assertTrue(Permission::removePolicy('writer', 'articles', 'edit'));
37+
$this->assertFalse(Permission::hasPolicy('writer', 'articles', 'edit'));
38+
$this->assertFalse(Permission::enforce('writer', 'articles', 'edit'));
39+
}
40+
41+
public function testAddRoleForUser()
42+
{
43+
$this->assertFalse(Permission::hasRoleForUser('eve', 'data2'));
44+
Permission::addRoleForUser('eve', 'data2');
45+
$this->assertTrue(in_array('data2', Permission::getAllRoles()));
46+
$this->assertTrue(Permission::hasRoleForUser('eve', 'data2'));
47+
}
48+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Casbin\WebmanPermission\Tests\LaravelDatabase;
4+
5+
use Casbin\WebmanPermission\Tests\Adapter;
6+
7+
class LaravelDatabaseAdapterTest extends TestCase
8+
{
9+
use Adapter;
10+
}

tests/LaravelDatabase/TestCase.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* @desc TestCase.php
4+
*
5+
* @author Tinywan(ShaoBo Wan)
6+
*
7+
* @date 2022/1/13 11:07
8+
*/
9+
10+
namespace Casbin\WebmanPermission\Tests\LaravelDatabase;
11+
12+
use PHPUnit\Framework\TestCase as BaseTestCase;
13+
use support\bootstrap\LaravelDb;
14+
use support\Db;
15+
use Webman\Config;
16+
use Workerman\Events\Select;
17+
use Workerman\Worker;
18+
19+
class TestCase extends BaseTestCase
20+
{
21+
protected function setUp(): void
22+
{
23+
Config::load(__DIR__.'/config');
24+
LaravelDb::start(null);
25+
Worker::$globalEvent = new Select();
26+
27+
$this->initDb();
28+
}
29+
30+
public function initDb()
31+
{
32+
$sql = <<<EOF
33+
CREATE TABLE `casbin_rule` (
34+
`id` BIGINT ( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT,
35+
`ptype` VARCHAR ( 128 ) NOT NULL DEFAULT '',
36+
`v0` VARCHAR ( 128 ) NOT NULL DEFAULT '',
37+
`v1` VARCHAR ( 128 ) NOT NULL DEFAULT '',
38+
`v2` VARCHAR ( 128 ) NOT NULL DEFAULT '',
39+
`v3` VARCHAR ( 128 ) NOT NULL DEFAULT '',
40+
`v4` VARCHAR ( 128 ) NOT NULL DEFAULT '',
41+
`v5` VARCHAR ( 128 ) NOT NULL DEFAULT '',
42+
PRIMARY KEY ( `id` ) USING BTREE,
43+
KEY `idx_ptype` ( `ptype` ) USING BTREE,
44+
KEY `idx_v0` ( `v0` ) USING BTREE,
45+
KEY `idx_v1` ( `v1` ) USING BTREE,
46+
KEY `idx_v2` ( `v2` ) USING BTREE,
47+
KEY `idx_v3` ( `v3` ) USING BTREE,
48+
KEY `idx_v4` ( `v4` ) USING BTREE,
49+
KEY `idx_v5` ( `v5` ) USING BTREE
50+
) ENGINE = INNODB CHARSET = utf8mb4 COMMENT = '策略规则表';
51+
EOF;
52+
Db::statement('drop table if exists casbin_rule');
53+
Db::statement($sql);
54+
}
55+
}

tests/LaravelDatabase/config/app.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* This file is part of webman.
4+
*
5+
* Licensed under The MIT License
6+
* For full copyright and license information, please see the MIT-LICENSE.txt
7+
* Redistributions of files must retain the above copyright notice.
8+
*
9+
* @author walkor<[email protected]>
10+
*
11+
* @copyright walkor<[email protected]>
12+
*
13+
* @see http://www.workerman.net/
14+
*
15+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
16+
*/
17+
18+
use support\Request;
19+
20+
return [
21+
'debug' => true,
22+
'error_reporting' => E_ALL,
23+
'default_timezone' => 'Asia/Shanghai',
24+
'request_class' => Request::class,
25+
'public_path' => base_path().DIRECTORY_SEPARATOR.'public',
26+
'runtime_path' => base_path(false).DIRECTORY_SEPARATOR.'runtime',
27+
'controller_suffix' => 'Controller',
28+
'controller_reuse' => false,
29+
];
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* This file is part of webman.
4+
*
5+
* Licensed under The MIT License
6+
* For full copyright and license information, please see the MIT-LICENSE.txt
7+
* Redistributions of files must retain the above copyright notice.
8+
*
9+
* @author walkor<[email protected]>
10+
*
11+
* @copyright walkor<[email protected]>
12+
*
13+
* @see http://www.workerman.net/
14+
*
15+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
16+
*/
17+
$builder = new \DI\ContainerBuilder();
18+
$builder->addDefinitions(config('dependence', []));
19+
$builder->useAutowiring(true);
20+
$builder->useAnnotations(true);
21+
22+
return $builder->build();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* This file is part of webman.
4+
*
5+
* Licensed under The MIT License
6+
* For full copyright and license information, please see the MIT-LICENSE.txt
7+
* Redistributions of files must retain the above copyright notice.
8+
*
9+
* @author walkor<[email protected]>
10+
*
11+
* @copyright walkor<[email protected]>
12+
*
13+
* @see http://www.workerman.net/
14+
*
15+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
16+
*/
17+
18+
return [
19+
// 默认数据库
20+
'default' => 'mysql',
21+
22+
// 各种数据库配置
23+
'connections' => [
24+
'mysql' => [
25+
'driver' => 'mysql',
26+
'host' => env('DB_HOST', '127.0.0.1'),
27+
'port' => env('DB_PORT', 3306),
28+
'database' => env('DB_NAME', 'test'),
29+
'username' => env('DB_USER', 'root'),
30+
'password' => env('DB_PASSWORD', 'test'),
31+
'unix_socket' => '',
32+
'charset' => 'utf8',
33+
'collation' => 'utf8_unicode_ci',
34+
'prefix' => '',
35+
'strict' => true,
36+
'engine' => null,
37+
],
38+
],
39+
];
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
return [
3+
'enable' => true,
4+
];
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
return [
4+
'default' => 'basic',
5+
// 基础配置
6+
'basic' => [
7+
// 策略模型Model设置
8+
'model' => [
9+
'config_type' => 'file',
10+
'config_file_path' => __DIR__.'/rbac-model.conf',
11+
'config_text' => '',
12+
],
13+
// 适配器
14+
'adapter' => Casbin\WebmanPermission\Adapter\LaravelDatabaseAdapter::class, // Laravel 适配器
15+
// 数据库设置
16+
'database' => [
17+
'connection' => '',
18+
'rules_table' => 'casbin_rule',
19+
'rules_name' => null,
20+
],
21+
],
22+
// 其他扩展配置,只需要按照基础配置一样,复制一份,指定相关策略模型和适配器即可
23+
];

0 commit comments

Comments
 (0)