1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace SmrTest\lib\DefaultGame; |
4
|
|
|
|
5
|
|
|
use DI\Container; |
6
|
|
|
use Error; |
7
|
|
|
use Exception; |
8
|
|
|
use MySqlDatabase; |
9
|
|
|
use mysqli; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use Smr\Container\DiContainer; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class MySqlDatabaseIntegrationTest |
15
|
|
|
* This is an integration test, but does not need to extend BaseIntegrationTest since we are not writing any data. |
16
|
|
|
* @covers MySqlDatabase |
17
|
|
|
* @package SmrTest\lib\DefaultGame |
18
|
|
|
*/ |
19
|
|
|
class MySqlDatabaseIntegrationTest extends TestCase { |
20
|
|
|
private Container $container; |
21
|
|
|
|
22
|
|
|
protected function setUp(): void { |
23
|
|
|
DiContainer::initializeContainer(); |
24
|
|
|
$this->container = DiContainer::getContainer(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function test_performing_operations_on_closed_database_throws_error() { |
28
|
|
|
// Expectations |
29
|
|
|
$this->expectException(Error::class); |
30
|
|
|
$this->expectExceptionMessage('Typed property MySqlDatabase::$dbConn must not be accessed before initialization'); |
31
|
|
|
// Given a mysql database instance |
32
|
|
|
$mysqlDatabase = MySqlDatabase::getInstance(); |
33
|
|
|
// And disconnect is called |
34
|
|
|
$mysqlDatabase->close(); |
35
|
|
|
// When calling database methods |
36
|
|
|
$mysqlDatabase->query("foo query"); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function test_getInstance_will_perform_reconnect_after_connection_closed() { |
40
|
|
|
// Given an original mysql connection |
41
|
|
|
$originalMysql = $this->container->get(mysqli::class); |
42
|
|
|
// And a mysql database instance |
43
|
|
|
$mysqlDatabase = MySqlDatabase::getInstance(); |
44
|
|
|
// And disconnect is called |
45
|
|
|
$mysqlDatabase->close(); |
46
|
|
|
// And mysql database is retrieved from the container |
47
|
|
|
$mysqlDatabase = MySqlDatabase::getInstance(); |
48
|
|
|
// When performing a query |
49
|
|
|
$mysqlDatabase->query("select 1"); |
50
|
|
|
// Then new mysqli instance is not the same as the initial mock |
51
|
|
|
self::assertNotSame($originalMysql, $this->container->get(mysqli::class)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function test_getInstance_will_not_perform_reconnect_if_connection_not_closed() { |
55
|
|
|
// Given an original mysql connection |
56
|
|
|
$originalMysql = $this->container->get(mysqli::class); |
57
|
|
|
// And a mysql database instance |
58
|
|
|
MySqlDatabase::getInstance(); |
59
|
|
|
// And get instance is called again |
60
|
|
|
MySqlDatabase::getInstance(); |
61
|
|
|
// Then the two mysqli instances are the same |
62
|
|
|
self::assertSame($originalMysql, $this->container->get(mysqli::class)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function test_escapeMicrotime() { |
66
|
|
|
$db = MySqlDatabase::getInstance(); |
67
|
|
|
// The current microtime must not throw an exception |
68
|
|
|
$db->escapeMicrotime(microtime(true)); |
69
|
|
|
// Check that the formatting preserves all digits |
70
|
|
|
self::assertSame("1608455259123456", $db->escapeMicrotime(1608455259.123456)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function test_escapeBoolean() { |
74
|
|
|
$db = MySqlDatabase::getInstance(); |
75
|
|
|
// Test both boolean values |
76
|
|
|
self::assertSame("'TRUE'", $db->escapeBoolean(true)); |
77
|
|
|
self::assertSame("'FALSE'", $db->escapeBoolean(false)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function test_escapeString() { |
81
|
|
|
$db = MySqlDatabase::getInstance(); |
82
|
|
|
// Test the empty string |
83
|
|
|
self::assertSame("''", $db->escapeString('')); |
84
|
|
|
self::assertSame('NULL', $db->escapeString('', true)); // nullable |
85
|
|
|
// Test null |
86
|
|
|
self::assertSame('NULL', $db->escapeString(null, true)); // nullable |
87
|
|
|
// Test a normal string |
88
|
|
|
self::assertSame("'bla'", $db->escapeString('bla')); |
89
|
|
|
self::assertSame("'bla'", $db->escapeString('bla', true)); // nullable |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function test_escapeString_null_throws() { |
93
|
|
|
$db = MySqlDatabase::getInstance(); |
94
|
|
|
$this->expectException(\TypeError::class); |
95
|
|
|
$db->escapeString(null); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function test_escapeArray() { |
99
|
|
|
$db = MySqlDatabase::getInstance(); |
100
|
|
|
// Test a mixed array |
101
|
|
|
self::assertSame("'a',2,'c'", $db->escapeArray(['a', 2, 'c'])); |
102
|
|
|
// Test a different implodeString |
103
|
|
|
self::assertSame("'a':2:'c'", $db->escapeArray(['a', 2, 'c'], ':')); |
104
|
|
|
// Test escapeIndividually=false |
105
|
|
|
self::assertSame("'a,2,c'", $db->escapeArray(['a', 2, 'c'], ',', false)); |
106
|
|
|
// Test nested arrays |
107
|
|
|
// Warning: The array is flattened, which may be unexpected! |
108
|
|
|
self::assertSame("'a','x',9,2", $db->escapeArray(['a', ['x', 9], 2], ',', true)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function test_escapeArray_nested_array_throws() { |
112
|
|
|
// Warning: It is dangerous to use nested arrays with escapeIndividually=false |
113
|
|
|
$db = MySqlDatabase::getInstance(); |
114
|
|
|
$this->expectNotice(); |
115
|
|
|
$this->expectNoticeMessage('Array to string conversion'); |
116
|
|
|
$db->escapeArray(['a', ['x', 9, 'y'], 2, 'c'], ':', false); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function test_escapeNumber() { |
120
|
|
|
// No escaping is done of numeric types |
121
|
|
|
$db = MySqlDatabase::getInstance(); |
122
|
|
|
// Test int |
123
|
|
|
self::assertSame(42, $db->escapeNumber(42)); |
124
|
|
|
// Test float |
125
|
|
|
self::assertSame(0.21, $db->escapeNumber(0.21)); |
126
|
|
|
// Test numeric string |
127
|
|
|
self::assertSame('42', $db->escapeNumber('42')); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function test_escapeNumber_nonnumeric_throws() { |
131
|
|
|
$db = MySqlDatabase::getInstance(); |
132
|
|
|
$this->expectException(\RuntimeException::class); |
133
|
|
|
$this->expectExceptionMessage('Not a number'); |
134
|
|
|
$db->escapeNumber('bla'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function test_escapeObject() { |
138
|
|
|
$db = MySqlDatabase::getInstance(); |
139
|
|
|
// Test null |
140
|
|
|
self::assertSame('NULL', $db->escapeObject(null, false, true)); |
141
|
|
|
// Test empty array |
142
|
|
|
self::assertSame("'a:0:{}'", $db->escapeObject([])); |
143
|
|
|
// Test empty string |
144
|
|
|
self::assertSame('\'s:0:\"\";\'', $db->escapeObject('')); |
145
|
|
|
// Test zero |
146
|
|
|
self::assertSame("'i:0;'", $db->escapeObject(0)); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|