You are here

class CountLimitProcessorTest in Facets 8

Unit test for processor.

@group facets

Hierarchy

Expanded class hierarchy of CountLimitProcessorTest

File

tests/src/Unit/Plugin/processor/CountLimitProcessorTest.php, line 17

Namespace

Drupal\Tests\facets\Unit\Plugin\processor
View source
class CountLimitProcessorTest extends UnitTestCase {

  /**
   * The processor to be tested.
   *
   * @var \Drupal\facets\processor\BuildProcessorInterface
   */
  protected $processor;

  /**
   * An array containing the results before the processor has ran.
   *
   * @var \Drupal\facets\Result\Result[]
   */
  protected $originalResults;

  /**
   * Creates a new processor object for use in the tests.
   */
  protected function setUp() {
    parent::setUp();
    $facet = new Facet([], 'facets_facet');
    $this->originalResults = [
      new Result($facet, 'llama', 'llama', 10),
      new Result($facet, 'badger', 'badger', 5),
      new Result($facet, 'duck', 'duck', 15),
    ];
    $processor_id = 'count_limit';
    $this->processor = new CountLimitProcessor([], $processor_id, []);
    $processor_definitions = [
      $processor_id => [
        'id' => $processor_id,
        'class' => CountLimitProcessor::class,
      ],
    ];
    $manager = $this
      ->getMockBuilder(ProcessorPluginManager::class)
      ->disableOriginalConstructor()
      ->getMock();
    $manager
      ->expects($this
      ->any())
      ->method('getDefinitions')
      ->willReturn($processor_definitions);
    $manager
      ->expects($this
      ->any())
      ->method('createInstance')
      ->willReturn($this->processor);
    $container_builder = new ContainerBuilder();
    $container_builder
      ->set('plugin.manager.facets.processor', $manager);
    \Drupal::setContainer($container_builder);
  }

  /**
   * Tests no filtering happens.
   */
  public function testNoFilter() {
    $facet = new Facet([], 'facets_facet');
    $facet
      ->setResults($this->originalResults);
    $facet
      ->addProcessor([
      'processor_id' => 'count_limit',
      'weights' => [],
      'settings' => [
        'minimum_items' => 4,
      ],
    ]);
    $this->processor
      ->setConfiguration([
      'minimum_items' => 4,
    ]);
    $sorted_results = $this->processor
      ->build($facet, $this->originalResults);
    $this
      ->assertCount(3, $sorted_results);
    $this
      ->assertEquals('llama', $sorted_results[0]
      ->getDisplayValue());
    $this
      ->assertEquals('badger', $sorted_results[1]
      ->getDisplayValue());
    $this
      ->assertEquals('duck', $sorted_results[2]
      ->getDisplayValue());
  }

  /**
   * Tests no filtering happens.
   */
  public function testMinEqualsValue() {
    $facet = new Facet([], 'facets_facet');
    $facet
      ->setResults($this->originalResults);
    $facet
      ->addProcessor([
      'processor_id' => 'count_limit',
      'weights' => [],
      'settings' => [
        'minimum_items' => 5,
      ],
    ]);
    $this->processor
      ->setConfiguration([
      'minimum_items' => 5,
    ]);
    $sorted_results = $this->processor
      ->build($facet, $this->originalResults);
    $this
      ->assertCount(3, $sorted_results);
    $this
      ->assertEquals('llama', $sorted_results[0]
      ->getDisplayValue());
    $this
      ->assertEquals('badger', $sorted_results[1]
      ->getDisplayValue());
    $this
      ->assertEquals('duck', $sorted_results[2]
      ->getDisplayValue());
  }

  /**
   * Tests between minimum and maximum values.
   */
  public function testBetweenMinAndMaxValue() {
    $facet = new Facet([], 'facets_facet');
    $facet
      ->setResults($this->originalResults);
    $facet
      ->addProcessor([
      'processor_id' => 'count_limit',
      'weights' => [],
      'settings' => [],
    ]);
    $this->processor
      ->setConfiguration([
      'minimum_items' => 6,
      'maximum_items' => 14,
    ]);
    $sorted_results = $this->processor
      ->build($facet, $this->originalResults);
    $this
      ->assertCount(1, $sorted_results);
    $this
      ->assertEquals('llama', $sorted_results[0]
      ->getDisplayValue());
    $this->processor
      ->setConfiguration([
      'minimum_items' => 60,
      'maximum_items' => 140,
    ]);
    $sorted_results = $this->processor
      ->build($facet, $this->originalResults);
    $this
      ->assertCount(0, $sorted_results);
    $this->processor
      ->setConfiguration([
      'minimum_items' => 1,
      'maximum_items' => 10,
    ]);
    $sorted_results = $this->processor
      ->build($facet, $this->originalResults);
    $this
      ->assertCount(2, $sorted_results);
  }

