You are here

class RangeSliderProcessorTest in Facets 8

Unit test for processor.

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

Hierarchy

Expanded class hierarchy of RangeSliderProcessorTest

File

modules/facets_range_widget/tests/src/Unit/Plugin/processor/RangeSliderProcessorTest.php, line 24

Namespace

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

  /**
   * The processor we're testing.
   *
   * @var \Drupal\facets_range_widget\Plugin\facets\processor\RangeSliderProcessor
   */
  protected $processor;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->processor = new RangeSliderProcessor([], 'range_slider', []);
    $facets_url_generator = $this
      ->prophesize(FacetsUrlGenerator::class);
    $facets_url_generator
      ->getUrl(Argument::any(), Argument::any())
      ->willReturn(new Url('test', [], [
      'query' => [
        'f' => [
          'animals::(min:__range_slider_min__,max:__range_slider_max__)',
        ],
      ],
    ]));
    $url_generator = $this
      ->prophesize(UrlGeneratorInterface::class);
    $container = new ContainerBuilder();
    $container
      ->set('url_generator', $url_generator
      ->reveal());
    $container
      ->set('facets.utility.url_generator', $facets_url_generator
      ->reveal());
    \Drupal::setContainer($container);
  }

  /**
   * Tests the pre query method.
   *
   * @covers ::preQuery
   */
  public function testPreQuery() {
    $facet = new Facet([
      'id' => 'llama',
    ], 'facets_facet');
    $facet
      ->setActiveItems([
      '(min:2,max:10)',
    ]);
    $this->processor
      ->preQuery($facet);
    $this
      ->assertCount(2, $facet
      ->getActiveItems()[0]);
    $this
      ->assertEquals([
      2,
      10,
    ], $facet
      ->getActiveItems()[0]);
  }

  /**
   * Tests the build method.
   *
   * @covers ::build
   */
  public function testBuild() {

    // Create the Url processor.
    $queryString = $this
      ->prophesize(QueryString::class);
    $queryString
      ->getFilterKey()
      ->willReturn('f');
    $queryString
      ->getSeparator()
      ->willReturn('::');
    $queryString
      ->getActiveFilters()
      ->willReturn([]);
    $urlHandler = $this
      ->prophesize(UrlProcessorHandler::class);
    $urlHandler
      ->getProcessor()
      ->willReturn($queryString
      ->reveal());
    $facet = $this
      ->prophesize(Facet::class);
    $facet
      ->getProcessors()
      ->willReturn([
      'url_processor_handler' => $urlHandler
        ->reveal(),
    ]);
    $facet
      ->getUrlAlias()
      ->willReturn('animals');
    $facet
      ->id()
      ->willReturn('animals');

    /** @var \Drupal\facets\Result\ResultInterface[] $results */
    $results = [
      new Result($facet
        ->reveal(), 1, 1, 1),
      new Result($facet
        ->reveal(), 5, 5, 5),
    ];
    $results[0]
      ->setUrl(new Url('test'));
    $results[1]
      ->setUrl(new Url('test'));
    $new_results = $this->processor
      ->build($facet
      ->reveal(), $results);
    $this
      ->assertCount(2, $new_results);
    $params = UrlHelper::buildQuery([
      'f' => [
        'animals::(min:__range_slider_min__,max:__range_slider_max__)',
      ],
    ]);
    $expected_route = 'route:test?' . $params;
    $this
      ->assertEquals($expected_route, $new_results[0]
      ->getUrl()
      ->toUriString());
    $this
      ->assertEquals($expected_route, $new_results[1]
      ->getUrl()
      ->toUriString());
  }

}

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.
RangeSliderProcessorTest::$processor protected property The processor we're testing.
RangeSliderProcessorTest::setUp public function Overrides UnitTestCase::setUp
RangeSliderProcessorTest::testBuild public function Tests the build method.
RangeSliderProcessorTest::testPreQuery public function Tests the pre query method.
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.