Skip to content

Commit 7c14ddc

Browse files
committed
Refactor test setup to centralize database connection logic with ConnectionUtil and optimize transaction handling in Repository.
1 parent 213967c commit 7c14ddc

8 files changed

Lines changed: 78 additions & 74 deletions

src/Repository.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use ByJG\Serializer\ObjectCopy;
2424
use ByJG\Serializer\Serialize;
2525
use Closure;
26+
use Exception;
2627
use ReflectionException;
2728
use stdClass;
2829
use Throwable;
@@ -259,18 +260,28 @@ public function bulkExecute(array $queries, ?IsolationLevelEnum $isolationLevel
259260
$bigSqlWrites .= rtrim($sql, "; \t\n\r\0\x0B") . ";\n";
260261
}
261262

262-
// First execute all writes (if any) in a single batch using direct PDO exec
263-
if (trim($bigSqlWrites) !== '') {
264-
// Use direct PDO to ensure multi-statement execution across drivers like SQLite
265-
$dbDriver->execute($bigSqlWrites, $bigParams);
266-
}
263+
$dbDriver->beginTransaction($isolationLevel, allowJoin: true);
264+
try {
265+
// First execute all writes (if any) in a single batch using direct PDO exec
266+
if (trim($bigSqlWrites) !== '') {
267+
// Use direct PDO to ensure multi-statement execution across drivers like SQLite
268+
$dbDriver->execute($bigSqlWrites, $bigParams);
269+
}
267270

268-
// If there is a trailing SELECT, fetch it and return its iterator. Otherwise return an empty iterator
269-
if (!empty($selectSql)) {
270-
return $dbDriver->getIterator($selectSql, $selectParams);
271-
}
271+
// If there is a trailing SELECT, fetch it and return its iterator. Otherwise return an empty iterator
272+
if (!empty($selectSql)) {
273+
$it = $dbDriver->getIterator($selectSql, $selectParams);
274+
} else {
275+
$it = (new ArrayDataset([]))->getIterator();
276+
}
277+
278+
$dbDriver->commitTransaction();
272279

273-
return (new ArrayDataset([]))->getIterator();
280+
return $it;
281+
} catch (Exception $ex) {
282+
$dbDriver->rollbackTransaction();
283+
throw $ex;
284+
}
274285
}
275286

276287
/**

tests/BulkTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Tests;
44

55
use ByJG\AnyDataset\Db\DbDriverInterface;
6-
use ByJG\AnyDataset\Db\Factory;
76
use ByJG\MicroOrm\DeleteQuery;
87
use ByJG\MicroOrm\Exception\InvalidArgumentException;
98
use ByJG\MicroOrm\InsertBulkQuery;
@@ -18,16 +17,12 @@
1817

1918
class BulkTest extends TestCase
2019
{
21-
const URI = 'mysql://root:password@127.0.0.1';
22-
2320
protected DbDriverInterface $dbDriver;
2421
protected Repository $repository;
2522

2623
protected function setUp(): void
2724
{
28-
$this->dbDriver = Factory::getDbInstance(self::URI);
29-
$this->dbDriver->execute('create database if not exists testmicroorm;');
30-
$this->dbDriver = Factory::getDbInstance(self::URI . '/testmicroorm');
25+
$this->dbDriver = ConnectionUtil::getConnection('testmicroorm');
3126

3227
// Create table and seed data similar to RepositoryTest
3328
$this->dbDriver->execute('create table users (

tests/ConnectionUtil.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use ByJG\AnyDataset\Db\DbDriverInterface;
6+
use ByJG\AnyDataset\Db\Factory;
7+
use ByJG\Util\Uri;
8+
9+
class ConnectionUtil
10+
{
11+
public static function getConnection(string $database): DbDriverInterface
12+
{
13+
$dbDriver = Factory::getDbInstance(ConnectionUtil::getUri());
14+
$dbDriver->execute("create database if not exists $database;");
15+
return Factory::getDbInstance(ConnectionUtil::getUri($database));
16+
}
17+
18+
public static function getUri(?string $database = null): Uri
19+
{
20+
$host = getenv('MYSQL_TEST_HOST') ? getenv('MYSQL_TEST_HOST') : '127.0.0.1';
21+
$uri = new Uri("mysql://root:password@$host");
22+
23+
if (empty($database)) {
24+
return $uri;
25+
}
26+
27+
return $uri->withPath("/$database");
28+
}
29+
}

tests/RepositoryAliasTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Tests;
44

55
use ByJG\AnyDataset\Db\DbDriverInterface;
6-
use ByJG\AnyDataset\Db\Factory;
76
use ByJG\MicroOrm\FieldMapping;
87
use ByJG\MicroOrm\Mapper;
98
use ByJG\MicroOrm\Query;
@@ -13,9 +12,6 @@
1312

1413
class RepositoryAliasTest extends TestCase
1514
{
16-
17-
const URI = 'mysql://root:password@127.0.0.1';
18-
1915
/**
2016
* @var Mapper
2117
*/
@@ -33,9 +29,7 @@ class RepositoryAliasTest extends TestCase
3329

