class RouteParametersWebformSourceEntityTest in Webform 8.5
Same name and namespace in other branches
- 6.x tests/src/Unit/Plugin/WebformSourceEntity/RouteParametersWebformSourceEntityTest.php \Drupal\Tests\webform\Unit\Plugin\WebformSourceEntity\RouteParametersWebformSourceEntityTest
Tests the "route_parameters" webform source entity plugin.
@group webform
@coversDefaultClass \Drupal\webform\Plugin\WebformSourceEntity\RouteParametersWebformSourceEntity
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\webform\Unit\Plugin\WebformSourceEntity\RouteParametersWebformSourceEntityTest
Expanded class hierarchy of RouteParametersWebformSourceEntityTest
File
- tests/
src/ Unit/ Plugin/ WebformSourceEntity/ RouteParametersWebformSourceEntityTest.php, line 18
Namespace
Drupal\Tests\webform\Unit\Plugin\WebformSourceEntityView source
class RouteParametersWebformSourceEntityTest extends UnitTestCase {
/**
* Tests detection of source entity via route parameters.
*
* @param array $route_parameters
* Route parameters array to inject. You may use the following magic values:
* - source_entity: to denote that this parameter should contain source
* entity
* - another_entity: to denote that this parameter should contain some other
* entity.
* @param array $ignored_types
* Array of entity types that may not be source.
* @param bool $expect_source_entity
* Whether we expect the tested method to return the source entity.
* @param string $assert_message
* Assert message to use.
*
* @see RouteParametersWebformSourceEntity::getSourceEntity()
*
* @dataProvider providerGetCurrentSourceEntity
*/
public function testGetCurrentSourceEntity(array $route_parameters, array $ignored_types, $expect_source_entity, $assert_message = '') {
$route_match = $this
->getMockBuilder(RouteMatchInterface::class)
->disableOriginalConstructor()
->getMock();
$source_entity = $this
->getMockBuilder(EntityInterface::class)
->getMock();
// Process $route_parameters to unwrap it with real objects.
if (isset($route_parameters['source_entity'])) {
$route_parameters['source_entity'] = $source_entity;
}
if (isset($route_parameters['another_entity'])) {
$route_parameters['another_entity'] = $this
->getMockBuilder(EntityInterface::class)
->getMock();
}
$route_match
->method('getParameters')
->willReturn(new ParameterBag($route_parameters));
$plugin = new RouteParametersWebformSourceEntity([], 'route_parameters', [], $route_match);
$output = $plugin
->getSourceEntity($ignored_types);
if ($expect_source_entity) {
$this
->assertSame($source_entity, $output, $assert_message);
}
else {
$this
->assertNull($output, $assert_message);
}
}
/**
* Data provider for testGetCurrentSourceEntity().
*
* @see testGetCurrentSourceEntity()
*/
public function providerGetCurrentSourceEntity() {
$tests[] = [
[],
[],
FALSE,
'Empty parameters',
];
$tests[] = [
[
'source_entity' => 1,
],
[],
TRUE,
'Just source entity in the parameters',
];
$tests[] = [
[
'source_entity' => 1,
],
[
'source_entity',
],
FALSE,
'Source entity in the parameters but forced to ignore',
];
$tests[] = [
[
'another_entity' => 1,
'source_entity' => 1,
'static_value' => 'static_value',
],
[],
TRUE,
'Testing the order in which parameters are scanned',
];
return $tests;
}
}
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. | |
RouteParametersWebformSourceEntityTest:: |
public | function | Data provider for testGetCurrentSourceEntity(). | |
RouteParametersWebformSourceEntityTest:: |
public | function | Tests detection of source entity via route parameters. | |
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. | |
UnitTestCase:: |
protected | function | 340 |