class ConditionalFieldControllerTest in Conditional Fields 8
Same name and namespace in other branches
- 4.x tests/src/Unit/ConditionalFieldControllerTest.php \Drupal\Tests\conditional_fields\Unit\ConditionalFieldControllerTest
ConditionalFieldController units tests.
In unit testing, there should be as few dependencies as possible. We want the smallest number of moving parts to be interacting in our test, or we won't be sure where the errors are, or whether our tests passed by accident.
@group conditional_fields
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\conditional_fields\Unit\ConditionalFieldControllerTest
Expanded class hierarchy of ConditionalFieldControllerTest
File
- tests/
src/ Unit/ ConditionalFieldControllerTest.php, line 21
Namespace
Drupal\Tests\conditional_fields\UnitView source
class ConditionalFieldControllerTest extends UnitTestCase {
/**
* CF Controller.
*
* @var ConditionalFieldController
*/
protected $controller;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$entity_types = [];
// `content_a` should appear in test results.
$entity_types['content_a'] = $this
->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityType')
->disableOriginalConstructor()
->getMock();
$entity_types['content_a']
->expects($this
->any())
->method('getLabel')
->will($this
->returnValue("contentA"));
// `content_b` shouldn't appear in test results.
$entity_types['content_b'] = $this
->createMock('Drupal\\Core\\Config\\Entity\\ConfigEntityTypeInterface');
$entity_types['content_b']
->expects($this
->any())
->method('getLabel')
->will($this
->returnValue("contentB"));
// Setup Drupal Container.
$entity_type_manager = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
$entity_type_manager
->expects($this
->any())
->method('getDefinitions')
->will($this
->returnValue($entity_types));
// For only one test case this classes not used,
// change this after adding new test cases.
$form_builder = $this
->createMock('Drupal\\Core\\Form\\FormBuilderInterface');
$form_builder
->expects($this
->never())
->method($this
->anything());
$entity_type_bundle_info = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeBundleInfoInterface');
$entity_type_bundle_info
->expects($this
->never())
->method($this
->anything());
$entity_field_manager = $this
->createMock('Drupal\\Core\\Entity\\EntityFieldManagerInterface');
$entity_field_manager
->expects($this
->never())
->method($this
->anything());
// ConditionalFieldController::create();
$this->controller = new ConditionalFieldController($entity_type_manager, $form_builder, $entity_type_bundle_info, $entity_field_manager);
}
/**
* Very simple test of ConditionalFieldController::entityTypeList().
*
* This is a very simple unit test of a single method.
* It checks that only instances of `ContentEntityType` are available.
*/
public function testEntityTypeList() {
$available_entity_types = $this->controller
->entityTypeList()['#content'];
$this
->assertEquals(1, count($available_entity_types));
$this
->assertEquals($available_entity_types[0]['title'], 'contentA');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConditionalFieldControllerTest:: |
protected | property | CF Controller. | |
ConditionalFieldControllerTest:: |
protected | function |
Overrides UnitTestCase:: |
|
ConditionalFieldControllerTest:: |
public | function | Very simple test of ConditionalFieldController::entityTypeList(). | |
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. |