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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace yii\rbac;
/**
* Mock for the filemtime() function for rbac classes. Avoid random test fails.
* @return int
*/
function filemtime($file)
{
return \yiiunit\framework\rbac\PhpManagerTest::$filemtime ?: \filemtime($file);
}
/**
* Mock for the time() function for rbac classes. Avoid random test fails.
* @return int
*/
function time()
{
return \yiiunit\framework\rbac\PhpManagerTest::$time ?: \time();
}
namespace yiiunit\framework\rbac;
use Yii;
/**
* @group rbac
* @property ExposedPhpManager $auth
*/
class PhpManagerTest extends ManagerTestCase
{
public static $filemtime;
public static $time;
protected function getItemFile()
{
return Yii::$app->getRuntimePath() . '/rbac-items.php';
}
protected function getAssignmentFile()
{
return Yii::$app->getRuntimePath() . '/rbac-assignments.php';
}
protected function getRuleFile()
{
return Yii::$app->getRuntimePath() . '/rbac-rules.php';
}
protected function removeDataFiles()
{
@unlink($this->getItemFile());
@unlink($this->getAssignmentFile());
@unlink($this->getRuleFile());
}
/**
* @inheritdoc
*/
protected function createManager()
{
return new ExposedPhpManager([
'itemFile' => $this->getItemFile(),
'assignmentFile' => $this->getAssignmentFile(),
'ruleFile' => $this->getRuleFile(),
]);
}
protected function setUp()
{
static::$filemtime = null;
static::$time = null;
parent::setUp();
if (defined('HHVM_VERSION')) {
$this->markTestSkipped('PhpManager is not compatible with HHVM.');
}
$this->mockApplication();
$this->removeDataFiles();
$this->auth = $this->createManager();
}
protected function tearDown()
{
$this->removeDataFiles();
static::$filemtime = null;
static::$time = null;
parent::tearDown();
}
public function testSaveLoad()
{
static::$time = static::$filemtime = \time();
$this->prepareData();
$items = $this->auth->items;
$children = $this->auth->children;
$assignments = $this->auth->assignments;
$rules = $this->auth->rules;
$this->auth->save();
$this->auth = $this->createManager();
$this->auth->load();
$this->assertEquals($items, $this->auth->items);
$this->assertEquals($children, $this->auth->children);
$this->assertEquals($assignments, $this->auth->assignments);
$this->assertEquals($rules, $this->auth->rules);
}
}