|
|
@@ -18,6 +18,7 @@ namespace Cake\Test\TestCase\ORM\Association;
|
|
|
|
|
|
use Cake\Database\Connection;
|
|
|
use Cake\Database\Driver;
|
|
|
+use Cake\Database\Driver\Sqlite;
|
|
|
use Cake\Database\Exception\DatabaseException;
|
|
|
use Cake\Database\Expression\OrderClauseExpression;
|
|
|
use Cake\Database\Expression\QueryExpression;
|
|
|
@@ -25,6 +26,7 @@ use Cake\Datasource\ConnectionManager;
|
|
|
use Cake\Datasource\EntityInterface;
|
|
|
use Cake\Event\EventInterface;
|
|
|
use Cake\I18n\DateTime;
|
|
|
+use Cake\Log\Log;
|
|
|
use Cake\ORM\Association\BelongsTo;
|
|
|
use Cake\ORM\Association\BelongsToMany;
|
|
|
use Cake\ORM\Association\HasMany;
|
|
|
@@ -102,6 +104,13 @@ class BelongsToManyTest extends TestCase
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
+ public function tearDown(): void
|
|
|
+ {
|
|
|
+ parent::tearDown();
|
|
|
+ ConnectionManager::drop('test_read_write');
|
|
|
+ Log::drop('queries');
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Tests setForeignKey()
|
|
|
*/
|
|
|
@@ -1702,4 +1711,69 @@ class BelongsToManyTest extends TestCase
|
|
|
// 4 records in the junction table.
|
|
|
$this->assertCount(4, $results);
|
|
|
}
|
|
|
+
|
|
|
+ public function testEagerLoaderConnectionRole(): void
|
|
|
+ {
|
|
|
+ $this->skipIf(!extension_loaded('pdo_sqlite'), 'Skipping as SQLite extension is missing');
|
|
|
+
|
|
|
+ Log::setConfig('queries', [
|
|
|
+ 'className' => 'Array',
|
|
|
+ 'scopes' => ['queriesLog'],
|
|
|
+ ]);
|
|
|
+
|
|
|
+ ConnectionManager::setConfig('test_read_write', [
|
|
|
+ 'className' => Connection::class,
|
|
|
+ 'driver' => Sqlite::class,
|
|
|
+ 'write' => [
|
|
|
+ 'database' => ':memory:',
|
|
|
+ 'cached' => 'shared', // used so role configs are unique
|
|
|
+ 'log' => true,
|
|
|
+ ],
|
|
|
+ 'read' => [
|
|
|
+ 'database' => ':memory:',
|
|
|
+ 'log' => true,
|
|
|
+ ],
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $connection = ConnectionManager::get('test_read_write');
|
|
|
+ $this->assertNotSame($connection->getDriver(Connection::ROLE_READ), $connection->getDriver(Connection::ROLE_WRITE));
|
|
|
+
|
|
|
+ // Create belongs to many relationships with unique table names
|
|
|
+ $driver = $connection->getDriver(Connection::ROLE_WRITE);
|
|
|
+ $driver->execute('CREATE TABLE unique_items (id int PRIMARY KEY);');
|
|
|
+ $driver->execute('CREATE TABLE articles (id int PRIMARY KEY);');
|
|
|
+ $driver->execute('CREATE TABLE articles_unique_items (unique_item_id int, article_id int);');
|
|
|
+
|
|
|
+ $driver = $connection->getDriver(Connection::ROLE_READ);
|
|
|
+ $driver->execute('CREATE TABLE unique_items (id int PRIMARY KEY);');
|
|
|
+ $driver->execute('CREATE TABLE articles (id int PRIMARY KEY);');
|
|
|
+ $driver->execute('CREATE TABLE articles_unique_items (unique_item_id int, article_id int);');
|
|
|
+ $driver->execute('INSERT INTO unique_items (id) VALUES (1)');
|
|
|
+ $driver->execute('INSERT INTO articles (id) VALUES (1)');
|
|
|
+ $driver->execute('INSERT INTO articles_unique_items (unique_item_id, article_id) VALUES (1, 1)');
|
|
|
+
|
|
|
+ $articles = $this->getTableLocator()->get('Articles')->setConnection($connection);
|
|
|
+ $articles->belongsToMany('UniqueItems')->getTarget()->setConnection($connection);
|
|
|
+
|
|
|
+ $query = $articles->find();
|
|
|
+ $this->assertSame(Connection::ROLE_WRITE, $query->getConnectionRole(), 'This test assumes select queries still default to write role');
|
|
|
+
|
|
|
+ $results = $query->contain('UniqueItems')->useReadRole()->toArray();
|
|
|
+ $this->assertCount(1, $results);
|
|
|
+ $this->assertCount(1, $results[0]->unique_items);
|
|
|
+ $this->assertSame(1, $results[0]->unique_items[0]->id);
|
|
|
+
|
|
|
+ $logs = Log::engine('queries')->read();
|
|
|
+ $this->assertNotEmpty($logs);
|
|
|
+
|
|
|
+ foreach ($logs as $log) {
|
|
|
+ if (
|
|
|
+ str_contains($log, 'FROM articles') ||
|
|
|
+ str_contains($log, 'FROM articles_unique_items') ||
|
|
|
+ str_contains($log, 'FROM unique_items')
|
|
|
+ ) {
|
|
|
+ $this->assertStringContainsString('role=read', $log);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|