You are here

class SliderProcessorTest in Facets 8

Unit test for processor.

@group facets @coversDefaultClass \Drupal\facets_range_widget\Plugin\facets\processor\SliderProcessor

Hierarchy

Expanded class hierarchy of SliderProcessorTest

File

modules/facets_range_widget/tests/src/Unit/Plugin/processor/SliderProcessorTest.php, line 19

Namespace

Drupal\Tests\facets_range_widget\Unit\Plugin\processor
View source
class SliderProcessorTest extends UnitTestCase {

  /**
   * The processor we're testing.
   *
   * @var \Drupal\facets\Processor\ProcessorInterface
   */
  protected $processor;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->processor = new SliderProcessor([], 'slider_processor', []);
  }

  /**
   * Tests the post query method.
   *
   * @covers ::postQuery
   */
  public function testPostQuery() {
    $widgetconfig = [
      'min_type' => 'foo',
      'step' => 1,
    ];
    $facet = new Facet([], 'facets_facet');
    $facet
      ->setWidget('raw', $widgetconfig);
    $this
      ->configureContainer($widgetconfig);
    $result_lower = new Result($facet, 5, '5', 1);
    $result_higher = new Result($facet, 150, '150', 1);
    $facet
      ->setResults([
      $result_lower,
      $result_higher,
    ]);

    // Process the data.
    $startTime = microtime(TRUE);
    $this->processor
      ->postQuery($facet);
    $new_results = $facet
      ->getResults();
    $stopTime = microtime(TRUE);
    if ($stopTime - $startTime > 1) {
      $this
        ->fail('Test is too slow');
    }
    $this
      ->assertCount(146, $new_results);
    $this
      ->assertEquals(5, $new_results[0]
      ->getRawValue());
    $this
      ->assertEquals(1, $new_results[0]
      ->getCount());
    $this
      ->assertEquals(6, $new_results[1]
      ->getRawValue());
    $this
      ->assertEquals(0, $new_results[1]
      ->getCount());
  }

  /**
   * Tests the post query method with a big dataset.
   *
   * @covers ::postQuery
   */
  public function testPostQueryBigDataSet() {
    $widgetconfig = [
      'min_type' => 'foo',
      'step' => 1,
    ];
    $facet = new Facet([], 'facets_facet');
    $facet
      ->setWidget('raw', $widgetconfig);
    $this
      ->configureContainer($widgetconfig);
    $original_results[] = new Result($facet, 1, 'Small', 5);
    foreach (range(100, 100000, 10) as $k) {
      $original_results[] = new Result($facet, $k, 'result ' . $k, 1);
    }
    $original_results[] = new Result($facet, 150000, 'Big', 5);
    $facet
      ->setResults($original_results);

    // Process the data.
    $startTime = microtime(TRUE);
    $this->processor
      ->postQuery($facet);
    $new_results = $facet
      ->getResults();
    $stopTime = microtime(TRUE);
    if ($stopTime - $startTime > 1) {
      $this
        ->fail('Test is too slow');
    }
    $this
      ->assertCount(150000, $new_results);
  }

  /**
   * Tests the post query method result sorting.
   *
   * @covers ::postQuery
   */
  public function testPostQueryResultSorting() {
    $widgetconfig = [
      'min_type' => 'foo',
      'step' => 1,
    ];
    $facet = new Facet([], 'facets_facet');
    $facet
      ->setWidget('raw', $widgetconfig);
    $this
      ->configureContainer($widgetconfig);
    $original_results = [];
    foreach ([
      10,
      100,
      200,
      5,
    ] as $k) {
      $original_results[] = new Result($facet, $k, 'result ' . $k, 1);
    }
    $facet
      ->setResults($original_results);

    // Process the data.
    $this->processor
      ->postQuery($facet);
    $new_results = $facet
      ->getResults();
    $this
      ->assertCount(196, $new_results);
    $this
      ->assertEquals(5, $new_results[0]
      ->getRawValue());
    $this
      ->assertEquals(200, $new_results[195]
      ->getRawValue());
  }

  /**
   * Adds a regression test for the out of range values.
   */
  public function testOutOfRange() {
    $widgetconfig = [
      'min_type' => 'foo',
      'step' => 7,
    ];
    $facet = new Facet([], 'facets_facet');
    $facet
      ->setWidget('raw', $widgetconfig);
    $this
      ->configureContainer($widgetconfig);
    $result_lower = new Result($facet, 5, '5', 4);
    $result_higher = new Result($facet, 15, '15', 4);
    $facet
      ->setResults([
      $result_lower,
      $result_higher,
    ]);

    // Process the data.
    $this->processor
      ->postQuery($facet);
    $new_results = $facet
      ->getResults();
    $this
      ->assertCount(3, $new_results);
    $this
      ->assertEquals(5, $new_results[0]
      ->getRawValue());
    $this
      ->assertEquals(12, $new_results[1]
      ->getRawValue());
    $this
      ->assertEquals(19, $new_results[2]
      ->getRawValue());
  }

  /**
   * Tests the post query method with fixed min/max.
   *
   * @covers ::postQuery
   */
  public function testPostQueryFixedMinMax() {
    $widgetconfig = [
      'min_type' => 'fixed',
      'min_value' => 10,
      'max_value' => 20,
      'step' => 1,
    ];
    $facet = new Facet([], 'facets_facet');
    $facet
      ->setWidget('raw', $widgetconfig);
    $this
      ->configureContainer($widgetconfig);
    $result_lower = new Result($facet, 5, '5', 1);
    $result_higher = new Result($facet, 150, '150', 1);
    $facet
      ->setResults([
      $result_lower,
      $result_higher,
    ]);

    // Process the data.
    $this->processor
      ->postQuery($facet);
    $new_results = $facet
      ->getResults();
    $this
      ->assertCount(11, $new_results);
  }

  /**
   * Tests the post query method with step > 1.
   *
   * @covers ::postQuery
   */
  public function testPostQueryStep() {
    $widgetconfig = [
      'min_type' => 'foo',
      'step' => 2,
    ];
    $facet = new Facet([], 'facets_facet');
    $facet
      ->setWidget('raw', $widgetconfig);
    $this
      ->configureContainer($widgetconfig);
    $result_lower = new Result($facet, 5, '5', 4);
    $result_higher = new Result($facet, 15, '15', 4);
    $facet
      ->setResults([
      $result_lower,
      $result_higher,
    ]);

    // Process the data.
    $this->processor
      ->postQuery($facet);
    $new_results = $facet
      ->getResults();
    $this
      ->assertCount(6, $new_results);
    $this
      ->assertEquals(5, $new_results[0]
      ->getRawValue());
    $this
      ->assertEquals(4, $new_results[0]
      ->getCount());
    $this
      ->assertEquals(7, $new_results[1]
      ->getRawValue());
    $this
      ->assertEquals(0, $new_results[1]
      ->getCount());
    $this
      ->assertEquals(15, $new_results[5]
      ->getRawValue());
    $this
      ->assertEquals(4, $new_results[5]
      ->getCount());
  }

  /**
   * Configures the container.
   *
   * @param array $config
   *   The config for the widget.
   */
  protected function configureContainer(array $config = []) {
    $widget = $this
      ->prophesize(ArrayWidget::class);
    $widget
      ->getConfiguration()
      ->willReturn($config);
    $pluginManager = $this
      ->prophesize(WidgetPluginManager::class);
    $pluginManager
      ->createInstance('raw', $config)
      ->willReturn($widget
      ->reveal());
    $container = new ContainerBuilder();
    $container
      ->set('plugin.manager.facets.widget', $pluginManager
      ->reveal());
    \Drupal::setContainer($container);
  }

}

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.
SliderProcessorTest::$processor protected property The processor we're testing.
SliderProcessorTest::configureContainer protected function Configures the container.
SliderProcessorTest::setUp public function Overrides UnitTestCase::setUp
SliderProcessorTest::testOutOfRange public function Adds a regression test for the out of range values.
SliderProcessorTest::testPostQuery public function Tests the post query method.
SliderProcessorTest::testPostQueryBigDataSet public function Tests the post query method with a big dataset.
SliderProcessorTest::testPostQueryFixedMinMax public function Tests the post query method with fixed min/max.
SliderProcessorTest::testPostQueryResultSorting public function Tests the post query method result sorting.
SliderProcessorTest::testPostQueryStep public function Tests the post query method with step > 1.
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.