Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

Commit 1db67c2

Browse files
committed
Added unit testing and fixes to upgrade casbin to 1.0 generated bugs.
1 parent 2bc7c63 commit 1db67c2

File tree

14 files changed

+273
-37
lines changed

14 files changed

+273
-37
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ composer.lock
66
*.iml
77

88
# coverage report
9-
/build
9+
/build
10+
11+
log

README.md

100644100755
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Think-Casbin
22
====
33

4+
[![Build Status](https://travis-ci.org/php-casbin/think-casbin.svg?branch=master)](https://travis-ci.org/php-casbin/think-casbin)
5+
[![Coverage Status](https://coveralls.io/repos/github/php-casbin/think-casbin/badge.svg)](https://coveralls.io/github/php-casbin/think-casbin)
46
[![Latest Stable Version](https://poser.pugx.org/casbin/think-adapter/v/stable)](https://packagist.org/packages/casbin/think-adapter)
57
[![Total Downloads](https://poser.pugx.org/casbin/think-adapter/downloads)](https://packagist.org/packages/casbin/think-adapter)
68
[![License](https://poser.pugx.org/casbin/think-adapter/license)](https://packagist.org/packages/casbin/think-adapter)

autoload.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
require_once __DIR__.'/vendor/autoload.php';
4+
5+
interface LoggerInterface
6+
{
7+
}
8+
class_alias(\think\facade\Env::class, 'Env');
9+
class_alias(\think\facade\Route::class, 'Route');
10+
class_alias(LoggerInterface::class, 'think\LoggerInterface');

composer.json

100644100755
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,27 @@
1010
],
1111
"license": "Apache-2.0",
1212
"require": {
13+
"topthink/think": "5.1.*",
1314
"topthink/think-migration": "^2.0",
1415
"casbin/casbin": "1.*"
1516
},
17+
"require-dev": {
18+
"phpunit/phpunit": "~5.7|~6.0|~7.0",
19+
"php-coveralls/php-coveralls": "^2.1"
20+
},
1621
"autoload": {
1722
"psr-4": {
1823
"CasbinAdapter\\Think\\": "src/"
1924
},
2025
"files": [
2126
"helper.php"
2227
]
28+
},
29+
"autoload-dev": {
30+
"psr-4": {
31+
"CasbinAdapter\\Think\\Tests\\": "tests/",
32+
"think\\": "vendor/topthink/framework/library/think/",
33+
"traits\\": "vendor/topthink/framework/library/traits/"
34+
}
2335
}
24-
2536
}

config/casbin-basic-model.conf

100644100755
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ r = sub, obj, act
44
[policy_definition]
55
p = sub, obj, act
66

7+
[role_definition]
8+
g = _, _
9+
710
[policy_effect]
811
e = some(where (p.eft == allow))
912

1013
[matchers]
11-
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
14+
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act

database/migrations/20181113071924_casbin_rule.php

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ public function up()
4141
public function down()
4242
{
4343
$table = $this->table(config('casbin.database.casbin_rules_name'));
44-
$table->delete();
44+
$table->drop();
4545
}
4646
}

helper.php

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
\think\Console::addDefaultCommands([
55
'casbin:publish' => \CasbinAdapter\Think\Commands\Publish::class,
66
'casbin:migrate' => \CasbinAdapter\Think\Commands\Migrate::class,
7+
'casbin:rollback' => \CasbinAdapter\Think\Commands\Rollback::class,
78
]);
89
}
910

phpunit.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<testsuites>
12+
<testsuite name="Application Test Suite">
13+
<directory>./tests/</directory>
14+
</testsuite>
15+
</testsuites>
16+
<filter>
17+
<whitelist processUncoveredFilesFromWhitelist="true">
18+
<directory suffix=".php">./src</directory>
19+
</whitelist>
20+
</filter>
21+
<logging>
22+
<log type="coverage-clover" target="build/logs/clover.xml"/>
23+
<log type="coverage-html" target="build/html"/>
24+
</logging>
25+
<php>
26+
<env name="DATABASE_DATABASE" value="casbin"/>
27+
</php>
28+
</phpunit>

src/Adapter.php

100644100755
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public function loadPolicy($model)
3636
$rows = $this->casbinRule->select()->toArray();
3737

3838
foreach ($rows as $row) {
39-
$line = implode(', ', array_slice(array_values($row), 1));
39+
$line = implode(', ', array_filter(array_slice($row, 1), function ($val) {
40+
return '' != $val && !is_null($val);
41+
}));
4042
$this->loadPolicyLine(trim($line), $model);
4143
}
4244
}

src/Commands/Migrate.php

100644100755
Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,14 @@
33
namespace CasbinAdapter\Think\Commands;
44

55
use think\migration\command\migrate\Run as MigrateRun;
6-
use Db;
76

87
class Migrate extends MigrateRun
98
{
9+
use MigrateTrait;
10+
1011
protected function configure()
1112
{
1213
parent::configure();
1314
$this->setName('casbin:migrate')->setDescription('Migrate the database for Casbin');
1415
}
15-
16-
protected function getPath()
17-
{
18-
return __DIR__.'/../../database/migrations';
19-
}
20-
21-
protected function getDbConfig()
22-
{
23-
$connection = config('casbin.database.connection') ?: config('database.default');
24-
25-
if ($connection) {
26-
$config = Db::connect()->getConfig();
27-
$config = $config[$connection];
28-
$dbConfig = [
29-
'adapter' => $config['type'],
30-
'host' => $config['hostname'],
31-
'name' => $config['database'],
32-
'user' => $config['username'],
33-
'pass' => $config['password'],
34-
'port' => $config['hostport'],
35-
'charset' => $config['charset'],
36-
'table_prefix' => $config['prefix'],
37-
];
38-
39-
$dbConfig['default_migration_table'] = $this->getConfig('table', $dbConfig['table_prefix'].'migrations');
40-
41-
return $dbConfig;
42-
}
43-
44-
return parent::getDbConfig();
45-
}
4616
}

0 commit comments

Comments
 (0)