Passed
Push — master ( b28c40...f61e74 )
by Petr
08:06
created

MockExpiration   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 12
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 12
rs 10
wmc 2
1
<?php
2
3
namespace SourcesTests\Files;
4
5
6
use CommonTestClass;
7
use kalanis\kw_auth\AuthException;
8
use kalanis\kw_auth\Data\TExpire;
9
use kalanis\kw_auth\Interfaces\IExpire;
10
use kalanis\kw_auth\Sources;
11
use kalanis\kw_locks\Interfaces\ILock;
12
use kalanis\kw_locks\LockException;
13
14
15
class BasicTest extends CommonTestClass
16
{
17
    /**
18
     * @param string $in
19
     * @param string $want
20
     * @dataProvider stripProvider
21
     */
22
    public function testStrip(string $in, string $want): void
23
    {
24
        $lib = new MockLines();
25
        $this->assertEquals($want, $lib->stripChars($in));
26
    }
27
28
    public function stripProvider(): array
29
    {
30
        return [
31
            ['yxcvbnm', 'yxcvbnm'],
32
            ['jk~l,.qđwĐe', 'jkl,.qwe'],
33
        ];
34
    }
35
36
    /**
37
     * @param string $in
38
     * @param string $want
39
     * @dataProvider exImProvider
40
     */
41
    public function testExIm(string $in, string $want): void
42
    {
43
        $lib = new MockLines();
44
        $this->assertEquals($want, $lib->implosion($lib->explosion($in)));
45
    }
46
47
    public function exImProvider(): array
48
    {
49
        return [
50
            ['yxc:vb:nm', 'yxc:vb:nm'],
51
            ['yxcvbnm', 'yxcvbnm'],
52
            ['j°k~l:,.qđwĐe', 'j°k~l:,.qđwĐe'],
53
        ];
54
    }
55
56
    /**
57
     * @throws AuthException
58
     * @throws LockException
59
     */
60
    public function testLockEmpty(): void
61
    {
62
        $lib = new MockAuthLock(null);
63
        $this->expectException(AuthException::class);
64
        $lib->check();
65
    }
66
67
    /**
68
     * @throws AuthException
69
     * @throws LockException
70
     */
71
    public function testLockSimple(): void
72
    {
73
        $lib = new MockAuthLock($this->getLockPath());
74
        $lib->check();
75
        $this->assertTrue(true); // it runs, no errors
76
    }
77
78
    /**
79
     * @throws AuthException
80
     * @throws LockException
81
     */
82
    public function testLockMix(): void
83
    {
84
        $lock = $this->getLockPath();
85
        $lib = new MockAuthLock($lock);
86
        $lock->create();
87
        $this->expectException(AuthException::class);
88
        $lib->check();
89
    }
90
91
    /**
92
     * @throws AuthException
93
     */
94
    public function testExpire(): void
95
    {
96
        $target = new Expire();
97
        $lib = new MockExpiration(700, 100);
98
        $this->assertFalse($target->willExpire());
99
100
        $lib->setExpirationNotice($target, 650);
101
        $this->assertTrue($target->willExpire());
102
103
        $lib->setExpirationNotice($target, 750);
104
        $this->assertFalse($target->willExpire());
105
106
        $lib->updateExpirationTime($target);
107
        $this->assertEquals(1350, $target->getExpireTime());
108
    }
109
}
110
111
112
class MockLines
113
{
114
    use Sources\Files\TLines;
115
}
116
117
118
class MockAuthLock
119
{
120
    use Sources\TAuthLock;
121
122
    public function __construct(?ILock $lock)
123
    {
124
        $this->initAuthLock($lock);
125
    }
126
127
    /**
128
     * @throws AuthException
129
     * @throws LockException
130
     */
131
    public function check(): void
132
    {
133
        $this->checkLock();
134
    }
135
}
136
137
138
class MockExpiration
139
{
140
    use Sources\TExpiration;
141
142
    public function __construct(int $changeInterval, int $changeNoticeBefore)
143
    {
144
        $this->initExpiry($changeInterval, $changeNoticeBefore);
145
    }
146
147
    protected function getTime(): int
148
    {
149
        return 650;
150
    }
151
}
152
153
154
class Expire implements IExpire
155
{
156
    use TExpire;
157
}
158