class SearchApiSortsManagerTest in Search API sorts 8
Tests the sorts manager.
@group search_api_sorts
@coversDefaultClass \Drupal\search_api_sorts\SearchApiSortsManager
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\search_api_sorts\Unit\SearchApiSortsManagerTest
Expanded class hierarchy of SearchApiSortsManagerTest
File
- tests/
src/ Unit/ SearchApiSortsManagerTest.php, line 25
Namespace
Drupal\Tests\search_api_sorts\UnitView source
class SearchApiSortsManagerTest extends UnitTestCase {
/**
* ModuleHandler object.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
private $moduleHandler;
/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
private $entityTypeManagerProphecy;
/**
* A display object.
*
* @var \Drupal\search_api\Display\DisplayInterface
*/
private $display;
/**
* A request object to use for each test case.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
private $request;
/**
* A request stack object to store requests.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
private $requestStack;
/**
* The search API Display manager.
*
* @var \Drupal\search_api\Display\DisplayPluginManagerInterface
*/
private $searchApiDisplayManager;
/**
* {@inheritdoc}
*/
public function setUp() : void {
parent::setUp();
$this->moduleHandler = $this
->prophesize(ModuleHandlerInterface::class)
->reveal();
$this->display = $this
->prophesize(DisplayInterface::class)
->reveal();
$this->requestStack = new RequestStack();
// Only set up the prophecy for now,
// it will need to be configured differently for each test function.
$this->entityTypeManagerProphecy = $this
->prophesize(EntityTypeManagerInterface::class);
$this->request = new Request();
$this->searchApiDisplayManager = $this
->prophesize(DisplayPluginManagerInterface::class)
->reveal();
}
/**
* Tests getActiveSort.
*
* @dataProvider provideSortOrders
*
* @covers ::getActiveSort
*/
public function testGetActiveSort($order_argument, $expected) {
$this->request->query = new ParameterBag([
'sort' => 'sort_field',
'order' => $order_argument,
]);
$this->requestStack
->push($this->request);
$manager = $this->entityTypeManagerProphecy
->reveal();
$searchApiSortsManager = new SearchApiSortsManager($this->requestStack, $manager, $this->moduleHandler, $this->searchApiDisplayManager);
$sorts = $searchApiSortsManager
->getActiveSort($this->display);
$this
->assertEquals('sort_field', $sorts
->getFieldName());
$this
->assertEquals($expected, $sorts
->getOrder());
}
/**
* Tests getEnabledSorts.
*
* @covers ::getEnabledSorts
*/
public function testGetEnabledSorts() {
$this->requestStack
->push($this->request);
$sortsField = new SearchApiSortsField([
'id' => $this
->randomMachineName(),
], 'search_api_sorts_field');
$storage = $this
->getMockBuilder(EntityStorageInterface::class)
->disableOriginalConstructor()
->getMock();
$storage
->expects($this
->once())
->method('loadByProperties')
->willReturn($sortsField);
try {
$this->entityTypeManagerProphecy
->getStorage('search_api_sorts_field')
->willReturn($storage);
} catch (InvalidPluginDefinitionException $e) {
$this
->fail("search_api_sorts storage not found.");
}
$manager = $this->entityTypeManagerProphecy
->reveal();
$searchApiSortsManager = new SearchApiSortsManager($this->requestStack, $manager, $this->moduleHandler, $this->searchApiDisplayManager);
$enabledSorts = $searchApiSortsManager
->getEnabledSorts($this->display);
$this
->assertEquals($sortsField, $enabledSorts);
}
/**
* Provides mock data and expected results for ::testActiveSortOrder.
*
* @return array
* An array of mockable data.
*/
public function provideSortOrders() {
return [
[
'asc',
'asc',
],
[
'desc',
'desc',
],
[
'aaa',
'asc',
],
[
NULL,
'asc',
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
SearchApiSortsManagerTest:: |
private | property | A display object. | |
SearchApiSortsManagerTest:: |
private | property | Entity type manager. | |
SearchApiSortsManagerTest:: |
private | property | ModuleHandler object. | |
SearchApiSortsManagerTest:: |
private | property | A request object to use for each test case. | |
SearchApiSortsManagerTest:: |
private | property | A request stack object to store requests. | |
SearchApiSortsManagerTest:: |
private | property | The search API Display manager. | |
SearchApiSortsManagerTest:: |
public | function | Provides mock data and expected results for ::testActiveSortOrder. | |
SearchApiSortsManagerTest:: |
public | function |
Overrides UnitTestCase:: |
|
SearchApiSortsManagerTest:: |
public | function | Tests getActiveSort. | |
SearchApiSortsManagerTest:: |
public | function | Tests getEnabledSorts. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |