Skip to content

Commit d3eff17

Browse files
houaiaiclaude
andcommitted
fix: 修复测试环境配置问题
- 创建简化的测试bootstrap文件 (bootstrap-test.php) - 创建SimplePermissionTest.php用于基础功能测试 - 修改phpunit.xml使用简化bootstrap - 添加run-simple-tests.php测试运行脚本 - 修复PermissionTest.php配置依赖问题 解决测试运行时的配置文件路径错误问题 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent e8c83eb commit d3eff17

File tree

5 files changed

+358
-1
lines changed

5 files changed

+358
-1
lines changed

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit backupGlobals="false"
33
backupStaticAttributes="false"
4-
bootstrap="tests/bootstrap.php"
4+
bootstrap="tests/bootstrap-test.php"
55
colors="true"
66
convertErrorsToExceptions="true"
77
convertNoticesToExceptions="true"

run-simple-tests.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
/**
5+
* 简化的测试运行脚本
6+
*/
7+
8+
echo "=== Webman Permission 测试运行器 ===\n\n";
9+
10+
// 检查PHPUnit是否可用
11+
if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
12+
echo "错误: 请先运行 composer install\n";
13+
exit(1);
14+
}
15+
16+
require_once __DIR__ . '/vendor/autoload.php';
17+
18+
// 设置测试环境
19+
putenv('APP_ENV=testing');
20+
21+
echo "正在运行测试...\n\n";
22+
23+
// 运行简单测试
24+
$command = 'php vendor/bin/phpunit tests/SimplePermissionTest.php --bootstrap tests/bootstrap-test.php --colors=always';
25+
26+
echo "命令: $command\n\n";
27+
echo "========================================\n";
28+
29+
passthru($command);
30+
31+
echo "\n========================================\n";
32+
echo "测试完成!\n";

