Skip to content

Commit f244176

Browse files
authored
Merge pull request #6 from basakest/githubAction
feat: use 'Github Actions' to run tests and auto release, fix #5
2 parents 760838d + 350385b commit f244176

File tree

7 files changed

+349
-1
lines changed

7 files changed

+349
-1
lines changed

.github/workflows/build.yml

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
name: build
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
services:
14+
mysql:
15+
image: mysql:5.7
16+
env:
17+
MYSQL_ALLOW_EMPTY_PASSWORD: yes
18+
MYSQL_DATABASE: easyswoole-permission
19+
ports:
20+
- 3306:3306
21+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
22+
23+
strategy:
24+
fail-fast: true
25+
matrix:
26+
# php: [ ]
27+
# easyswoole: [ ]
28+
# stability: [ prefer-lowest, prefer-stable ]
29+
include:
30+
# easyswoole 3.4
31+
- php: 7.2
32+
easyswoole: 3.4.*
33+
phpunit: ~9.0
34+
swoole: 4.5
35+
- php: 7.3
36+
easyswoole: 3.4.*
37+
phpunit: ~9.0
38+
swoole: 4.5
39+
- php: 7.4
40+
easyswoole: 3.4.*
41+
phpunit: ~9.0
42+
swoole: 4.5
43+
# easyswoole 3.3
44+
- php: 7.2
45+
easyswoole: 3.3.*
46+
phpunit: ~8.0
47+
swoole: 4.5
48+
- php: 7.3
49+
easyswoole: 3.3.*
50+
phpunit: ~8.0
51+
swoole: 4.5
52+
- php: 7.4
53+
easyswoole: 3.3.*
54+
phpunit: ~8.0
55+
swoole: 4.5
56+
57+
name: easyswoole${{ matrix.easyswoole }}-PHP${{ matrix.php }}
58+
59+
steps:
60+
- name: Checkout code
61+
uses: actions/checkout@v2
62+
63+
- name: Setup PHP
64+
uses: shivammathur/setup-php@v2
65+
with:
66+
php-version: ${{ matrix.php }}
67+
tools: composer:v2
68+
coverage: xdebug
69+
extensions: swoole-${{ matrix.swoole }}
70+
71+
- name: Validate composer.json and composer.lock
72+
run: composer validate
73+
74+
- name: Install dependencies
75+
if: steps.composer-cache.outputs.cache-hit != 'true'
76+
run: |
77+
composer require easyswoole/easyswoole:${{ matrix.easyswoole }} --no-update --no-interaction
78+
composer install --prefer-dist --no-progress --no-suggest
79+
80+
- name: Run test suite
81+
run: ./vendor/bin/co-phpunit tests
82+
83+
- name: Run Coveralls
84+
env:
85+
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
86+
COVERALLS_PARALLEL: true
87+
COVERALLS_FLAG_NAME: ${{ runner.os }} - ${{ matrix.php }} - easyswoole${{ matrix.easyswoole }}
88+
run: |
89+
composer global require php-coveralls/php-coveralls:^2.4
90+
php-coveralls --coverage_clover=build/logs/clover.xml -v
91+
92+
upload-coverage:
93+
runs-on: ubuntu-latest
94+
needs: [ test ]
95+
steps:
96+
- name: Coveralls Finished
97+
uses: coverallsapp/github-action@master
98+
with:
99+
github-token: ${{ secrets.GITHUB_TOKEN }}
100+
parallel-finished: true
101+
102+
semantic-release:
103+
runs-on: ubuntu-latest
104+
needs: [ test, upload-coverage ]
105+
steps:
106+
- uses: actions/checkout@v2
107+
- uses: actions/setup-node@v1
108+
with:
109+
node-version: '12'
110+
111+
- name: Run semantic-release
112+
env:
113+
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
114+
run: npx semantic-release

.releaserc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
plugins:
2+
- "@semantic-release/commit-analyzer"
3+
- "@semantic-release/release-notes-generator"
4+
- "@semantic-release/github"

EasySwooleEvent.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
4+
namespace EasySwoole\EasySwoole;
5+
6+
7+
use EasySwoole\EasySwoole\AbstractInterface\Event;
8+
use EasySwoole\EasySwoole\Swoole\EventRegister;
9+
use EasySwoole\ORM\Db\Connection;
10+
use EasySwoole\ORM\DbManager;
11+
12+
class EasySwooleEvent implements Event
13+
{
14+
public static function initialize()
15+
{
16+
date_default_timezone_set('Asia/Shanghai');
17+
$config = new \EasySwoole\ORM\Db\Config(Config::getInstance()->getConf('MYSQL'));
18+
DbManager::getInstance()->addConnection(new Connection($config));
19+
}
20+
21+
public static function mainServerCreate(EventRegister $register)
22+
{
23+
24+
}
25+
}

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
"ext-swoole": ">=4.5",
1414
"casbin/casbin": "^2.2",
1515
"easyswoole/orm": "^1.4",
16-
"easyswoole/easyswoole": "3.x"
16+
"easyswoole/easyswoole": "~3.3|~3.4"
17+
},
18+
"require-dev": {
19+
"easyswoole/easyswoole": "~3.3|~3.4",
20+
"easyswoole/ddl": "^1.0",
21+
"easyswoole/phpunit": "^1.0"
1722
},
1823
"autoload": {
1924
"psr-4": {

dev.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
use EasySwoole\Log\LoggerInterface;
4+
5+
return [
6+
'SERVER_NAME' => "EasySwoole",
7+
'MAIN_SERVER' => [
8+
'LISTEN_ADDRESS' => '0.0.0.0',
9+
'PORT' => 9501,
10+
'SERVER_TYPE' => EASYSWOOLE_WEB_SERVER, //可选为 EASYSWOOLE_SERVER EASYSWOOLE_WEB_SERVER EASYSWOOLE_WEB_SOCKET_SERVER
11+
'SOCK_TYPE' => SWOOLE_TCP,
12+
'RUN_MODEL' => SWOOLE_PROCESS,
13+
'SETTING' => [
14+
'worker_num' => 8,
15+
'reload_async' => true,
16+
'max_wait_time' => 3
17+
],
18+
'TASK' => [
19+
'workerNum' => 4,
20+
'maxRunningNum' => 128,
21+
'timeout' => 15
22+
]
23+
],
24+
"LOG" => [
25+
'dir' => null,
26+
'level' => LoggerInterface::LOG_LEVEL_DEBUG,
27+
'handler' => null,
28+
'logConsole' => true,
29+
'displayConsole'=>true,
30+
'ignoreCategory' => []
31+
],
32+
'TEMP_DIR' => null,
33+
'MYSQL' => [
34+
'host' => '127.0.0.1',
35+
'port' => 3306,
36+
'user' => 'root',
37+
'password' => '',
38+
'database' => 'easyswoole',
39+
'timeout' => 5,
40+
'charset' => 'utf8mb4',
41+
]
42+
];

phpunit.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/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+
</phpunit>

tests/DatabaseAdapterTest.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
namespace EasySwoole\Permission\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use EasySwoole\ORM\DbManager;
7+
use EasySwoole\Permission\Model\RulesModel;
8+
use EasySwoole\Permission\Casbin;
9+
use EasySwoole\Permission\Config;
10+
use EasySwoole\ORM\Db\Connection;
11+
use EasySwoole\EasySwoole\Config as ESConfig;
12+
use EasySwoole\DDL\Blueprint\Create\Table as CreateTable;
13+
use EasySwoole\DDL\DDLBuilder;
14+
use EasySwoole\DDL\Enum\Character;
15+
use EasySwoole\DDL\Enum\Engine;
16+
17+
class DatabaseAdapterTest extends TestCase
18+
{
19+
protected function initDb()
20+
{
21+
RulesModel::create()->destroy(null, true);
22+
RulesModel::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read'])->save();
23+
RulesModel::create(['ptype' => 'p', 'v0' => 'bob', 'v1' => 'data2', 'v2' => 'write'])->save();
24+
RulesModel::create(['ptype' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'read'])->save();
25+
RulesModel::create(['ptype' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'write'])->save();
26+
RulesModel::create(['ptype' => 'g', 'v0' => 'alice', 'v1' => 'data2_admin'])->save();
27+
}
28+
29+
protected function getEnforcer()
30+
{
31+
$this->initConfig();
32+
$config = new Config();
33+
$casbin = new Casbin($config);
34+
$this->initTable();
35+
$this->initDb();
36+
return $casbin->enforcer();
37+
}
38+
39+
protected function initConfig()
40+
{
41+
$instance = \EasySwoole\EasySwoole\Config::getInstance();
42+
$conf = $instance->getConf();
43+
$conf['MYSQL'] = [
44+
'host' => '127.0.0.1',
45+
'port' => 3306,
46+
'user' => 'root',
47+
'password' => '',
48+
'database' => 'easyswoole',
49+
'timeout' => 5,
50+
'charset' => 'utf8mb4',
51+
];
52+
$instance->load($conf);
53+
$config = new \EasySwoole\ORM\Db\Config(ESConfig::getInstance()->getConf('MYSQL'));
54+
DbManager::getInstance()->addConnection(new Connection($config));
55+
}
56+
57+
public function initTable()
58+
{
59+
DDLBuilder::create('casbin_rules', function (CreateTable $table) {
60+
$table->setIfNotExists()->setTableComment('rule table of casbin');
61+
$table->setTableCharset(Character::UTF8MB4_GENERAL_CI);
62+
$table->setTableEngine(Engine::MYISAM);
63+
$table->int('id')->setIsUnsigned()->setIsAutoIncrement()->setIsPrimaryKey();
64+
$table->varchar('ptype', 255);
65+
$table->varchar('v0', 255);
66+
$table->varchar('v1', 255);
67+
$table->varchar('v2', 255);
68+
$table->varchar('v3', 255);
69+
$table->varchar('v4', 255);
70+
$table->varchar('v5', 255);
71+
});
72+
}
73+
74+
public function testRemovePolicy()
75+
{
76+
$e = $this->getEnforcer();
77+
$this->assertFalse($e->enforce('alice', 'data5', 'read'));
78+
$e->addPermissionForUser('alice', 'data5', 'read');
79+
$this->assertTrue($e->enforce('alice', 'data5', 'read'));
80+
$e->deletePermissionForUser('alice', 'data5', 'read');
81+
$this->assertFalse($e->enforce('alice', 'data5', 'read'));
82+
}
83+
84+
public function testLoadPolicy()
85+
{
86+
$e = $this->getEnforcer();
87+
$this->assertTrue($e->enforce('alice', 'data1', 'read'));
88+
$this->assertFalse($e->enforce('bob', 'data1', 'read'));
89+
$this->assertTrue($e->enforce('bob', 'data2', 'write'));
90+
$this->assertTrue($e->enforce('alice', 'data2', 'read'));
91+
$this->assertTrue($e->enforce('alice', 'data2', 'write'));
92+
}
93+
94+
public function testAddPolicy()
95+
{
96+
$e = $this->getEnforcer();
97+
$this->assertFalse($e->enforce('eve', 'data3', 'read'));
98+
$e->addPermissionForUser('eve', 'data3', 'read');
99+
$this->assertTrue($e->enforce('eve', 'data3', 'read'));
100+
}
101+
102+
public function testRemoveFilteredPolicy()
103+
{
104+
$e = $this->getEnforcer();
105+
$this->assertTrue($e->enforce('alice', 'data1', 'read'));
106+
$e->removeFilteredPolicy(1, 'data1');
107+
$this->assertFalse($e->enforce('alice', 'data1', 'read'));
108+
$this->assertTrue($e->enforce('bob', 'data2', 'write'));
109+
$this->assertTrue($e->enforce('alice', 'data2', 'read'));
110+
$this->assertTrue($e->enforce('alice', 'data2', 'write'));
111+
$e->removeFilteredPolicy(1, 'data2', 'read');
112+
$this->assertTrue($e->enforce('bob', 'data2', 'write'));
113+
$this->assertFalse($e->enforce('alice', 'data2', 'read'));
114+
$this->assertTrue($e->enforce('alice', 'data2', 'write'));
115+
$e->removeFilteredPolicy(2, 'write');
116+
$this->assertFalse($e->enforce('bob', 'data2', 'write'));
117+
$this->assertFalse($e->enforce('alice', 'data2', 'write'));
118+
}
119+
120+
public function testSavePolicy()
121+
{
122+
$e = $this->getEnforcer();
123+
$this->assertFalse($e->enforce('alice', 'data4', 'read'));
124+
125+
$model = $e->getModel();
126+
$model->clearPolicy();
127+
$model->addPolicy('p', 'p', ['alice', 'data4', 'read']);
128+
129+
$adapter = $e->getAdapter();
130+
$adapter->savePolicy($model);
131+
$this->assertTrue($e->enforce('alice', 'data4', 'read'));
132+
}
133+
}

0 commit comments

Comments
 (0)