You are here

class TermWeightWidgetOrderProcessorTest in Facets 8

Unit test for processor.

@group facets

Hierarchy

Expanded class hierarchy of TermWeightWidgetOrderProcessorTest

File

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

Namespace

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

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

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

  /**
   * The mocked entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityTypeManager;

  /**
   * Mocked term, used for comparison.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $termA;

  /**
   * Mocked term, used for comparison.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $termB;

  /**
   * Mocked entity (term) storage.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $termStorage;

  /**
   * Creates a new processor object for use in the tests.
   */
  protected function setUp() {
    parent::setUp();

    // Build up a chain of mocks that we will have the processor use to fetch
    // the weight of the terms that are being compared.
    $this->termStorage = $this
      ->getMockBuilder(EntityStorageInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->entityTypeManager = $this
      ->getMockBuilder(EntityTypeManagerInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->entityTypeManager
      ->expects($this
      ->any())
      ->method('getStorage')
      ->willReturn($this->termStorage);

    // Instantiate the processor and load it up with our mock chain.
    $this->processor = new TermWeightWidgetOrderProcessor([], 'term_weight_widget_order', [], $this->entityTypeManager);

    // Setup two mock terms that will be set up to have specific weights before
    // the processor is used to compare them.
    // The mocks are used in the individual tests.
    $this->termA = $this
      ->getMockBuilder(Term::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->termB = $this
      ->getMockBuilder(Term::class)
      ->disableOriginalConstructor()
      ->getMock();

    // Prepare the terms that will be returned when the processor loads its list
    // of term-ids from the Results raw values.
    $terms = [
      1 => $this->termA,
      2 => $this->termB,
    ];

    // Setup the termStorage mock to return our terms. As we keep a reference to
    // the terms via $this the individual tests can set up the weights later.
    $this->termStorage
      ->expects($this
      ->any())
      ->method('loadMultiple')
      ->willReturn($terms);

    // Prepare the results that we use the processor to sort, the raw_value has
    // to match the term_id keys used above in $terms. Display_value and count
    // is not used.
    $facet = new Facet([], 'facets_facet');
    $this->originalResults = [
      new Result($facet, 1, 10, 100),
      new Result($facet, 2, 20, 200),
    ];
  }

  /**
   * Tests that sorting two terms of equal weight yields 0.
   */
  public function testEqual() {
    $this->termA
      ->expects($this
      ->any())
      ->method('getWeight')
      ->willReturn('1');
    $this->termB
      ->expects($this
      ->any())
      ->method('getWeight')
      ->willReturn('1');
    $sort_value = $this->processor
      ->sortResults($this->originalResults[0], $this->originalResults[1]);
    $this
      ->assertEquals(0, $sort_value);
  }

  /**
   * Compare a term with a high weight with a term with a low.
   */
  public function testHigher() {
    $this->termA
      ->expects($this
      ->any())
      ->method('getWeight')
      ->willReturn('10');
    $this->termB
      ->expects($this
      ->any())
      ->method('getWeight')
      ->willReturn('-10');
    $sort_value = $this->processor
      ->sortResults($this->originalResults[0], $this->originalResults[1]);
    $this
      ->assertGreaterThan(0, $sort_value);
  }

  /**
   * Compare a term with a low weight with a term with a high.
   */
  public function testLow() {
    $this->termA
      ->expects($this
      ->any())
      ->method('getWeight')
      ->willReturn('-10');
    $this->termB
      ->expects($this
      ->any())
      ->method('getWeight')
      ->willReturn('10');

    // Compare the two values and check the result with an assertion.
    $sort_value = $this->processor
      ->sortResults($this->originalResults[0], $this->originalResults[1]);
    $this
      ->assertLessThan(0, $sort_value);
  }

}

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.
TermWeightWidgetOrderProcessorTest::$entityTypeManager protected property The mocked entity type manager.
TermWeightWidgetOrderProcessorTest::$originalResults protected property An array containing the results before the processor has ran.
TermWeightWidgetOrderProcessorTest::$processor protected property The processor to be tested.
TermWeightWidgetOrderProcessorTest::$termA protected property Mocked term, used for comparison.
TermWeightWidgetOrderProcessorTest::$termB protected property Mocked term, used for comparison.
TermWeightWidgetOrderProcessorTest::$termStorage protected property Mocked entity (term) storage.
TermWeightWidgetOrderProcessorTest::setUp protected function Creates a new processor object for use in the tests. Overrides UnitTestCase::setUp
TermWeightWidgetOrderProcessorTest::testEqual public function Tests that sorting two terms of equal weight yields 0.
TermWeightWidgetOrderProcessorTest::testHigher public function Compare a term with a high weight with a term with a low.
TermWeightWidgetOrderProcessorTest::testLow public function Compare a term with a low weight with a term with a high.
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.