-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDatabaseHandlerTest.php
More file actions
194 lines (180 loc) · 6.47 KB
/
Copy pathDatabaseHandlerTest.php
File metadata and controls
194 lines (180 loc) · 6.47 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/*
* This file is part of Aplus Framework Session Library.
*
* (c) Natan Felles <natanfelles@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\Session\SaveHandlers;
use Framework\Database\Database;
use Framework\Database\Definition\Table\TableDefinition;
use Framework\Session\SaveHandlers\DatabaseHandler;
use Framework\Session\Session;
/**
* Class DatabaseHandlerTest.
*
* @runTestsInSeparateProcesses
*/
class DatabaseHandlerTest extends AbstractHandler
{
protected string $handlerClass = DatabaseHandler::class;
public function setUp() : void
{
$this->replaceConfig([
'username' => \getenv('DB_USERNAME'),
'password' => \getenv('DB_PASSWORD'),
'schema' => \getenv('DB_SCHEMA'),
'host' => \getenv('DB_HOST'),
'port' => \getenv('DB_PORT'),
'table' => \getenv('DB_TABLE'),
]);
$this->createDummyData();
parent::setUp();
}
protected function createDummyData() : void
{
$database = new Database($this->config);
$database->dropTable($this->config['table'])->ifExists()->run();
$database->createTable($this->config['table'])
->definition(static function (TableDefinition $definition) : void {
$definition->column('id')->varchar(128)->primaryKey();
$definition->column('timestamp')->timestamp();
$definition->column('data')->blob();
$definition->column('ip')->varchar(45)->default('');
$definition->column('ua')->varchar(255)->default('');
$definition->column('user_id')->int(11)->null()->default(null);
$definition->index('timestamp')->key('timestamp');
$definition->index('ip')->key('ip');
$definition->index('ua')->key('ua');
$definition->index('user_id')->key('user_id');
})->run();
}
public function testUserId() : void
{
$this->session->stop();
$this->replaceConfig([
'save_user_id' => true,
]);
$handler = new class($this->config, $this->logger) extends DatabaseHandler {
public ?Database $database;
};
$session = new Session(handler: $handler);
$session->start();
$database = $handler->database;
$session->set('user_id', 123);
$id = $session->id();
$session->stop();
$result = $database->select('user_id') // @phpstan-ignore-line
->from($this->config['table'])
->whereEqual('id', $id) // @phpstan-ignore-line
->run()
->fetch()->user_id;
self::assertSame(123, $result);
}
public function testOpenError() : void
{
$this->session->stop();
$handler = new DatabaseHandler([
'username' => 'user-error',
'password' => \getenv('DB_PASSWORD'),
'host' => \getenv('DB_HOST'),
], $this->logger);
$session = new Session([], $handler);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Session could not be started');
try {
$session->start();
} catch (\RuntimeException $exception) {
self::assertMatchesRegularExpression(
'#Session \(database\): Thrown a mysqli_sql_exception while trying to open: '
. 'Access denied for user \'user-error\'@\'[0-9\.]+\' \(using password: YES\)#',
$this->logger->getLastLog()->message
);
throw $exception;
}
}
public function testFailToRead() : void
{
$handler = new class($this->config) extends DatabaseHandler {
public ?Database $database;
};
$handler->database = null;
self::assertSame('', $handler->read('foo'));
}
public function testFailToWrite() : void
{
$handler = new class($this->config) extends DatabaseHandler {
public ?Database $database;
public false | string $lockId;
};
$handler->database = null;
self::assertFalse($handler->write('foo', 'data'));
$handler->database = new Database($this->config);
$handler->lockId = false;
self::assertFalse($handler->write('foo', 'data'));
}
public function testFailToGC() : void
{
$handler = new DatabaseHandler([], $this->logger);
self::assertFalse(@$handler->gc(3600));
self::assertStringStartsWith(
'Session (database): Thrown a mysqli_sql_exception',
$this->logger->getLastLog()->message
);
}
public function testUnlockWithoutLockId() : void
{
$handler = new class() extends DatabaseHandler {
public function unlock() : bool
{
return parent::unlock();
}
};
self::assertTrue($handler->unlock());
}
public function testFailToUnlock() : void
{
$handler = new class($this->config) extends DatabaseHandler {
public false | string $lockId;
public function unlock() : bool
{
return parent::unlock();
}
};
$handler->open('', '');
$handler->lockId = 'foo';
self::assertFalse($handler->unlock());
}
public function testFailToLock() : void
{
$handler = new class($this->config, $this->logger) extends DatabaseHandler {
public function lock(string $id) : bool
{
return parent::lock($id);
}
};
$handler->open('', '');
self::assertFalse($handler->lock(''));
self::assertSame(
'Session (database): Error while trying to lock',
$this->logger->getLastLog()->message
);
}
public function testDatabaseSetterAndGetter() : void
{
$handler = new DatabaseHandler($this->config);
$database = new Database([
'username' => \getenv('DB_USERNAME'),
'password' => \getenv('DB_PASSWORD'),
'host' => \getenv('DB_HOST'),
]);
self::assertNull($handler->getDatabase());
$handler->setDatabase($database);
self::assertTrue($handler->open('', ''));
self::assertSame($database, $handler->getDatabase());
self::assertTrue($handler->close());
self::assertSame($database, $handler->getDatabase());
}
}