abstract class WidgetTestBase in Facets 8
Base class for widget unit tests.
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\facets\Unit\Plugin\widget\WidgetTestBase
Expanded class hierarchy of WidgetTestBase
1 file declares its use of WidgetTestBase
- SliderWidgetTest.php in modules/
facets_range_widget/ tests/ src/ Unit/ Plugin/ widget/ SliderWidgetTest.php
File
- tests/
src/ Unit/ Plugin/ widget/ WidgetTestBase.php, line 18
Namespace
Drupal\Tests\facets\Unit\Plugin\widgetView source
abstract class WidgetTestBase extends UnitTestCase {
/**
* The widget to be tested.
*
* @var \Drupal\facets\Widget\WidgetPluginInterface
*/
protected $widget;
/**
* The facet used for the widget test.
*
* @var \Drupal\facets\FacetInterface
*/
protected $facet;
/**
* An array containing the results for the widget.
*
* @var \Drupal\facets\Result\Result[]
*/
protected $originalResults;
/**
* An array of possible query types.
*
* @var string[]
*/
protected $queryTypes;
/**
* Sets up the container and other variables used in all the tests.
*/
protected function setUp() {
parent::setUp();
$facet = new Facet([], 'facets_facet');
$this->facet = $facet;
/** @var \Drupal\facets\Result\Result[] $original_results */
$original_results = [
new Result($facet, 'llama', 'Llama', 10),
new Result($facet, 'badger', 'Badger', 20),
new Result($facet, 'duck', 'Duck', 15),
new Result($facet, 'alpaca', 'Alpaca', 9),
];
foreach ($original_results as $original_result) {
$original_result
->setUrl(new Url('test'));
}
$this->originalResults = $original_results;
// Create a container, so we can access string translation.
$string_translation = $this
->prophesize(TranslationInterface::class);
$url_generator = $this
->prophesize(UrlGeneratorInterface::class);
$widget_manager = $this
->prophesize(WidgetPluginManager::class);
$container = new ContainerBuilder();
$container
->set('plugin.manager.facets.widget', $widget_manager
->reveal());
$container
->set('string_translation', $string_translation
->reveal());
$container
->set('url_generator', $url_generator
->reveal());
\Drupal::setContainer($container);
$this->queryTypes = [
'date' => 'date',
'string' => 'string',
'numeric' => 'numeric',
'range' => 'range',
];
}
/**
* Tests default configuration.
*/
public function testDefaultConfiguration() {
$default_config = $this->widget
->defaultConfiguration();
$this
->assertEquals([
'show_numbers' => FALSE,
'soft_limit' => 0,
], $default_config);
}
/**
* Tests get query type.
*/
public function testGetQueryType() {
$result = $this->widget
->getQueryType($this->queryTypes);
$this
->assertEquals(NULL, $result);
}
/**
* Tests default for required properties.
*/
public function testIsPropertyRequired() {
$this
->assertFalse($this->widget
->isPropertyRequired('llama', 'owl'));
}
/**
* Build a formattable markup object to use as assertion.
*
* @param string $text
* Text to display.
* @param string $raw_value
* Raw value of the result.
* @param \Drupal\facets\FacetInterface $facet
* The facet.
* @param int $count
* Number of results.
* @param bool $active
* Link is active.
* @param bool $show_numbers
* Numbers are displayed.
*
* @return array
* A render array.
*/
protected function buildLinkAssertion($text, $raw_value, FacetInterface $facet, $count = 0, $active = FALSE, $show_numbers = TRUE) {
return [
'#theme' => 'facets_result_item',
'#raw_value' => $raw_value,
'#facet' => $facet,
'#value' => $text,
'#show_count' => $show_numbers && $count !== NULL,
'#count' => $count,
'#is_active' => $active,
];
}
}
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. | |
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. | |
WidgetTestBase:: |
protected | property | The facet used for the widget test. | |
WidgetTestBase:: |
protected | property | An array containing the results for the widget. | |
WidgetTestBase:: |
protected | property | An array of possible query types. | |
WidgetTestBase:: |
protected | property | The widget to be tested. | |
WidgetTestBase:: |
protected | function | Build a formattable markup object to use as assertion. | |
WidgetTestBase:: |
protected | function |
Sets up the container and other variables used in all the tests. Overrides UnitTestCase:: |
5 |
WidgetTestBase:: |
public | function | Tests default configuration. | 5 |
WidgetTestBase:: |
public | function | Tests get query type. | 1 |
WidgetTestBase:: |
public | function | Tests default for required properties. | 1 |