Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Luc45 committed Apr 7, 2024
1 parent 2848100 commit 4f22206
Show file tree
Hide file tree
Showing 31 changed files with 567 additions and 399 deletions.
3 changes: 2 additions & 1 deletion src/src/Environment/EnvConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public function init_env_info(
): EnvInfo {
// Load the environment config file..
$env_config = $this->load_config();
$this->normalize_plural_to_singular( $env_config );

// Check that config file doesn't contain disallowed keys.
foreach ( EnvInfo::$not_user_configurable as $d ) {
Expand Down Expand Up @@ -74,6 +73,8 @@ public function init_env_info(
}
}

$this->normalize_plural_to_singular( $env_config );

// Plugins and Themes.
$env_config['plugin'] = $this->plugins_and_themes_parser->parse_extensions(
$env_config['plugin'] ?? [],
Expand Down
15 changes: 15 additions & 0 deletions src/src/Environment/ExtensionDownload/ExtensionDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ public function categorize_extensions( array $plugins, array $themes ): array {
] as $type => $extensions
) {
foreach ( $extensions as $ext ) {
if ( ! $ext instanceof Extension ) {
throw new \LogicException( 'Invalid extension object.' );
}

// At this point, source for all extensions should be set.
if ( empty( $ext->source ) ) {
throw new \LogicException( 'Extension source is required.' );
}

// At this point, slug should already be inferred from the source.
if ( empty( $ext->slug ) ) {
throw new \LogicException( 'Extension slug should be defined at this point.' );
}

// @phan-suppress-next-line PhanPossiblyUndeclaredVariable
if ( array_key_exists( $ext->slug, $categorized_extensions ) ) {
throw new \InvalidArgumentException( 'Duplicate extension found.' );
}
Expand Down
9 changes: 5 additions & 4 deletions src/src/Environment/PluginsAndThemesParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __construct( OutputInterface $output, WooExtensionsList $woo_ext

/**
* // phpcs:disable
*
* @param array<int|string, string|array{
* source?: string,
* slug?: string,
Expand Down Expand Up @@ -101,15 +102,15 @@ public function parse_extensions( array $plugins_or_themes, string $type, string
}

// Set default action if not provided.
if ( ! isset( $extension['action'] ) ) {
$extension['action'] = $default_action;
}
$extension['action'] = $extension['action'] ?? $default_action;

// Ensure test_tags is set.
if ( empty( $extension['test_tags'] ) ) {
// @phpstan-ignore-next-line.
if ( ! isset( $extension['test_tags'] ) || ! is_array( $extension['test_tags'] ) ) {
$extension['test_tags'] = [ 'default' ];
}

// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset
foreach ( $extension['test_tags'] as $test_tag ) {
if ( ! file_exists( $test_tag ) ) {
if ( ! preg_match( '/^[a-z0-9-_]+$/i', $test_tag ) ) {
Expand Down
1 change: 0 additions & 1 deletion src/src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use QIT_CLI\Commands\ConfigDirCommand;
use QIT_CLI\Commands\CreateMassTestCommands;
use QIT_CLI\Commands\CreateRunCommands;
use QIT_CLI\Commands\CustomTests\ExtensionDownloaderCommand;
use QIT_CLI\Commands\CustomTests\ScaffoldE2ECommand;
use QIT_CLI\Commands\CustomTests\ShowReportCommand;
use QIT_CLI\Commands\CustomTests\UploadCustomTestCommand;
Expand Down
76 changes: 28 additions & 48 deletions src/tests/Environment/EnvConfigLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ public function test_env_config_loader_override_with_invalid_json_should_throw()
public function test_env_config_loader_plugins_array() {
$complexStructure = [
'plugins' => [
'automatewoo' => [
'test_tag' => 'bar',
'qit-beaver' => [
'test_tags' => [ 'bar' ],
],
],
];
Expand All @@ -230,7 +230,7 @@ public function test_env_config_loader_plugins_array() {
public function test_env_config_loader_plugins_string() {
$complexStructure = [
'plugins' => [
'automatewoo',
'qit-beaver',
],
];
$this->create_config_file( 'qit-env.json', json_encode( $complexStructure ) );
Expand All @@ -241,15 +241,15 @@ public function test_env_config_loader_plugins_string() {
public function test_env_config_loader_plugins_mixed() {
$complexStructure = [
'plugins' => [
'automatewoo_birthdays' => [
'test_tag' => 'bar',
'qit-cat' => [
'test_tags' => [ 'bar' ],
],
'automatewoo',
'qit-beaver',
],
'themes' => [
'twentytwenty',
'twentytwentyone' => [
'test_tag' => 'foo',
'qit-dog',
'foo-extension' => [
'test_tags' => [ 'foo' ],
],
],
];
Expand All @@ -261,59 +261,39 @@ public function test_env_config_loader_plugins_mixed() {
public function test_env_config_loader_plugins_override() {
$override_structure = [
'plugins' => [
'automatewoo_birthdays: untrimed_override',
'automatewoo' => [
'test_tag' => 'array_override',
'qit-beaver' => [
'test_tags' => [ 'array_override' ],
],
'automatewoo_source_from_key' => [
'qit-dog' => [
'source' => 'source_from_value',
],
'https://woo.com/automatewoo-url',
'https://woo.com/automatewoo-url-array' => [
'test_tag' => 'test_tag_array',
'https://woo.com/qit-beaver',
'foo-extension' => [
'source' => 'https://woo.com/foo-extension',
'test_tags' => [ 'test_tag_array' ],
],
// 'https://woo.com/automatewoo-referrals:bar', <!-- Forbidden because we can't infer the "slug" to get the "bar" test tag. -->
'{"source": "https://woo.com/automatewoo-referrals", "slug": "automatewoo-referrals", "test_tag": "bar"}',
'C:\\Users\\user\\Desktop\\automatewoo-url-windows',
'C:\\Users\\user\\Desktop\\automatewoo-url-windows:baz',
// 'https://woo.com/qit-dog:bar', <!-- Forbidden because we can't infer the "slug" to get the "bar" test tag. -->
'{"source": "https://woo.com/qit-dog", "slug": "qit-dog", "test_tags": ["bar"]}',
'C:\\Users\\user\\Desktop\\qit-beaver',
'C:\\Users\\user\\Desktop\\qit-beaver:install',
],
'themes' => [
'twentytwentyone:string_override',
'qit-beaver:test',
],
];
$this->create_config_file( 'qit-env.json', json_encode( [] ) );

$this->assertMatchesJsonSnapshot( json_encode( $this->normalized_env_info( $this->sut->load_config(), $override_structure ) ) );
}

public function test_env_config_loader_plugins_url_without_source() {
$this->expectException( InvalidArgumentException::class );
$this->create_config_file( 'qit-env.json', json_encode( [] ) );
$this->normalized_env_info( $this->sut->load_config(), [
'plugins' => [
'https://woo.com/automatewoo-url',
],
] );
}

public function test_env_config_loader_plugins_url_without_source_json() {
$this->expectException( InvalidArgumentException::class );
$this->create_config_file( 'qit-env.json', json_encode( [] ) );
$this->normalized_env_info( $this->sut->load_config(), [
'plugins' => [
'{"source": "https://woo.com/automatewoo-url"}',
],
] );
}

public function test_env_config_loader_plugins_url_with_source() {
$this->create_config_file( 'qit-env.json', json_encode( [] ) );
$this->assertMatchesJsonSnapshot(
json_encode(
$this->normalized_env_info( $this->sut->load_config(), [
'plugins' => [
'automatewoo' => [
'source' => 'https://woo.com/automatewoo-url',
'qit-beaver' => [
'source' => 'https://woo.com/qit-beaver',
],
],
] )
Expand All @@ -327,9 +307,9 @@ public function test_env_config_loader_plugins_url_with_source_and_tags() {
json_encode(
$this->normalized_env_info( $this->sut->load_config(), [
'plugins' => [
'automatewoo' => [
'source' => 'https://woo.com/automatewoo-url',
'test_tag' => 'rc',
'qit-beaver' => [
'source' => 'https://woo.com/qit-beaver',
'test_tags' => [ 'rc' ],
],
],
] )
Expand All @@ -343,7 +323,7 @@ public function test_env_config_loader_plugins_url_with_source_json() {
json_encode(
$this->normalized_env_info( $this->sut->load_config(), [
'plugins' => [
'{"source":"https://woo.com/automatewoo-url", "slug":"automatewoo"}',
'{"source":"https://woo.com/qit-beaver", "slug":"qit-beaver"}',
],
] )
)
Expand All @@ -356,7 +336,7 @@ public function test_env_config_loader_plugins_url_with_source_and_tags_json() {
json_encode(
$this->normalized_env_info( $this->sut->load_config(), [
'plugins' => [
'{"source":"https://woo.com/automatewoo-url", "slug":"automatewoo", "test_tag": "rc"}',
'{"source":"https://woo.com/qit-beaver", "slug":"qit-beaver", "test_tags": ["rc"]}',
],
] )
)
Expand Down
Loading

0 comments on commit 4f22206

Please sign in to comment.