You are here

SolrBackendTrait.php in Search API Solr 8.3

File

tests/src/Kernel/Processor/SolrBackendTrait.php
View source
<?php

namespace Drupal\Tests\search_api_solr\Kernel\Processor;

use Drupal\search_api\Entity\Server;
use Drupal\search_api_solr\Utility\SolrCommitTrait;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Symfony\Component\Yaml\Yaml;
defined('TRAVIS_BUILD_DIR') || define('TRAVIS_BUILD_DIR', getenv('TRAVIS_BUILD_DIR') ?: '.');
defined('SOLR_CLOUD') || define('SOLR_CLOUD', getenv('SOLR_CLOUD') ?: 'false');

/**
 * Helper to exchange the DB backend for a Solr backend in processor tests.
 */
trait SolrBackendTrait {

  /**
   * @var \Psr\Log\LoggerInterface
   */
  protected $travisLogger;
  use SolrCommitTrait;

  /**
   * Swap the DB backend for a Solr backend.
   *
   * This function has to be called from the test setUp() function.
   */
  protected function enableSolrServer() {
    $this
      ->installConfig([
      'devel',
    ]);
    $config = '/config/install/search_api.server.solr_search_server' . ('true' === SOLR_CLOUD ? '_cloud' : '') . '.yml';
    $this->server = Server::create(Yaml::parse(file_get_contents(drupal_get_path('module', 'search_api_solr_test') . $config)));
    $this->server
      ->save();
    $this->index
      ->setServer($this->server);
    $this->index
      ->save();
    $index_storage = $this->container
      ->get('entity_type.manager')
      ->getStorage('search_api_index');
    $index_storage
      ->resetCache([
      $this->index
        ->id(),
    ]);
    $this->index = $index_storage
      ->load($this->index
      ->id());
    $logger = new Logger('search_api_solr');
    $logger
      ->pushHandler(new StreamHandler(TRAVIS_BUILD_DIR . '/solr.query.log', Logger::DEBUG));
    \Drupal::service('search_api_solr_devel.solarium_request_logger')
      ->setLogger($logger);
  }

  /**
   * {@inheritdoc}
   */
  protected function indexItems() {
    $index_status = parent::indexItems();
    $this
      ->ensureCommit($this->index);
    return $index_status;
  }

  /**
   * {@inheritdoc}
   */
  protected function tearDown() {
    $this->index
      ->clear();
    $this
      ->ensureCommit($this->index);
    parent::tearDown();
  }

}

Traits

Namesort descending Description
SolrBackendTrait Helper to exchange the DB backend for a Solr backend in processor tests.