Skip to content

Commit 7a49773

Browse files
committed
Unit tests
1 parent 9c532a7 commit 7a49773

2 files changed

Lines changed: 133 additions & 3 deletions

File tree

tests/DBConnectorTest.php

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ public function testThat_setupDbWorksAsExpected() {
5555
public function testThat_executeWorksAsExpected() {
5656

5757
self::assertTrue(
58-
DbConnectorSubclass::executePublic('Select * from posts', [], false, static::$dsn)
58+
DbConnectorSubclass::oldExecutePublic('Select * from posts', [], false, static::$dsn)
5959
);
6060

61-
$execResult = DbConnectorSubclass::executePublic(
61+
$execResult = DbConnectorSubclass::oldExecutePublic(
6262
'Select * from authors where author_id in (?, ?, ?, ?, ?, ?) and name like ? ',
6363
(static::$driverName === 'pgsql')
6464
? [1, 5, 10, null, 0, 1, 'user_1%']
@@ -96,6 +96,126 @@ public function testThat_executeWorksAsExpected() {
9696
);
9797
}
9898

99+
public function testThatExecuteWorksAsExpected() {
100+
101+
////////////////////////////////////////////////////////////////////////
102+
// create first and second connection with same dsn
103+
////////////////////////////////////////////////////////////////////////
104+
$pdo_driver_opts =
105+
(static::$driverName === 'mysql')
106+
? [ \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', ] : [];
107+
DbConnectorSubclass::configure(static::$dsn, null, static::$dsn);//use $dsn as connection name in 3rd parameter
108+
DbConnectorSubclass::configure(DbConnectorSubclass::CONFIG_KEY_USERNAME, static::$username ?? "", static::$dsn);//use $dsn as connection name in 3rd parameter
109+
DbConnectorSubclass::configure(DbConnectorSubclass::CONFIG_KEY_PASSWORD, static::$password ?? "", static::$dsn);//use $dsn as connection name in 3rd parameter
110+
111+
if( $pdo_driver_opts !== [] ) {
112+
113+
DBConnector::configure(DbConnectorSubclass::CONFIG_KEY_DRIVER_OPTS, $pdo_driver_opts, static::$dsn);//use $dsn as connection name in 3rd parameter
114+
}
115+
116+
/** @var DbConnectorSubclass $db_connector */
117+
$db_connector = DbConnectorSubclass::create(static::$dsn);//use $dsn as connection name
118+
////////////////////////////////////////////////////////////////////////
119+
// End: create first and second connection with same dsn
120+
////////////////////////////////////////////////////////////////////////
121+
122+
self::assertTrue(
123+
$db_connector->executePublic('Select * from posts', [], static::$dsn)->pdo_statement_execute_result
124+
);
125+
126+
$execResult = $db_connector->executePublic(
127+
'Select * from authors where author_id in (?, ?, ?, ?, ?, ?) and name like ? ',
128+
(static::$driverName === 'pgsql')
129+
? [1, 5, 10, null, 0, 1, 'user_1%']
130+
: [1, 5, 10, null, true, false, 'user_1%']
131+
,
132+
static::$dsn
133+
);
134+
135+
self::assertInstanceOf(\PDOStatement::class, $execResult->pdo_statement);
136+
137+
// lets loop through the query results & assert the rows we are expecting
138+
$records = $execResult->pdo_statement->fetchAll(\PDO::FETCH_ASSOC) ?? [];
139+
140+
self::assertCount(2, $records);
141+
142+
foreach($records as $record) {
143+
144+
self::assertArrayHasAllKeys($record, ["author_id", "name", "m_timestamp", "date_created"]);
145+
}
146+
147+
self::assertEquals(
148+
(static::$driverName === 'pgsql')
149+
? [1, 10] // postgres driver correctly returns ints, while mysql & sqlite return ints in a string
150+
: ['1', '10'],
151+
array_column($records, "author_id")
152+
);
153+
154+
self::assertEquals(
155+
['user_1', 'user_10'],
156+
array_column($records, "name")
157+
);
158+
}
159+
160+
public function testThatRunQueryWorksAsExpected() {
161+
162+
////////////////////////////////////////////////////////////////////////
163+
// create first and second connection with same dsn
164+
////////////////////////////////////////////////////////////////////////
165+
$pdo_driver_opts =
166+
(static::$driverName === 'mysql')
167+
? [ \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', ] : [];
168+
DbConnectorSubclass::configure(static::$dsn, null, static::$dsn);//use $dsn as connection name in 3rd parameter
169+
DbConnectorSubclass::configure(DbConnectorSubclass::CONFIG_KEY_USERNAME, static::$username ?? "", static::$dsn);//use $dsn as connection name in 3rd parameter
170+
DbConnectorSubclass::configure(DbConnectorSubclass::CONFIG_KEY_PASSWORD, static::$password ?? "", static::$dsn);//use $dsn as connection name in 3rd parameter
171+
172+
if( $pdo_driver_opts !== [] ) {
173+
174+
DBConnector::configure(DbConnectorSubclass::CONFIG_KEY_DRIVER_OPTS, $pdo_driver_opts, static::$dsn);//use $dsn as connection name in 3rd parameter
175+
}
176+
177+
/** @var DbConnectorSubclass $db_connector */
178+
$db_connector = DbConnectorSubclass::create(static::$dsn);//use $dsn as connection name
179+
////////////////////////////////////////////////////////////////////////
180+
// End: create first and second connection with same dsn
181+
////////////////////////////////////////////////////////////////////////
182+
183+
self::assertTrue(
184+
$db_connector->runQuery('Select * from posts', [])->pdo_statement_execute_result
185+
);
186+
187+
$execResult = $db_connector->runQuery(
188+
'Select * from authors where author_id in (?, ?, ?, ?, ?, ?) and name like ? ',
189+
(static::$driverName === 'pgsql')
190+
? [1, 5, 10, null, 0, 1, 'user_1%']
191+
: [1, 5, 10, null, true, false, 'user_1%']
192+
);
193+
194+
self::assertInstanceOf(\PDOStatement::class, $execResult->pdo_statement);
195+
196+
// lets loop through the query results & assert the rows we are expecting
197+
$records = $execResult->pdo_statement->fetchAll(\PDO::FETCH_ASSOC) ?? [];
198+
199+
self::assertCount(2, $records);
200+
201+
foreach($records as $record) {
202+
203+
self::assertArrayHasAllKeys($record, ["author_id", "name", "m_timestamp", "date_created"]);
204+
}
205+
206+
self::assertEquals(
207+
(static::$driverName === 'pgsql')
208+
? [1, 10] // postgres driver correctly returns ints, while mysql & sqlite return ints in a string
209+
: ['1', '10'],
210+
array_column($records, "author_id")
211+
);
212+
213+
self::assertEquals(
214+
['user_1', 'user_10'],
215+
array_column($records, "name")
216+
);
217+
}
218+
99219
public function testThatClearQueryLogWorksAsExpected() {
100220

101221
////////////////////////////////////////////////////////////////////////

tests/TestObjects/DbConnectorSubclass.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,21 @@
66
*/
77
class DbConnectorSubclass extends \LeanOrm\DBConnector {
88

9-
public static function executePublic($query, array $parameters = [], bool $return_pdo_stmt_and_exec_time = false, string $connection_name = self::DEFAULT_CONNECTION) {
9+
public static function oldExecutePublic($query, array $parameters = [], bool $return_pdo_stmt_and_exec_time = false, string $connection_name = self::DEFAULT_CONNECTION) {
1010

1111
return parent::_execute($query, $parameters, $return_pdo_stmt_and_exec_time, $connection_name);
1212
}
1313

14+
public function executePublic(
15+
string $query,
16+
array $parameters = [],
17+
string $connection_name = self::DEFAULT_CONNECTION,
18+
?object $calling_object = null
19+
): \LeanOrm\DBExceuteQueryResult {
20+
21+
return $this->execute($query, $parameters, $connection_name, $calling_object);
22+
}
23+
1424
public static function initDbConfigWithDefaultValsPublic(string $connection_name): void {
1525

1626
parent::_initDbConfigWithDefaultVals($connection_name);

0 commit comments

Comments
 (0)