forked from php-casbin/laravel-authz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestCase.php
91 lines (73 loc) · 2.85 KB
/
TestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace Lauthz\Tests;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Lauthz\Exceptions\UnauthorizedException;
use Lauthz\Models\Rule;
use Lauthz\Tests\Models\User;
abstract class TestCase extends BaseTestCase
{
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$this->app = require __DIR__.'/../vendor/laravel/laravel/bootstrap/app.php';
$this->app->booting(function () {
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Enforcer', \Lauthz\Facades\Enforcer::class);
});
$this->app->make(Kernel::class)->bootstrap();
$this->initConfig();
$this->app->register(\Lauthz\LauthzServiceProvider::class);
$this->artisan('vendor:publish', ['--provider' => 'Lauthz\LauthzServiceProvider']);
$this->artisan('migrate', ['--force' => true]);
$this->afterApplicationCreated(function () {
$this->initTable();
});
return $this->app;
}
protected function initConfig()
{
$this->app['config']->set('database.default', 'mysql');
$this->app['config']->set('database.connections.mysql.charset', 'utf8');
$this->app['config']->set('database.connections.mysql.collation', 'utf8_unicode_ci');
$this->app['config']->set('cache.default', 'array');
// $app['config']->set('lauthz.log.enabled', true);
}
protected function initTable()
{
Rule::truncate();
Rule::create(['ptype' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read']);
Rule::create(['ptype' => 'p', 'v0' => 'bob', 'v1' => 'data2', 'v2' => 'write']);
Rule::create(['ptype' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'read']);
Rule::create(['ptype' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'write']);
Rule::create(['ptype' => 'g', 'v0' => 'alice', 'v1' => 'data2_admin']);
}
protected function runMiddleware($middleware, $request, ...$args)
{
$middleware = $this->app->make($middleware);
try {
return $middleware->handle($request, function () {
return (new Response())->setContent('<html></html>');
}, ...$args)->status();
} catch (UnauthorizedException $e) {
return 'Unauthorized Exception';
}
return 'Exception';
}
protected function login($name)
{
Auth::login($this->user($name));
}
protected function user($name)
{
$user = new User();
$user->name = $name;
return $user;
}
}