Skip to content

Commit c38a267

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into 4.7
# Conflicts: # utils/phpstan-baseline/empty.notAllowed.neon # utils/phpstan-baseline/loader.neon
2 parents b00e8b9 + d61ec88 commit c38a267

File tree

15 files changed

+244
-50
lines changed

15 files changed

+244
-50
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"phpunit/phpcov": "^9.0.2 || ^10.0",
2929
"phpunit/phpunit": "^10.5.16 || ^11.2",
3030
"predis/predis": "^3.0",
31-
"rector/rector": "2.3.0",
31+
"rector/rector": "2.3.2",
3232
"shipmonk/phpstan-baseline-per-identifier": "^2.0"
3333
},
3434
"replace": {

system/Database/MySQLi/Connection.php

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -207,21 +207,6 @@ public function connect(bool $persistent = false)
207207
$socket,
208208
$clientFlags,
209209
)) {
210-
// Prior to version 5.7.3, MySQL silently downgrades to an unencrypted connection if SSL setup fails
211-
if (($clientFlags & MYSQLI_CLIENT_SSL) !== 0 && version_compare($this->mysqli->client_info, 'mysqlnd 5.7.3', '<=')
212-
&& empty($this->mysqli->query("SHOW STATUS LIKE 'ssl_cipher'")->fetch_object()->Value)
213-
) {
214-
$this->mysqli->close();
215-
$message = 'MySQLi was configured for an SSL connection, but got an unencrypted connection instead!';
216-
log_message('error', $message);
217-
218-
if ($this->DBDebug) {
219-
throw new DatabaseException($message);
220-
}
221-
222-
return false;
223-
}
224-
225210
if (! $this->mysqli->set_charset($this->charset)) {
226211
log_message('error', "Database: Unable to set the configured connection charset ('{$this->charset}').");
227212

@@ -630,8 +615,6 @@ public function insertID(): int
630615
*/
631616
protected function _transBegin(): bool
632617
{
633-
$this->connID->autocommit(false);
634-
635618
return $this->connID->begin_transaction();
636619
}
637620

@@ -640,26 +623,14 @@ protected function _transBegin(): bool
640623
*/
641624
protected function _transCommit(): bool
642625
{
643-
if ($this->connID->commit()) {
644-
$this->connID->autocommit(true);
645-
646-
return true;
647-
}
648-
649-
return false;
626+
return $this->connID->commit();
650627
}
651628

652629
/**
653630
* Rollback Transaction
654631
*/
655632
protected function _transRollback(): bool
656633
{
657-
if ($this->connID->rollback()) {
658-
$this->connID->autocommit(true);
659-
660-
return true;
661-
}
662-
663-
return false;
634+
return $this->connID->rollback();
664635
}
665636
}

system/Database/Seeder.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Seeder
2727
/**
2828
* The name of the database group to use.
2929
*
30-
* @var non-empty-string
30+
* @var non-empty-string|null
3131
*/
3232
protected $DBGroup;
3333

@@ -92,10 +92,16 @@ public function __construct(Database $config, ?BaseConnection $db = null)
9292

9393
$this->config = &$config;
9494

95-
$db ??= Database::connect($this->DBGroup);
96-
97-
$this->db = $db;
98-
$this->forge = Database::forge($this->DBGroup);
95+
if (isset($this->DBGroup)) {
96+
$this->db = Database::connect($this->DBGroup);
97+
$this->forge = Database::forge($this->DBGroup);
98+
} elseif ($db instanceof BaseConnection) {
99+
$this->db = $db;
100+
$this->forge = Database::forge($db);
101+
} else {
102+
$this->db = Database::connect($config->defaultGroup);
103+
$this->forge = Database::forge($config->defaultGroup);
104+
}
99105
}
100106

101107
/**
@@ -145,7 +151,7 @@ public function call(string $class)
145151
}
146152

147153
/** @var Seeder $seeder */
148-
$seeder = new $class($this->config);
154+
$seeder = new $class($this->config, $this->db);
149155
$seeder->setSilent($this->silent)->run();
150156

151157
unset($seeder);

system/Test/CIUnitTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ abstract class CIUnitTestCase extends TestCase
124124
*
125125
* @var string
126126
*/
127-
protected $basePath = SUPPORTPATH . 'Database';
127+
protected $basePath = TESTPATH . '_support/Database';
128128

