Skip to content

Instantly share code, notes, and snippets.

@kerasai
Created August 25, 2017 17:48
Show Gist options
  • Save kerasai/68417e33032268fdfb083440ba79b0b9 to your computer and use it in GitHub Desktop.
Save kerasai/68417e33032268fdfb083440ba79b0b9 to your computer and use it in GitHub Desktop.
Behat Context for checking field configuration in Drupal 8
<?php
use Behat\Behat\Context\Context;
/**
* Defines application features from the specific context.
*/
class FieldContext implements Context {
/**
* Verify a field exists on an entity bundle.
*
* @Then :bundle type :entity_type entities have a(n) :field_type field :field_name
*/
public function assertEntityTypeFieldType($entity_type, $bundle, $field_type, $field_name) {
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $fieldManager */
$fieldManager = \Drupal::service('entity_field.manager');
$fieldDefinitions = $fieldManager->getFieldDefinitions($entity_type, $bundle);
// Verify field set on entity type, bundle.
if (!array_key_exists($field_name, $fieldDefinitions)) {
throw new \Exception(sprintf('No field "%s" exists on "%s" type "%s" entities.', $field_name, $bundle, $entity_type));
}
// Verify type.
if ($fieldDefinitions[$field_name]->getType() != $field_type) {
throw new \Exception(sprintf('No field "%s" on "%s" type "%s" entities is not type "%s".', $field_name, $bundle, $entity_type, $field_type));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment