Passed
Push — master ( d309df...7ec489 )
by Petr
02:37
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

2 Methods

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