129129
/**
130130
* The namespace(s) to help us find the migration classes.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\Database\Seeds;
15+
16+
use CodeIgniter\Database\BaseConnection;
17+
use CodeIgniter\Database\Seeder;
18+
19+
/**
20+
* Test seeder with explicit DBGroup set.
21+
*/
22+
class SeederWithDBGroup extends Seeder
23+
{
24+
protected $DBGroup = 'tests';
25+
26+
/**
27+
* Store the connection used during run() for testing.
28+
*/
29+
public static ?BaseConnection $lastConnection = null;
30+
31+
public function run(): void
32+
{
33+
self::$lastConnection = $this->db;
34+
}
35+
36+
/**
37+
* Expose the db connection for testing.
38+
*/
39+
public function getDatabase(): BaseConnection
40+
{
41+
return $this->db;
42+
}
43+
44+
/**
45+
* Reset static state for testing.
46+
*/
47+
public static function reset(): void
48+
{
49+
self::$lastConnection = null;
50+
}
51+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\Database\Seeds;
15+
16+
use CodeIgniter\Database\BaseConnection;
17+
use CodeIgniter\Database\Seeder;
18+
19+
/**
20+
* Test seeder without DBGroup set (should inherit connection).
21+
*/
22+
class SeederWithoutDBGroup extends Seeder
23+
{
24+
/**
25+
* Store the connection used during run() for testing.
26+
*/
27+
public static ?BaseConnection $lastConnection = null;
28+
29+
public function run(): void
30+
{
31+
self::$lastConnection = $this->db;
32+
}
33+
34+
/**
35+
* Expose the db connection for testing.
36+
*/
37+
public function getDatabase(): BaseConnection
38+
{
39+
return $this->db;
40+
}
41+
42+
/**
43+
* Reset static state for testing.
44+
*/
45+
public static function reset(): void
46+
{
47+
self::$lastConnection = null;
48+
}
49+
}

tests/system/Cache/Handlers/AbstractHandlerTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ public function testGetMetaData(): void
3838
$this->handler->save(self::$key1, 'value');
3939

4040
$actual = $this->handler->getMetaData(self::$key1);
41+
$this->assertIsArray($actual);
4142

4243
// This test is time-dependent, and depending on the timing,
4344
// seconds in `$time` (e.g. 12:00:00.9999) and seconds of
4445
// `$this->memcachedHandler->save()` (e.g. 12:00:01.0000)
4546
// may be off by one second. In that case, the following calculation
4647
// will result in maximum of (60 + 1).
4748
$this->assertLessThanOrEqual(60 + 1, $actual['expire'] - $time);
48-
4949
$this->assertLessThanOrEqual(1, $actual['mtime'] - $time);
5050
$this->assertSame('value', $actual['data']);
5151
}

tests/system/Cache/Handlers/PredisHandlerTest.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,12 @@ public function testSave(): void
107107
public function testSavePermanent(): void
108108
{
109109
$this->assertTrue($this->handler->save(self::$key1, 'value', 0));
110-
$metaData = $this->handler->getMetaData(self::$key1);
111110

112-
$this->assertNull($metaData['expire']);
113-
$this->assertLessThanOrEqual(1, $metaData['mtime'] - Time::now()->getTimestamp());
114-
$this->assertSame('value', $metaData['data']);
111+
$metadata = $this->handler->getMetaData(self::$key1);
112+
$this->assertIsArray($metadata);
113+
$this->assertNull($metadata['expire']);
114+
$this->assertLessThanOrEqual(1, $metadata['mtime'] - Time::now()->getTimestamp());
115+
$this->assertSame('value', $metadata['data']);
115116

116117
$this->assertTrue($this->handler->delete(self::$key1));
117118
}
@@ -131,8 +132,11 @@ public function testDeleteMatchingPrefix(): void
131132
$this->handler->save('key_' . $i, 'value' . $i);
132133
}
133134

135+
$cacheInfo = $this->handler->getCacheInfo();
136+
$this->assertIsArray($cacheInfo);
137+
134138
// check that there are 101 items is cache store
135-
$this->assertSame('101', $this->handler->getCacheInfo()['Keyspace']['db0']['keys']);
139+
$this->assertSame('101', $cacheInfo['Keyspace']['db0']['keys']);
136140

