devel_wizard-2.x-dev/src/Plugin/DevelWizard/Spell/ConfigEntityBehatSpellBase.php
src/Plugin/DevelWizard/Spell/ConfigEntityBehatSpellBase.php
<?php
declare(strict_types=1);
namespace Drupal\devel_wizard\Plugin\DevelWizard\Spell;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Template\TwigEnvironment;
use Drupal\devel_wizard\Utils;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Filesystem\Filesystem;
/**
* @todo Patch:
* "drupal/drupal-extension": [
* {
* "description": "Behat config schema - extendable selectors and text",
* "url": "https://patch-diff.githubusercontent.com/raw/jhedstrom/drupalextension/pull/640.patch",
* "sha256": "0c6759fe79625b3c0893418cacef8a6c0bde5256207efcc928a772aaa974a156",
* "extra": {
* "issue-tracker-url": "https://github.com/jhedstrom/drupalextension/issues/493"
* }
* }
* ],
* Composer repositories:
* "nuvoleweb/drupal-behat": {
* "type": "github",
* "url": "https://github.com/albeorte96/drupal-behat.git"
* },
* ---
* Required composer packages
* - dmore/behat-chrome-extension
* - nuvoleweb/drupal-behat
* - sweetchuck/dtt-core
*/
abstract class ConfigEntityBehatSpellBase extends ConfigEntitySpellBase {
protected TwigEnvironment $twig;
protected Filesystem $fs;
public static function create(
ContainerInterface $container,
array $configuration,
$plugin_id,
$plugin_definition,
) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('messenger'),
$container->get('logger.channel.devel_wizard_spell'),
$container->get('string_translation'),
$container->get('devel_wizard.utils'),
$container->get('config.factory'),
$container->get('entity_type.manager'),
$container->get('twig'),
new Filesystem(),
);
}
/**
* {@inheritdoc}
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
MessengerInterface $messenger,
LoggerInterface $logger,
TranslationInterface $stringTranslation,
Utils $utils,
ConfigFactoryInterface $configFactory,
EntityTypeManagerInterface $entityTypeManager,
TwigEnvironment $twig,
Filesystem $fs,
) {
$this->entityTypeManager = $entityTypeManager;
$this->twig = $twig;
$this->fs = $fs;
parent::__construct(
$configuration,
$plugin_id,
$plugin_definition,
$messenger,
$logger,
$stringTranslation,
$utils,
$configFactory,
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'machine_name' => '',
'features_dir' => '',
];
}
protected function populateCalculatedConfigurationValues(): static {
parent::populateCalculatedConfigurationValues();
if (empty($this->configuration['machine_name'])) {
throw new \InvalidArgumentException('machine_name is required');
}
if (empty($this->configuration['features_dir'])) {
// @todo Make this path configurable or autodetect based on the behat.yml.
$this->configuration['features_dir'] = '../tests/behat/features';
}
return $this;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$configuration = $this->getConfiguration();
$configEntityType = $this->getConfigEntityType();
$form['machine_name'] = [
'#type' => 'textfield',
'#required' => TRUE,
'#title' => $configEntityType?->getLabel() ?: $this->t('Bundle'),
'#description' => $this->t(
'The machine name of the @entity_type.label to generate Behat tests.',
[
'@entity_type.label' => $configEntityType?->getLabel() ?: $this->configEntityTypeId,
],
),
'#default_value' => $configuration['machine_name'],
'#autocomplete_route_name' => 'devel_wizard.autocomplete.config_entity_instance',
'#autocomplete_route_parameters' => [
'entityTypeId' => $this->configEntityTypeId,
],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
// @todo Implement validateConfigurationForm() method.
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValue($form['#parents'], []);
$this->setConfiguration($values);
}
protected function doIt(): static {
$this->doItGenerateTests();
$this->batchContext['sandbox']['finished'] = 1.0;
return $this;
}
abstract protected function doItGenerateTests(): static;
}