  /**
   * Tests maximum values.
   */
  public function testMaxValue() {
    $facet = new Facet([], 'facets_facet');
    $facet
      ->setResults($this->originalResults);
    $facet
      ->addProcessor([
      'processor_id' => 'count_limit',
      'weights' => [],
      'settings' => [],
    ]);
    $this->processor
      ->setConfiguration([
      'maximum_items' => 14,
    ]);
    $sorted_results = $this->processor
      ->build($facet, $this->originalResults);
    $this
      ->assertCount(2, $sorted_results);
    $this
      ->assertEquals('llama', $sorted_results[0]
      ->getDisplayValue());
    $this
      ->assertEquals('badger', $sorted_results[1]
      ->getDisplayValue());
    $this->processor
      ->setConfiguration([
      'maximum_items' => 140,
    ]);
    $sorted_results = $this->processor
      ->build($facet, $this->originalResults);
    $this
      ->assertCount(3, $sorted_results);
    $this
      ->assertEquals('llama', $sorted_results[0]
      ->getDisplayValue());
    $this
      ->assertEquals('badger', $sorted_results[1]
      ->getDisplayValue());
    $this
      ->assertEquals('duck', $sorted_results[2]
      ->getDisplayValue());
    $this->processor
      ->setConfiguration([
      'maximum_items' => 1,
    ]);
    $sorted_results = $this->processor
      ->build($facet, $this->originalResults);
    $this
      ->assertCount(0, $sorted_results);
  }

  /**
   * Tests filtering of results.
   */
  public function testFilterResults() {
    $facet = new Facet([], 'facets_facet');
    $facet
      ->setResults($this->originalResults);
    $facet
      ->addProcessor([
      'processor_id' => 'count_limit',
      'weights' => [],
      'settings' => [
        'minimum_items' => 8,
      ],
    ]);
    $this->processor
      ->setConfiguration([
      'minimum_items' => 8,
    ]);
    $sorted_results = $this->processor
      ->build($facet, $this->originalResults);
    $this
      ->assertCount(2, $sorted_results);
    $this
      ->assertEquals('llama', $sorted_results[0]
      ->getDisplayValue());
    $this
      ->assertEquals('duck', $sorted_results[2]
      ->getDisplayValue());
  }

  /**
   * Tests configuration.
   */
  public function testConfiguration() {
    $config = $this->processor
      ->defaultConfiguration();
    $this
      ->assertEquals([
      'minimum_items' => 1,
      'maximum_items' => 0,
    ], $config);
  }

  /**
   * Tests testDescription().
   */
  public function testDescription() {
    $this
      ->assertEquals('', $this->processor
      ->getDescription());
  }

  /**
   * Tests isHidden().
   */
  public function testIsHidden() {
    $this
      ->assertEquals(FALSE, $this->processor
      ->isHidden());
  }

  /**
   * Tests isLocked().
   */
  public function testIsLocked() {
    $this
      ->assertEquals(FALSE, $this->processor
      ->isLocked());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CountLimitProcessorTest::$originalResults protected property An array containing the results before the processor has ran.
CountLimitProcessorTest::$processor protected property The processor to be tested.
CountLimitProcessorTest::setUp protected function Creates a new processor object for use in the tests. Overrides UnitTestCase::setUp
CountLimitProcessorTest::testBetweenMinAndMaxValue public function Tests between minimum and maximum values.
CountLimitProcessorTest::testConfiguration public function Tests configuration.
CountLimitProcessorTest::testDescription public function Tests testDescription().
CountLimitProcessorTest::testFilterResults public function Tests filtering of results.
CountLimitProcessorTest::testIsHidden public function Tests isHidden().
CountLimitProcessorTest::testIsLocked public function Tests isLocked().
CountLimitProcessorTest::testMaxValue public function Tests maximum values.
CountLimitProcessorTest::testMinEqualsValue public function Tests no filtering happens.
CountLimitProcessorTest::testNoFilter public function Tests no filtering happens.
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.