3430
public function setUp(): void
3531
{
36-
$this->dbDriver = Factory::getDbInstance(self::URI);
37-
$this->dbDriver->execute('create database if not exists testmicroorm;');
38-
$this->dbDriver = Factory::getDbInstance(self::URI . "/testmicroorm");
32+
$this->dbDriver = ConnectionUtil::getConnection("testmicroorm");
3933

4034
$this->dbDriver->execute('create table customers (
4135
id integer primary key auto_increment,

tests/RepositoryPkListTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@
33
namespace Tests;
44

55
use ByJG\AnyDataset\Db\DbDriverInterface;
6-
use ByJG\AnyDataset\Db\Factory;
76
use ByJG\MicroOrm\Mapper;
87
use ByJG\MicroOrm\Repository;
98
use PHPUnit\Framework\TestCase;
109
use Tests\Model\Items;
1110

1211
class RepositoryPkListTest extends TestCase
1312
{
14-
15-
const URI = 'mysql://root:password@127.0.0.1';
16-
1713
/**
1814
* @var Mapper
1915
*/
@@ -31,9 +27,7 @@ class RepositoryPkListTest extends TestCase
3127

3228
public function setUp(): void
3329
{
34-
$this->dbDriver = Factory::getDbInstance(self::URI);
35-
$this->dbDriver->execute('create database if not exists testmicroorm;');
36-
$this->dbDriver = Factory::getDbInstance(self::URI . "/testmicroorm");
30+
$this->dbDriver = ConnectionUtil::getConnection("testmicroorm");
3731

3832
$this->dbDriver->execute('CREATE TABLE items (
3933
storeid INTEGER,

tests/RepositoryTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use ByJG\AnyDataset\Core\Enum\Relation;
66
use ByJG\AnyDataset\Core\IteratorFilter;
77
use ByJG\AnyDataset\Db\DbDriverInterface;
8-
use ByJG\AnyDataset\Db\Factory;
98
use ByJG\Cache\Psr16\ArrayCacheEngine;
109
use ByJG\MicroOrm\CacheQueryResult;
1110
use ByJG\MicroOrm\DeleteQuery;
@@ -44,9 +43,6 @@
4443

4544
class RepositoryTest extends TestCase
4645
{
47-
48-
const URI = 'mysql://root:password@127.0.0.1';
49-
5046
/**
5147
* @var Mapper
5248
*/
@@ -69,9 +65,7 @@ class RepositoryTest extends TestCase
6965

7066
public function setUp(): void
7167
{
72-
$this->dbDriver = Factory::getDbInstance(self::URI);
73-
$this->dbDriver->execute('create database if not exists testmicroorm;');
74-
$this->dbDriver = Factory::getDbInstance(self::URI . "/testmicroorm");
68+
$this->dbDriver = ConnectionUtil::getConnection("testmicroorm");
7569

7670
$this->dbDriver->execute('create table users (
7771
id integer primary key auto_increment,

tests/RepositoryUuidTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@
33
namespace Tests;
44

55
use ByJG\AnyDataset\Db\DbDriverInterface;
6-
use ByJG\AnyDataset\Db\Factory;
76
use ByJG\MicroOrm\Literal\HexUuidLiteral;
87
use ByJG\MicroOrm\Repository;
98
use PHPUnit\Framework\TestCase;
109
use Tests\Model\UsersWithUuidKey;
1110

1211
class RepositoryUuidTest extends TestCase
1312
{
14-
15-
const URI = 'mysql://root:password@127.0.0.1';
16-
1713
/**
1814
* @var DbDriverInterface
1915
*/
@@ -26,9 +22,7 @@ class RepositoryUuidTest extends TestCase
2622

2723
public function setUp(): void
2824
{
29-
$this->dbDriver = Factory::getDbInstance(self::URI);
30-
$this->dbDriver->execute('create database if not exists testmicroorm;');
31-
$this->dbDriver = Factory::getDbInstance(self::URI . "/testmicroorm");
25+
$this->dbDriver = ConnectionUtil::getConnection("testmicroorm");
3226

3327
$this->dbDriver->execute('create table usersuuid (
3428
id binary(16) primary key,

tests/TransactionManagerTest.php

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
class TransactionManagerTest extends TestCase
1515
{
16-
const URI = 'mysql://root:password@127.0.0.1';
1716
/**
1817
* @var TransactionManager
1918
*/
@@ -22,24 +21,18 @@ class TransactionManagerTest extends TestCase
2221
public function setUp(): void
2322
{
2423
$this->object = new TransactionManager();
25-
26-
$dbDriver = Factory::getDbInstance(self::URI);
27-
$dbDriver->execute('create database if not exists a;');
28-
$dbDriver->execute('create database if not exists b;');
29-
$dbDriver->execute('create database if not exists c;');
30-
$dbDriver->execute('create database if not exists d;');
3124
}
3225

3326
public function tearDown(): void
3427
{
3528
$this->object->destroy();
3629
$this->object = null;
3730

38-
$dbDriver = Factory::getDbInstance(self::URI . "/a");
31+
$dbDriver = ConnectionUtil::getConnection("a");
3932
$dbDriver->execute('drop table if exists users;');
4033
$dbDriver->execute('drop table if exists users1;');
4134

42-
$dbDriver = Factory::getDbInstance(self::URI . "/b");
35+
$dbDriver = ConnectionUtil::getConnection("b");
4336
$dbDriver->execute('drop table if exists users2;');
4437
}
4538

@@ -48,16 +41,16 @@ public function testAddConnectionError()
4841
$this->expectException(InvalidArgumentException::class);
4942
$this->expectExceptionMessage("The connection already exists with a different instance");
5043

51-
$dbDrive1 = $this->object->addConnection(self::URI . "/a");
52-
$dbDrive2 = $this->object->addConnection(self::URI . "/b");
53-
$dbDrive3 = $this->object->addConnection(self::URI . "/a");
54-
$dbDrive4 = $this->object->addConnection(self::URI . "/b");
44+
$dbDrive1 = $this->object->addConnection(ConnectionUtil::getUri("a"));
45+
$dbDrive2 = $this->object->addConnection(ConnectionUtil::getUri("b"));
46+
$dbDrive3 = $this->object->addConnection(ConnectionUtil::getUri("a"));
47+
$dbDrive4 = $this->object->addConnection(ConnectionUtil::getUri("b"));
5548
}
5649

5750
public function testAddConnection()
5851
{
59-
$dbDrive1 = $this->object->addConnection(self::URI . "/a");
60-
$dbDrive2 = $this->object->addConnection(self::URI . "/b");
52+
$dbDrive1 = $this->object->addConnection(ConnectionUtil::getUri("a"));
53+
$dbDrive2 = $this->object->addConnection(ConnectionUtil::getUri("b"));
6154
$this->object->addDbDriver($dbDrive1);
6255
$this->object->addDbDriver($dbDrive2);
6356

@@ -71,10 +64,10 @@ public function testAddDbDriverError()
7164
$this->expectException(InvalidArgumentException::class);
7265
$this->expectExceptionMessage("The connection already exists with a different instance");
7366

74-
$dbDrive1 = Factory::getDbInstance(self::URI . "/a");
75-
$dbDrive2 = Factory::getDbInstance(self::URI . "/b");
76-
$dbDrive3 = Factory::getDbInstance(self::URI . "/a");
77-
$dbDrive4 = Factory::getDbInstance(self::URI . "/b");
67+
$dbDrive1 = Factory::getDbInstance(ConnectionUtil::getUri("a"));
68+
$dbDrive2 = Factory::getDbInstance(ConnectionUtil::getUri("b"));
69+
$dbDrive3 = Factory::getDbInstance(ConnectionUtil::getUri("a"));
70+
$dbDrive4 = Factory::getDbInstance(ConnectionUtil::getUri("b"));
7871

7972
$this->object->addDbDriver($dbDrive1);
8073
$this->object->addDbDriver($dbDrive2);
@@ -84,8 +77,8 @@ public function testAddDbDriverError()
8477

8578
public function testAddDbDriver()
8679
{
87-
$dbDrive1 = Factory::getDbInstance(self::URI . "/a");
88-
$dbDrive2 = Factory::getDbInstance(self::URI . "/b");
80+
$dbDrive1 = ConnectionUtil::getConnection("a");
81+
$dbDrive2 = ConnectionUtil::getConnection("b");
8982

9083
$this->object->addDbDriver($dbDrive1);
9184
$this->object->addDbDriver($dbDrive2);
@@ -99,7 +92,7 @@ public function testAddDbDriver()
9992

10093
public function testAddRepository()
10194
{
102-
$dbDriver = Factory::getDbInstance(self::URI . "/a");
95+
$dbDriver = ConnectionUtil::getConnection("a");
10396

10497
$dbDriver->execute('create table users (
10598
id integer primary key auto_increment,
@@ -121,8 +114,8 @@ public function testAddRepository()
121114

122115
public function testBeginTransaction()
123116
{
124-
$this->object->addConnection(self::URI . "/c");
125-
$this->object->addConnection(self::URI . "/d");
117+
$this->object->addConnection(ConnectionUtil::getUri("c"));
118+
$this->object->addConnection(ConnectionUtil::getUri("d"));
126119

127120
$this->object->beginTransaction();
128121
$this->object->commitTransaction();
@@ -135,8 +128,8 @@ public function testBeginTransactionTwice()
135128
$this->expectException(TransactionException::class);
136129
$this->expectExceptionMessage("Transaction Already Started");
137130

138-
$this->object->addConnection(self::URI . "/a");
139-
$this->object->addConnection(self::URI . "/d");
131+
$this->object->addConnection(ConnectionUtil::getUri("a"));
132+
$this->object->addConnection(ConnectionUtil::getUri("d"));
140133

141134
$this->assertEquals(2, $this->object->count());
142135

@@ -149,7 +142,7 @@ public function testRollbackWithNoTransaction()
149142
$this->expectException(TransactionException::class);
150143
$this->expectExceptionMessage("There is no Active Transaction");
151144

152-
$this->object->addConnection(self::URI . "/c");
145+
$this->object->addConnection(ConnectionUtil::getUri("c"));
153146
$this->assertEquals(1, $this->object->count());
154147
$this->object->rollbackTransaction();
155148
}
@@ -159,15 +152,15 @@ public function testCommitWithNoTransaction()
159152
$this->expectException(TransactionException::class);
160153
$this->expectExceptionMessage("There is no Active Transaction");
161154

162-
$this->object->addConnection(self::URI . "/d");
155+
$this->object->addConnection(ConnectionUtil::getUri("d"));
163156
$this->assertEquals(1, $this->object->count());
164157
$this->object->commitTransaction();
165158
}
166159

167160
public function testTransaction()
168161
{
169-
$dbDrive1 = Factory::getDbInstance(self::URI . "/a");
170-
$dbDrive2 = Factory::getDbInstance(self::URI . "/b");
162+
$dbDrive1 = ConnectionUtil::getConnection("a");
163+
$dbDrive2 = ConnectionUtil::getConnection("b");
171164

172165
$dbDrive1->execute('create table users1 (
173166
id integer primary key auto_increment,

0 commit comments

Comments
 (0)