3 namespace Drupal\KernelTests\Core\EventSubscriber;
5 use Drupal\Core\Database\Database;
6 use Drupal\Core\EventSubscriber\ReplicaDatabaseIgnoreSubscriber;
7 use Drupal\Core\DrupalKernel;
8 use Drupal\KernelTests\KernelTestBase;
9 use Symfony\Component\HttpFoundation\Request;
10 use Symfony\Component\HttpKernel\HttpKernelInterface;
11 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
14 * Tests that ReplicaDatabaseIgnoreSubscriber functions correctly.
18 class IgnoreReplicaSubscriberTest extends KernelTestBase {
21 * Tests \Drupal\Core\EventSubscriber\ReplicaDatabaseIgnoreSubscriber::checkReplicaServer().
23 public function testSystemInitIgnoresSecondaries() {
24 // Clone the master credentials to a replica connection.
25 // Note this will result in two independent connection objects that happen
26 // to point to the same place.
27 $connection_info = Database::getConnectionInfo('default');
28 Database::addConnectionInfo('default', 'replica', $connection_info['default']);
31 $class_loader = require $this->root . '/autoload.php';
32 $kernel = new DrupalKernel('testing', $class_loader, FALSE);
33 $event = new GetResponseEvent($kernel, Request::create('http://example.com'), HttpKernelInterface::MASTER_REQUEST);
34 $subscriber = new ReplicaDatabaseIgnoreSubscriber();
35 $subscriber->checkReplicaServer($event);
37 $db1 = Database::getConnection('default', 'default');
38 $db2 = Database::getConnection('replica', 'default');
40 $this->assertSame($db1, $db2, 'System Init ignores secondaries when requested.');