tests/PermissionTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,72 @@ class PermissionTest extends TestCase
1313
{
1414
protected function setUp(): void
1515
{
16+
// 模拟配置
17+
global $config;
18+
$config = [
19+
'plugin' => [
20+
'casbin' => [
21+
'webman-permission' => [
22+
'permission' => [
23+
'default' => 'default',
24+
'default' => [
25+
'model' => [
26+
'config_type' => 'text',
27+
'config_text' => '
28+
[request_definition]
29+
r = sub, obj, act
30+
31+
[policy_definition]
32+
p = sub, obj, act
33+
34+
[role_definition]
35+
g = _, _
36+
37+
[policy_effect]
38+
e = some(where (p.eft == allow))
39+
40+
[matchers]
41+
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
42+
',
43+
],
44+
'adapter' => \Casbin\WebmanPermission\Adapter\DatabaseAdapter::class,
45+
],
46+
'other' => [
47+
'model' => [
48+
'config_type' => 'text',
49+
'config_text' => '
50+
[request_definition]
51+
r = sub, obj, act
52+
53+
[policy_definition]
54+
p = sub, obj, act
55+
56+
[role_definition]
57+
g = _, _
58+
59+
[policy_effect]
60+
e = some(where (p.eft == allow))
61+
62+
[matchers]
63+
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
64+
',
65+
],
66+
'adapter' => \Casbin\WebmanPermission\Adapter\DatabaseAdapter::class,
67+
'adapter_config' => [
68+
'table' => 'other_casbin_rule'
69+
],
70+
],
71+
'log' => [
72+
'enabled' => false,
73+
'logger' => 'casbin',
74+
'path' => '/tmp/casbin.log',
75+
],
76+
]
77+
]
78+
]
79+
]
80+
];
81+
1682
Permission::clear();
1783
}
1884

tests/SimplePermissionTest.php

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?php
2+
3+
namespace Casbin\WebmanPermission\Tests;
4+
5+
use Casbin\WebmanPermission\Permission;
6+
use Casbin\WebmanPermission\Adapter\DatabaseAdapter;
7+
use Casbin\WebmanPermission\Adapter\LaravelDatabaseAdapter;
8+
use Casbin\Exceptions\CasbinException;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class SimplePermissionTest extends TestCase
12+
{
13+
protected function setUp(): void
14+
{
15+
// 模拟配置
16+
global $config;
17+
$config = [
18+
'plugin' => [
19+
'casbin' => [
20+
'webman-permission' => [
21+
'permission' => [
22+
'default' => 'default',
23+
'default' => [
24+
'model' => [
25+
'config_type' => 'text',
26+
'config_text' => '
27+
[request_definition]
28+
r = sub, obj, act
29+
30+
[policy_definition]
31+
p = sub, obj, act
32+
33+
[role_definition]
34+
g = _, _
35+
36+
[policy_effect]
37+
e = some(where (p.eft == allow))
38+
39+
[matchers]
40+
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
41+
',
42+
],
43+
'adapter' => \Casbin\WebmanPermission\Adapter\DatabaseAdapter::class,
44+
],
45+
'other' => [
46+
'model' => [
47+
'config_type' => 'text',
48+
'config_text' => '
49+
[request_definition]
50+
r = sub, obj, act
51+
52+
[policy_definition]
53+
p = sub, obj, act
54+
55+
[role_definition]
56+
g = _, _
57+
58+
[policy_effect]
59+
e = some(where (p.eft == allow))
60+
61+
[matchers]
62+
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
63+
',
64+
],
65+
'adapter' => \Casbin\WebmanPermission\Adapter\DatabaseAdapter::class,
66+
'adapter_config' => [
67+
'table' => 'other_casbin_rule'
68+
],
69+
],
70+
'log' => [
71+
'enabled' => false,
72+
'logger' => 'casbin',
73+
'path' => '/tmp/casbin.log',
74+
],
75+
]
76+
]
77+
]
78+
]
79+
];
80+
81+
Permission::clear();
82+
}
83+
84+
public function testBasicPermission()
85+
{
86+
$result = Permission::addPolicy('writer', 'articles', 'edit');
87+
$this->assertTrue($result);
88+
89+
$result = Permission::enforce('writer', 'articles', 'edit');
90+
$this->assertTrue($result);
91+
92+
$result = Permission::enforce('writer', 'articles', 'delete');
93+
$this->assertFalse($result);
94+
}
95+
96+
public function testRoleManagement()
97+
{
98+
$result = Permission::addRoleForUser('alice', 'admin');
99+
$this->assertTrue($result);
100+
101+
$result = Permission::hasRoleForUser('alice', 'admin');
102+
$this->assertTrue($result);
103+
104+
$roles = Permission::getRolesForUser('alice');
105+
$this->assertContains('admin', $roles);
106+
}
107+
108+
public function testPermissionForUser()
109+
{
110+
$result = Permission::addPermissionForUser('alice', 'data1', 'read');
111+
$this->assertTrue($result);
112+
113+
$result = Permission::enforce('alice', 'data1', 'read');
114+
$this->assertTrue($result);
115+
116+
$permissions = Permission::getPermissionsForUser('alice');
117+
$this->assertContains(['alice', 'data1', 'read'], $permissions);
118+
}
119+
120+
public function testBatchOperations()
121+
{
122+
$policies = [
123+
['alice', 'data1', 'read'],
124+
['bob', 'data2', 'write']
125+
];
126+
127+
$result = Permission::addPolicies($policies);
128+
$this->assertTrue($result);
129+
130+
$this->assertTrue(Permission::enforce('alice', 'data1', 'read'));
131+
$this->assertTrue(Permission::enforce('bob', 'data2', 'write'));
132+
}
133+
134+
public function testPolicyUpdate()
135+
{
136+
Permission::addPolicy('writer', 'articles', 'edit');
137+
$this->assertTrue(Permission::hasPolicy('writer', 'articles', 'edit'));
138+
139+
$result = Permission::updatePolicies(
140+
[['writer', 'articles', 'edit']],
141+
[['writer', 'articles', 'update']]
142+
);
143+
$this->assertTrue($result);
144+
145+
$this->assertFalse(Permission::hasPolicy('writer', 'articles', 'edit'));
146+
$this->assertTrue(Permission::hasPolicy('writer', 'articles', 'update'));
147+
}
148+
149+
public function testRemoveOperations()
150+
{
151+
Permission::addPolicy('writer', 'articles', 'edit');
152+
$this->assertTrue(Permission::hasPolicy('writer', 'articles', 'edit'));
153+
154+
$result = Permission::removePolicy('writer', 'articles', 'edit');
155+
$this->assertTrue($result);
156+
$this->assertFalse(Permission::hasPolicy('writer', 'articles', 'edit'));
157+
}
158+
159+
public function testDriverManagement()
160+
{
161+
$driver = Permission::getDefaultDriver();
162+
$this->assertNotEmpty($driver);
163+
164+
$drivers = Permission::getAllDriver();
165+
$this->assertIsArray($drivers);
166+
}
167+
168+
public function testClear()
169+
{
170+
Permission::addPolicy('writer', 'articles', 'edit');
171+
$this->assertTrue(Permission::hasPolicy('writer', 'articles', 'edit'));
172+
173+
Permission::clear();
174+
$this->assertFalse(Permission::hasPolicy('writer', 'articles', 'edit'));
175+
}
176+
177+
protected function tearDown(): void
178+
{
179+
Permission::clear();
180+
}
181+
}

tests/bootstrap-test.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* 测试环境bootstrap文件
4+
*/
5+
6+
// 设置基础路径
7+
define('BASE_PATH', dirname(__DIR__));
8+
9+
// 自动加载
10+
require_once BASE_PATH . '/vendor/autoload.php';
11+
12+
// 设置测试环境
13+
putenv('APP_ENV=testing');
14+
15+
// 加载测试配置
16+
$testConfigPath = __DIR__ . '/config';
17+
if (is_dir($testConfigPath)) {
18+
$config = [];
19+
$files = scandir($testConfigPath);
20+
foreach ($files as $file) {
21+
if (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
22+
$config[pathinfo($file, PATHINFO_FILENAME)] = require $testConfigPath . '/' . $file;
23+
}
24+
}
25+
26+
// 设置配置到全局
27+
if (!function_exists('config')) {
28+
function config($key, $default = null) {
29+
global $config;
30+
$keys = explode('.', $key);
31+
$value = $config;
32+
33+
foreach ($keys as $k) {
34+
if (isset($value[$k])) {
35+
$value = $value[$k];
36+
} else {
37+
return $default;
38+
}
39+
}
40+
41+
return $value;
42+
}
43+
}
44+
}
45+
46+
// 设置基础函数
47+
if (!function_exists('base_path')) {
48+
function base_path($path = '') {
49+
return BASE_PATH . ($path ? '/' . $path : '');
50+
}
51+
}
52+
53+
if (!function_exists('config_path')) {
54+
function config_path($path = '') {
55+
return __DIR__ . '/config' . ($path ? '/' . $path : '');
56+
}
57+
}
58+
59+
if (!function_exists('runtime_path')) {
60+
function runtime_path($path = '') {
61+
return __DIR__ . '/runtime' . ($path ? '/' . $path : '');
62+
}
63+
}
64+
65+
// 创建运行时目录
66+
if (!is_dir(__DIR__ . '/runtime')) {
67+
mkdir(__DIR__ . '/runtime', 0755, true);
68+
}
69+
70+
// 设置错误处理
71+
set_error_handler(function ($level, $message, $file = '', $line = 0) {
72+
if (error_reporting() & $level) {
73+
throw new ErrorException($message, 0, $level, $file, $line);
74+
}
75+
});
76+
77+
// 设置时区
78+
date_default_timezone_set('Asia/Shanghai');

0 commit comments

Comments
 (0)