PhpManagerTest.php 2.61 KB
Newer Older
tof06 committed
1 2
<?php

Carsten Brandt committed
3 4 5 6 7 8 9 10 11 12 13
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);
}

14 15 16 17 18 19 20 21 22
/**
 * Mock for the time() function for rbac classes. Avoid random test fails.
 * @return int
 */
function time()
{
    return \yiiunit\framework\rbac\PhpManagerTest::$time ?: \time();
}

tof06 committed
23 24 25 26 27 28
namespace yiiunit\framework\rbac;

use Yii;

/**
 * @group rbac
29
 * @property ExposedPhpManager $auth
tof06 committed
30
 */
31
class PhpManagerTest extends ManagerTestCase
tof06 committed
32
{
Carsten Brandt committed
33
    public static $filemtime;
34
    public static $time;
Carsten Brandt committed
35

Alexander Makarov committed
36
    protected function getItemFile()
37 38 39 40
    {
        return Yii::$app->getRuntimePath() . '/rbac-items.php';
    }

Alexander Makarov committed
41
    protected function getAssignmentFile()
42 43 44 45
    {
        return Yii::$app->getRuntimePath() . '/rbac-assignments.php';
    }

Alexander Makarov committed
46
    protected function getRuleFile()
47 48 49 50 51 52
    {
        return Yii::$app->getRuntimePath() . '/rbac-rules.php';
    }

    protected function removeDataFiles()
    {
Alexander Makarov committed
53 54 55
        @unlink($this->getItemFile());
        @unlink($this->getAssignmentFile());
        @unlink($this->getRuleFile());
56 57
    }

58 59 60
    /**
     * @inheritdoc
     */
61 62 63
    protected function createManager()
    {
        return new ExposedPhpManager([
Alexander Makarov committed
64 65 66
            'itemFile' => $this->getItemFile(),
            'assignmentFile' => $this->getAssignmentFile(),
            'ruleFile' => $this->getRuleFile(),
67 68 69
        ]);
    }

tof06 committed
70 71
    protected function setUp()
    {
Carsten Brandt committed
72
        static::$filemtime = null;
73
        static::$time = null;
tof06 committed
74
        parent::setUp();
75 76 77 78 79

        if (defined('HHVM_VERSION')) {
            $this->markTestSkipped('PhpManager is not compatible with HHVM.');
        }

tof06 committed
80
        $this->mockApplication();
81 82
        $this->removeDataFiles();
        $this->auth = $this->createManager();
tof06 committed
83 84 85 86
    }

    protected function tearDown()
    {
87
        $this->removeDataFiles();
Carsten Brandt committed
88
        static::$filemtime = null;
89
        static::$time = null;
tof06 committed
90 91 92 93 94
        parent::tearDown();
    }

    public function testSaveLoad()
    {
95
        static::$time = static::$filemtime = \time();
96

97
        $this->prepareData();
98 99 100 101
        $items = $this->auth->items;
        $children = $this->auth->children;
        $assignments = $this->auth->assignments;
        $rules = $this->auth->rules;
tof06 committed
102
        $this->auth->save();
103 104

        $this->auth = $this->createManager();
tof06 committed
105 106
        $this->auth->load();

107 108 109 110 111
        $this->assertEquals($items, $this->auth->items);
        $this->assertEquals($children, $this->auth->children);
        $this->assertEquals($assignments, $this->auth->assignments);
        $this->assertEquals($rules, $this->auth->rules);
    }
112
}