TermWeightWidgetOrderProcessorTest.php in Facets 8
File
tests/src/Unit/Plugin/processor/TermWeightWidgetOrderProcessorTest.php
View source
<?php
namespace Drupal\Tests\facets\Unit\Plugin\processor;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\processor\TermWeightWidgetOrderProcessor;
use Drupal\facets\Result\Result;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\UnitTestCase;
class TermWeightWidgetOrderProcessorTest extends UnitTestCase {
protected $processor;
protected $originalResults;
protected $entityTypeManager;
protected $termA;
protected $termB;
protected $termStorage;
protected function setUp() {
parent::setUp();
$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);
$this->processor = new TermWeightWidgetOrderProcessor([], 'term_weight_widget_order', [], $this->entityTypeManager);
$this->termA = $this
->getMockBuilder(Term::class)
->disableOriginalConstructor()
->getMock();
$this->termB = $this
->getMockBuilder(Term::class)
->disableOriginalConstructor()
->getMock();
$terms = [
1 => $this->termA,
2 => $this->termB,
];
$this->termStorage
->expects($this
->any())
->method('loadMultiple')
->willReturn($terms);
$facet = new Facet([], 'facets_facet');
$this->originalResults = [
new Result($facet, 1, 10, 100),
new Result($facet, 2, 20, 200),
];
}
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);
}
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);
}
public function testLow() {
$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
->assertLessThan(0, $sort_value);
}
}