You are here

class ConditionalFieldControllerTest in Conditional Fields 8

Same name and namespace in other branches
  1. 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

Expanded class hierarchy of ConditionalFieldControllerTest

File

tests/src/Unit/ConditionalFieldControllerTest.php, line 21

Namespace

Drupal\Tests\conditional_fields\Unit
View 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

Namesort descending Modifiers Type Description Overrides
ConditionalFieldControllerTest::$controller protected property CF Controller.
ConditionalFieldControllerTest::setUp protected function Overrides UnitTestCase::setUp
ConditionalFieldControllerTest::testEntityTypeList public function Very simple test of ConditionalFieldController::entityTypeList().
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.