IgnoreReplicaSubscriberTest.php 1.6 KB

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