You are here

abstract class WidgetTestBase in Facets 8

Base class for widget unit tests.

Hierarchy

Expanded class hierarchy of WidgetTestBase

1 file declares its use of WidgetTestBase
SliderWidgetTest.php in modules/facets_range_widget/tests/src/Unit/Plugin/widget/SliderWidgetTest.php

File

tests/src/Unit/Plugin/widget/WidgetTestBase.php, line 18

Namespace

Drupal\Tests\facets\Unit\Plugin\widget
View source
abstract class WidgetTestBase extends UnitTestCase {

  /**
   * The widget to be tested.
   *
   * @var \Drupal\facets\Widget\WidgetPluginInterface
   */
  protected $widget;

  /**
   * The facet used for the widget test.
   *
   * @var \Drupal\facets\FacetInterface
   */
  protected $facet;

  /**
   * An array containing the results for the widget.
   *
   * @var \Drupal\facets\Result\Result[]
   */
  protected $originalResults;

  /**
   * An array of possible query types.
   *
   * @var string[]
   */
  protected $queryTypes;

  /**
   * Sets up the container and other variables used in all the tests.
   */
  protected function setUp() {
    parent::setUp();
    $facet = new Facet([], 'facets_facet');
    $this->facet = $facet;

    /** @var \Drupal\facets\Result\Result[] $original_results */
    $original_results = [
      new Result($facet, 'llama', 'Llama', 10),
      new Result($facet, 'badger', 'Badger', 20),
      new Result($facet, 'duck', 'Duck', 15),
      new Result($facet, 'alpaca', 'Alpaca', 9),
    ];
    foreach ($original_results as $original_result) {
      $original_result
        ->setUrl(new Url('test'));
    }
    $this->originalResults = $original_results;

    // Create a container, so we can access string translation.
    $string_translation = $this
      ->prophesize(TranslationInterface::class);
    $url_generator = $this
      ->prophesize(UrlGeneratorInterface::class);
    $widget_manager = $this
      ->prophesize(WidgetPluginManager::class);
    $container = new ContainerBuilder();
    $container
      ->set('plugin.manager.facets.widget', $widget_manager
      ->reveal());
    $container
      ->set('string_translation', $string_translation
      ->reveal());
    $container
      ->set('url_generator', $url_generator
      ->reveal());
    \Drupal::setContainer($container);
    $this->queryTypes = [
      'date' => 'date',
      'string' => 'string',
      'numeric' => 'numeric',
      'range' => 'range',
    ];
  }

  /**
   * Tests default configuration.
   */
  public function testDefaultConfiguration() {
    $default_config = $this->widget
      ->defaultConfiguration();
    $this
      ->assertEquals([
      'show_numbers' => FALSE,
      'soft_limit' => 0,
    ], $default_config);
  }

  /**
   * Tests get query type.
   */
  public function testGetQueryType() {
    $result = $this->widget
      ->getQueryType($this->queryTypes);
    $this
      ->assertEquals(NULL, $result);
  }

  /**
   * Tests default for required properties.
   */
  public function testIsPropertyRequired() {
    $this
      ->assertFalse($this->widget
      ->isPropertyRequired('llama', 'owl'));
  }

  /**
   * Build a formattable markup object to use as assertion.
   *
   * @param string $text
   *   Text to display.
   * @param string $raw_value
   *   Raw value of the result.
   * @param \Drupal\facets\FacetInterface $facet
   *   The facet.
   * @param int $count
   *   Number of results.
   * @param bool $active
   *   Link is active.
   * @param bool $show_numbers
   *   Numbers are displayed.
   *
   * @return array
   *   A render array.
   */
  protected function buildLinkAssertion($text, $raw_value, FacetInterface $facet, $count = 0, $active = FALSE, $show_numbers = TRUE) {
    return [
      '#theme' => 'facets_result_item',
      '#raw_value' => $raw_value,
      '#facet' => $facet,
      '#value' => $text,
      '#show_count' => $show_numbers && $count !== NULL,
      '#count' => $count,
      '#is_active' => $active,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
WidgetTestBase::$facet protected property The facet used for the widget test.
WidgetTestBase::$originalResults protected property An array containing the results for the widget.
WidgetTestBase::$queryTypes protected property An array of possible query types.
WidgetTestBase::$widget protected property The widget to be tested.
WidgetTestBase::buildLinkAssertion protected function Build a formattable markup object to use as assertion.
WidgetTestBase::setUp protected function Sets up the container and other variables used in all the tests. Overrides UnitTestCase::setUp 5
WidgetTestBase::testDefaultConfiguration public function Tests default configuration. 5
WidgetTestBase::testGetQueryType public function Tests get query type. 1
WidgetTestBase::testIsPropertyRequired public function Tests default for required properties. 1