You are here

class DependentFacetProcessorTest in Facets 8

Unit test for processor.

@group facets

Hierarchy

Expanded class hierarchy of DependentFacetProcessorTest

File

tests/src/Unit/Plugin/processor/DependentFacetProcessorTest.php, line 18

Namespace

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

  /**
   * An array of results.
   *
   * @var \Drupal\facets\Result\ResultInterface[]
   */
  protected $results;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $facet = new Facet([], 'facets_facet');
    $this->results = [
      new Result($facet, 'snow_owl', 'Snow owl', 2),
      new Result($facet, 'forest_owl', 'Forest owl', 3),
      new Result($facet, 'sand_owl', 'Sand owl', 8),
      new Result($facet, 'church_owl', 'Church owl', 1),
      new Result($facet, 'barn_owl', 'Barn owl', 1),
    ];
  }

  /**
   * Tests to no-config case.
   */
  public function testNotConfigured() {
    $facetManager = $this
      ->prophesize(DefaultFacetManager::class)
      ->reveal();
    $etm = $this
      ->prophesize(EntityTypeManagerInterface::class)
      ->reveal();
    $dfp = new DependentFacetProcessor([], 'dependent_facet_processor', [], $facetManager, $etm);
    $facet = new Facet([
      'id' => 'owl',
      'name' => 'øwl',
    ], 'facets_facet');
    $computed = $dfp
      ->build($facet, $this->results);
    $this
      ->assertEquals($computed, $this->results);
  }

  /**
   * Tests the case where no facets are enabled.
   */
  public function testNoEnabledFacets() {
    $facetManager = $this
      ->prophesize(DefaultFacetManager::class)
      ->reveal();
    $etm = $this
      ->prophesize(EntityTypeManagerInterface::class)
      ->reveal();
    $configuration = [
      'owl' => [
        'enable' => FALSE,
        'condition' => 'not_empty',
      ],
    ];
    $dfp = new DependentFacetProcessor($configuration, 'dependent_facet_processor', [], $facetManager, $etm);
    $facet = new Facet([
      'id' => 'owl',
      'name' => 'øwl',
    ], 'facets_facet');
    $computed = $dfp
      ->build($facet, $this->results);
    $this
      ->assertEquals($computed, $this->results);
  }

  /**
   * Tests that facet is not empty.
   *
   * @dataProvider provideNegated
   */
  public function testNotEmpty($negated) {
    $facet = new Facet([
      'id' => 'owl',
      'name' => 'øwl',
    ], 'facets_facet');
    $facet
      ->setActiveItem('snow_owl');
    $facetManager = $this
      ->prophesize(DefaultFacetManager::class);
    $facetManager
      ->returnProcessedFacet($facet)
      ->willReturn($facet);
    $entityStorage = $this
      ->prophesize(EntityStorageInterface::class);
    $entityStorage
      ->load('owl')
      ->willReturn($facet);
    $etm = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $etm
      ->getStorage('facets_facet')
      ->willReturn($entityStorage
      ->reveal());
    $configuration = [
      'owl' => [
        'enable' => TRUE,
        'negate' => $negated,
        'condition' => 'not_empty',
      ],
    ];
    $dfp = new DependentFacetProcessor($configuration, 'dependent_facet_processor', [], $facetManager
      ->reveal(), $etm
      ->reveal());
    $computed = $dfp
      ->build($facet, $this->results);
    if ($negated) {
      $this
        ->assertEquals($computed, []);
    }
    else {
      $this
        ->assertEquals($computed, $this->results);
    }
  }

  /**
   * Provides test cases with data.
   *
   * @return array
   *   An array of test data.
   */
  public function provideNegated() {
    return [
      'negated' => [
        TRUE,
      ],
      'normal' => [
        FALSE,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependentFacetProcessorTest::$results protected property An array of results.
DependentFacetProcessorTest::provideNegated public function Provides test cases with data.
DependentFacetProcessorTest::setUp public function Overrides UnitTestCase::setUp
DependentFacetProcessorTest::testNoEnabledFacets public function Tests the case where no facets are enabled.
DependentFacetProcessorTest::testNotConfigured public function Tests to no-config case.
DependentFacetProcessorTest::testNotEmpty public function Tests that facet is not empty.
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.