Skip to content

Commit

Permalink
Improve tests by using command name constants (#5898)
Browse files Browse the repository at this point in the history
  • Loading branch information
weitzman authored Mar 12, 2024
1 parent 3cae34f commit e0478fa
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 101 deletions.
8 changes: 5 additions & 3 deletions src/Commands/field/FieldBaseInfoCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;

class FieldBaseInfoCommands extends DrushCommands
final class FieldBaseInfoCommands extends DrushCommands
{
use AutowireTrait;

use EntityTypeBundleAskTrait;
use EntityTypeBundleValidationTrait;
use FieldDefinitionRowsOfFieldsTrait;

const BASE_INFO = 'field:base-info';

public function __construct(
protected EntityTypeManagerInterface $entityTypeManager,
protected EntityTypeBundleInfoInterface $entityTypeBundleInfo,
Expand All @@ -32,7 +34,7 @@ public function __construct(
/**
* List all base fields of an entity type
*/
#[CLI\Command(name: 'field:base-info', aliases: ['field-base-info', 'fbi'])]
#[CLI\Command(name: self::BASE_INFO, aliases: ['field-base-info', 'fbi'])]
#[CLI\Argument(name: 'entityType', description: 'The machine name of the entity type.')]
#[CLI\Option(name: 'show-machine-names', description: 'Show machine names instead of labels in option lists.')]
#[CLI\DefaultTableFields(fields: [
Expand All @@ -58,7 +60,7 @@ public function __construct(
])]
#[CLI\FilterDefaultField(field: 'field_name')]
#[CLI\Usage(name: 'field:base-info taxonomy_term', description: 'List all base fields.')]
#[CLI\Usage(name: 'field:base-info', description: 'List all base fields and fill in the remaining information through prompts.')]
#[CLI\Usage(name: self::BASE_INFO, description: 'List all base fields and fill in the remaining information through prompts.')]
#[CLI\Complete(method_name_or_callable: 'complete')]
#[CLI\Version(version: '11.0')]
public function info(?string $entityType = null, array $options = [
Expand Down
6 changes: 4 additions & 2 deletions src/Commands/field/FieldBaseOverrideCreateCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@

use function dt;

class FieldBaseOverrideCreateCommands extends DrushCommands
final class FieldBaseOverrideCreateCommands extends DrushCommands
{
use AutowireTrait;
use EntityTypeBundleAskTrait;
use EntityTypeBundleValidationTrait;

const BASE_OVERRIDE_CREATE = 'field:base-override-create';

public function __construct(
protected EntityTypeManagerInterface $entityTypeManager,
protected EntityTypeBundleInfoInterface $entityTypeBundleInfo,
Expand All @@ -37,7 +39,7 @@ public function __construct(
* @see \Drupal\field_ui\Form\FieldConfigEditForm
* @see \Drupal\field_ui\Form\FieldStorageConfigEditForm
*/
#[CLI\Command(name: 'field:base-override-create', aliases: ['bfoc'])]
#[CLI\Command(name: self::BASE_OVERRIDE_CREATE, aliases: ['bfoc'])]
#[CLI\Argument(name: 'entityType', description: 'The machine name of the entity type.')]
#[CLI\Argument(name: 'bundle', description: 'The machine name of the bundle.')]
#[CLI\Option(name: 'field-name', description: 'A unique machine-readable name containing letters, numbers, and underscores.')]
Expand Down
6 changes: 4 additions & 2 deletions src/Commands/field/FieldDeleteCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
use function dt;
use function field_purge_batch;

class FieldDeleteCommands extends DrushCommands
final class FieldDeleteCommands extends DrushCommands
{
use AutowireTrait;
use EntityTypeBundleAskTrait;
use EntityTypeBundleValidationTrait;

const DELETE = 'field:delete';

public function __construct(
protected EntityTypeManagerInterface $entityTypeManager,
protected EntityTypeBundleInfoInterface $entityTypeBundleInfo
Expand All @@ -36,7 +38,7 @@ public function __construct(
*
* @see \Drupal\field_ui\Form\FieldConfigDeleteForm
*/
#[CLI\Command(name: 'field:delete', aliases: ['field-delete', 'fd'])]
#[CLI\Command(name: self::DELETE, aliases: ['field-delete', 'fd'])]
#[CLI\Argument(name: 'entityType', description: 'The machine name of the entity type.')]
#[CLI\Argument(name: 'bundle', description: 'The machine name of the bundle.')]
#[CLI\Option(name: 'field-name', description: 'The machine name of the field.')]
Expand Down
8 changes: 5 additions & 3 deletions src/Commands/field/FieldInfoCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;

class FieldInfoCommands extends DrushCommands
final class FieldInfoCommands extends DrushCommands
{
use AutowireTrait;
use EntityTypeBundleAskTrait;
use EntityTypeBundleValidationTrait;
use FieldDefinitionRowsOfFieldsTrait;

const INFO = 'field:info';

public function __construct(
protected EntityTypeManagerInterface $entityTypeManager,
protected EntityTypeBundleInfoInterface $entityTypeBundleInfo
Expand All @@ -29,7 +31,7 @@ public function __construct(
/**
* List all configurable fields of an entity bundle
*/
#[CLI\Command(name: 'field:info', aliases: ['field-info', 'fi'])]
#[CLI\Command(name: self::INFO, aliases: ['field-info', 'fi'])]
#[CLI\Argument(name: 'entityType', description: 'The machine name of the entity type.')]
#[CLI\Argument(name: 'bundle', description: 'The machine name of the bundle.')]
#[CLI\Option(name: 'show-machine-names', description: 'Show machine names instead of labels in option lists.')]
Expand All @@ -56,7 +58,7 @@ public function __construct(
])]
#[CLI\FilterDefaultField(field: 'field_name')]
#[CLI\Usage(name: 'field:info taxonomy_term tag', description: 'List all fields.')]
#[CLI\Usage(name: 'field:info', description: 'List all fields and fill in the remaining information through prompts.')]
#[CLI\Usage(name: self::INFO, description: 'List all fields and fill in the remaining information through prompts.')]
#[CLI\Complete(method_name_or_callable: 'complete')]
#[CLI\Version(version: '11.0')]
public function info(?string $entityType = null, ?string $bundle = null, array $options = [
Expand Down
37 changes: 20 additions & 17 deletions tests/functional/ArchiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Unish;

use Drush\Commands\core\ArchiveDumpCommands;
use Drush\Commands\core\ArchiveRestoreCommands;
use Drush\Commands\core\StatusCommands;
use PharData;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Process\Process;
Expand Down Expand Up @@ -40,7 +43,7 @@ public function setUp(): void

$this->archivePath = Path::join($this->getSandbox(), 'archive.tar.gz');
$this->drush(
'archive:dump',
ArchiveDumpCommands::DUMP,
[],
array_merge($this->archiveDumpOptions, [
'destination' => $this->archivePath,
Expand All @@ -59,7 +62,7 @@ public function setUp(): void
$archive->extractTo($this->extractPath);

$this->drush(
'status',
StatusCommands::STATUS,
[],
['format' => 'json']
);
Expand All @@ -85,7 +88,7 @@ public function testArchiveDumpCommand(): void

// Try to overwrite the existing archive with "--destination".
$this->drush(
'archive:dump',
ArchiveDumpCommands::DUMP,
[],
array_merge($this->archiveDumpOptions, [
'destination' => $this->archivePath,
Expand All @@ -98,7 +101,7 @@ public function testArchiveDumpCommand(): void

// Overwrite the existing archive with "--destination" and "--override".
$this->drush(
'archive:dump',
ArchiveDumpCommands::DUMP,
[],
array_merge($this->archiveDumpOptions, [
'destination' => $this->archivePath,
Expand All @@ -110,7 +113,7 @@ public function testArchiveDumpCommand(): void

// Validate database credentials are present in settings.php file.
$this->drush(
'archive:dump',
ArchiveDumpCommands::DUMP,
[],
[],
null,
Expand Down Expand Up @@ -143,7 +146,7 @@ public function SKIPtestArchiveRestoreCommand(): void
$testFileName = 'test-file-' . mt_rand() . '.txt';
file_put_contents(Path::join($this->extractPath, 'code', 'sut', $testFileName), 'foo_bar');
$this->drush(
'archive:restore',
ArchiveRestoreCommands::RESTORE,
[],
array_merge($this->archiveRestoreOptions, [
'code' => null,
Expand All @@ -159,7 +162,7 @@ public function SKIPtestArchiveRestoreCommand(): void

// Restore archive from an existing file and an existing destination path.
$this->drush(
'archive:restore',
ArchiveRestoreCommands::RESTORE,
[$this->archivePath],
array_diff_key($this->archiveRestoreOptions, ['overwrite' => null]),
null,
Expand All @@ -173,7 +176,7 @@ public function SKIPtestArchiveRestoreCommand(): void

// Restore archive from an existing file.
$this->drush(
'archive:restore',
ArchiveRestoreCommands::RESTORE,
[$this->archivePath],
$this->archiveRestoreOptions
);
Expand All @@ -185,7 +188,7 @@ public function SKIPtestArchiveRestoreCommand(): void
file_put_contents(Path::join($this->extractPath, 'files', $testFileName), 'foo_bar');
$filesRelativePath = Path::join('sut', 'sites', 'default', 'files');
$this->drush(
'archive:restore',
ArchiveRestoreCommands::RESTORE,
[],
array_merge($this->archiveRestoreOptions, [
'files' => null,
Expand All @@ -200,7 +203,7 @@ public function SKIPtestArchiveRestoreCommand(): void

// Restore the database from a source path.
$this->drush(
'archive:restore',
ArchiveRestoreCommands::RESTORE,
[],
array_merge($this->archiveRestoreOptions, [
'db' => null,
Expand All @@ -213,7 +216,7 @@ public function SKIPtestArchiveRestoreCommand(): void

// Restore database with invalid --db-url.
$this->drush(
'archive:restore',
ArchiveRestoreCommands::RESTORE,
[],
array_merge($this->archiveRestoreOptions, [
'db' => null,
Expand All @@ -234,7 +237,7 @@ public function SKIPtestArchiveRestoreCommand(): void

// Restore database with --db-url option with an invalid host.
$this->drush(
'archive:restore',
ArchiveRestoreCommands::RESTORE,
[],
array_merge($this->archiveRestoreOptions, [
'db' => null,
Expand Down Expand Up @@ -262,7 +265,7 @@ public function SKIPtestArchiveRestoreCommand(): void

// Restore database with a set of database connection options.
$this->drush(
'archive:restore',
ArchiveRestoreCommands::RESTORE,
[],
array_merge(
array_diff_key($this->archiveRestoreOptions, ['db-url' => null]),
Expand All @@ -283,7 +286,7 @@ public function SKIPtestArchiveRestoreCommand(): void

// Restore database with a set of database connection options with an invalid host.
$this->drush(
'archive:restore',
ArchiveRestoreCommands::RESTORE,
[],
array_merge(
array_diff_key($this->archiveRestoreOptions, ['db-url' => null]),
Expand Down Expand Up @@ -312,7 +315,7 @@ public function SKIPtestArchiveRestoreCommand(): void
// Restore archive from a non-existing file.
$nonExistingArchivePath = Path::join($this->getSandbox(), 'arch.tar.gz');
$this->drush(
'archive:restore',
ArchiveRestoreCommands::RESTORE,
[$nonExistingArchivePath],
$this->archiveRestoreOptions,
null,
Expand All @@ -329,7 +332,7 @@ public function SKIPtestArchiveRestoreCommand(): void

// Restore database without database connection settings.
$this->drush(
'archive:restore',
ArchiveRestoreCommands::RESTORE,
[],
array_merge(
array_diff_key($this->archiveRestoreOptions, ['db-url' => null]),
Expand Down Expand Up @@ -374,7 +377,7 @@ private function assertRestoredSiteStatus(): void
$this->assertTrue(is_file(Path::join($this->restorePath, 'sut', 'sites', 'dev', 'settings.local.php')));

$this->drush(
'status',
StatusCommands::STATUS,
[],
['format' => 'json'],
null,
Expand Down
Loading

0 comments on commit e0478fa

Please sign in to comment.