137141
// Checking that given the prefix "key_1", deleteMatching deletes 13 keys:
138142
// (key_1, key_10, key_11, key_12, key_13, key_14, key_15, key_16, key_17, key_18, key_19, key_100, key_101)
@@ -149,8 +153,11 @@ public function testDeleteMatchingSuffix(): void
149153
$this->handler->save('key_' . $i, 'value' . $i);
150154
}
151155

156+
$cacheInfo = $this->handler->getCacheInfo();
157+
$this->assertIsArray($cacheInfo);
158+
152159
// check that there are 101 items is cache store
153-
$this->assertSame('101', $this->handler->getCacheInfo()['Keyspace']['db0']['keys']);
160+
$this->assertSame('101', $cacheInfo['Keyspace']['db0']['keys']);
154161

155162
// Checking that given the suffix "1", deleteMatching deletes 11 keys:
156163
// (key_1, key_11, key_21, key_31, key_41, key_51, key_61, key_71, key_81, key_91, key_101)

tests/system/Database/DatabaseSeederTest.php

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,23 @@
1717
use Config\Database;
1818
use Faker\Generator;
1919
use PHPUnit\Framework\Attributes\Group;
20+
use Tests\Support\Database\Seeds\SeederWithDBGroup;
21+
use Tests\Support\Database\Seeds\SeederWithoutDBGroup;
2022

2123
/**
2224
* @internal
2325
*/
24-
#[Group('Others')]
26+
#[Group('DatabaseLive')]
2527
final class DatabaseSeederTest extends CIUnitTestCase
2628
{
29+
protected function tearDown(): void
30+
{
31+
parent::tearDown();
32+
33+
SeederWithDBGroup::reset();
34+
SeederWithoutDBGroup::reset();
35+
}
36+
2737
public function testInstantiateNoSeedPath(): void
2838
{
2939
$this->expectException('InvalidArgumentException');
@@ -57,4 +67,60 @@ public function testCallOnEmptySeeder(): void
5767
$seeder = new Seeder(new Database());
5868
$seeder->call('');
5969
}
70+
71+
public function testSeederWithDBGroupUsesOwnConnection(): void
72+
{
73+
$config = new Database();
74+
$db = Database::connect('tests', false);
75+
76+
$seeder = new SeederWithDBGroup($config, $db);
77+
78+
$testsDb = Database::connect('tests');
79+
$this->assertSame($testsDb, $seeder->getDatabase());
80+
$this->assertNotSame($db, $seeder->getDatabase());
81+
}
82+
83+
public function testSeederWithoutDBGroupUsesPassedConnection(): void
84+
{
85+
$config = new Database();
86+
$db = Database::connect('tests');
87+
88+
$seeder = new SeederWithoutDBGroup($config, $db);
89+
90+
$this->assertSame($db, $seeder->getDatabase());
91+
}
92+
93+
public function testSeederWithoutDBGroupAndNoConnectionUsesDefault(): void
94+
{
95+
$config = new Database();
96+
97+
$seeder = new SeederWithoutDBGroup($config);
98+
99+
$defaultDb = Database::connect($config->defaultGroup);
100+
$this->assertSame($defaultDb, $seeder->getDatabase());
101+
}
102+
103+
public function testCallPassesConnectionToChildSeeder(): void
104+
{
105+
$config = new Database();
106+
$db = Database::connect('tests');
107+
108+
$seeder = new Seeder($config, $db);
109+
$seeder->setSilent(true)->call(SeederWithoutDBGroup::class);
110+
111+
$this->assertSame($db, SeederWithoutDBGroup::$lastConnection);
112+
}
113+
114+
public function testCallChildWithDBGroupUsesOwnConnection(): void
115+
{
116+
$config = new Database();
117+
$db = Database::connect('tests', false);
118+
119+
$seeder = new Seeder($config, $db);
120+
$seeder->setSilent(true)->call(SeederWithDBGroup::class);
121+
122+
$testsDb = Database::connect('tests');
123+
$this->assertSame($testsDb, SeederWithDBGroup::$lastConnection);
124+
$this->assertNotSame($db, SeederWithDBGroup::$lastConnection);
125+
}
60126
}

user_guide_src/source/changelogs/v4.6.5.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Deprecations
3030
Bugs Fixed
3131
**********
3232

33+
- **Database:** Fixed a bug where ``Seeder::call()`` did not pass the database connection to child seeders, causing them to use the default connection instead of the one specified via ``Database::seeder('group')``.
34+
3335
See the repo's
3436
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
3537
for a complete list of bugs fixed.

0 commit comments

Comments
 (0)