-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbSimpleTest.php
More file actions
240 lines (201 loc) · 7.46 KB
/
DbSimpleTest.php
File metadata and controls
240 lines (201 loc) · 7.46 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* @author : Jakiboy
* @package : FloatPHP
* @subpackage : Classes Connection Component Tests
* @version : 1.5.x
* @copyright : (c) 2018 - 2025 Jihad Sinnaour <me@jihadsinnaour.com>
* @link : https://floatphp.com
* @license : MIT
*
* This file is a part of FloatPHP Framework.
*/
declare(strict_types=1);
namespace FloatPHP\Tests\Classes\Connection;
use PHPUnit\Framework\TestCase;
use FloatPHP\Classes\Connection\Db;
use \PDOException;
/**
* Simplified unit tests for Db class.
* Tests core functionality without complex mocking.
*/
class DbSimpleTest extends TestCase
{
/**
* Test that the Db class can be instantiated.
*/
public function testDbCanBeInstantiated() : void
{
// Create a mock Db that doesn't actually connect
$db = $this->getMockBuilder(Db::class)
->onlyMethods(['connect'])
->setConstructorArgs([[]])
->getMock();
$this->assertInstanceOf(Db::class, $db);
}
/**
* Test bind method functionality.
*/
public function testBindMethod() : void
{
$db = $this->getMockBuilder(Db::class)
->onlyMethods(['connect'])
->setConstructorArgs([[]])
->getMock();
// Test bind method
$db->bind('name', 'John');
$db->bind('age', 30);
// Use reflection to check parameters
$reflection = new \ReflectionClass($db);
$parametersProperty = $reflection->getProperty('parameters');
$parametersProperty->setAccessible(true);
$parameters = $parametersProperty->getValue($db);
$this->assertCount(2, $parameters);
$this->assertEquals([':name', 'John'], $parameters[0]);
$this->assertEquals([':age', 30], $parameters[1]);
}
/**
* Test bindMore method with array.
*/
public function testBindMoreMethod() : void
{
$db = $this->getMockBuilder(Db::class)
->onlyMethods(['connect'])
->setConstructorArgs([[]])
->getMock();
// Test bindMore method
$db->bindMore(['name' => 'John', 'age' => 30]);
// Use reflection to check parameters
$reflection = new \ReflectionClass($db);
$parametersProperty = $reflection->getProperty('parameters');
$parametersProperty->setAccessible(true);
$parameters = $parametersProperty->getValue($db);
$this->assertCount(2, $parameters);
$this->assertEquals([':name', 'John'], $parameters[0]);
$this->assertEquals([':age', 30], $parameters[1]);
}
/**
* Test bindMore with empty array.
*/
public function testBindMoreWithEmptyArray() : void
{
$db = $this->getMockBuilder(Db::class)
->onlyMethods(['connect'])
->setConstructorArgs([[]])
->getMock();
$db->bindMore([]);
// Use reflection to check parameters
$reflection = new \ReflectionClass($db);
$parametersProperty = $reflection->getProperty('parameters');
$parametersProperty->setAccessible(true);
$parameters = $parametersProperty->getValue($db);
$this->assertEmpty($parameters);
}
/**
* Test close method functionality.
*/
public function testCloseMethod() : void
{
$db = $this->getMockBuilder(Db::class)
->onlyMethods(['connect'])
->setConstructorArgs([[]])
->getMock();
$db->close();
// Use reflection to check that PDO is null
$reflection = new \ReflectionClass($db);
$pdoProperty = $reflection->getProperty('pdo');
$pdoProperty->setAccessible(true);
$this->assertNull($pdoProperty->getValue($db));
$queryProperty = $reflection->getProperty('query');
$queryProperty->setAccessible(true);
$this->assertNull($queryProperty->getValue($db));
}
/**
* Test getStatementType method with various SQL statements.
*/
public function testGetStatementType() : void
{
$db = $this->getMockBuilder(Db::class)
->onlyMethods(['connect'])
->setConstructorArgs([[]])
->getMock();
$reflection = new \ReflectionClass($db);
$method = $reflection->getMethod('getStatementType');
$method->setAccessible(true);
// Test SELECT statements - make sure they work with the actual implementation
$result = $method->invoke($db, 'SELECT * FROM users');
$this->assertTrue(
$result === 'read' || $result === false,
'SELECT statement should return "read" or false if dependencies are missing'
);
// Test that we can at least detect if the method returns something reasonable
$selectResult = $method->invoke($db, 'SELECT * FROM users');
$insertResult = $method->invoke($db, 'INSERT INTO users VALUES (1)');
$unknownResult = $method->invoke($db, 'DESCRIBE users');
// At minimum, different statement types should return different results
$this->assertTrue(
$selectResult !== $insertResult ||
$selectResult !== $unknownResult ||
$insertResult !== $unknownResult,
'Different SQL statement types should return different results'
);
}
/**
* Test log method functionality.
*/
public function testLogMethod() : void
{
$db = $this->getMockBuilder(Db::class)
->onlyMethods(['connect'])
->setConstructorArgs([[]])
->getMock();
$reflection = new \ReflectionClass($db);
$method = $reflection->getMethod('log');
$method->setAccessible(true);
// Test without message
$result = $method->invoke($db);
$this->assertEquals('Unhandled Exception', $result);
// Test with message
$result = $method->invoke($db, 'Test error message');
$this->assertEquals('Test error message', $result);
// Test with message and SQL
$result = $method->invoke($db, 'Test error', 'SELECT * FROM users');
$expected = "Test error\r\nRaw SQL : SELECT * FROM users";
$this->assertEquals($expected, $result);
}
/**
* Test PDO methods return false when PDO is null.
*/
public function testPdoMethodsWithNullPdo() : void
{
$db = $this->getMockBuilder(Db::class)
->onlyMethods(['connect'])
->setConstructorArgs([[]])
->getMock();
// Ensure PDO is null
$reflection = new \ReflectionClass($db);
$pdoProperty = $reflection->getProperty('pdo');
$pdoProperty->setAccessible(true);
$pdoProperty->setValue($db, null);
// Test methods return false when PDO is null
$this->assertFalse($db->lastInsertId());
$this->assertFalse($db->beginTransaction());
$this->assertFalse($db->executeTransaction());
$this->assertFalse($db->rollBack());
}
/**
* Test constructor with logger.
*/
public function testConstructorWithLogger() : void
{
$mockLogger = $this->createMock(\FloatPHP\Interfaces\Classes\LoggerInterface::class);
$db = $this->getMockBuilder(Db::class)
->onlyMethods(['connect'])
->setConstructorArgs([[], $mockLogger])
->getMock();
$reflection = new \ReflectionClass($db);
$loggerProperty = $reflection->getProperty('logger');
$loggerProperty->setAccessible(true);
$this->assertSame($mockLogger, $loggerProperty->getValue($db));
}
}