|
| 1 | +package project |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +// writeMinimalPlugin creates a minimal platform plugin the upload command can |
| 14 | +// load, so its RunE reaches the environment-resolution step. |
| 15 | +func writeMinimalPlugin(t *testing.T) string { |
| 16 | + t.Helper() |
| 17 | + |
| 18 | + dir := t.TempDir() |
| 19 | + require.NoError(t, os.WriteFile(filepath.Join(dir, "composer.json"), []byte(`{ |
| 20 | + "name": "frosh/frosh-test", |
| 21 | + "type": "shopware-platform-plugin", |
| 22 | + "license": "MIT", |
| 23 | + "version": "1.0.0", |
| 24 | + "require": { "shopware/core": "~6.6.0" }, |
| 25 | + "autoload": { "psr-4": { "FroshTest\\": "src/" } }, |
| 26 | + "extra": { |
| 27 | + "shopware-plugin-class": "FroshTest\\FroshTest", |
| 28 | + "label": { "de-DE": "Test", "en-GB": "Test" } |
| 29 | + } |
| 30 | + }`), 0o644)) |
| 31 | + require.NoError(t, os.WriteFile(filepath.Join(dir, ".shopware-extension.yml"), []byte( |
| 32 | + "build:\n zip:\n composer:\n enabled: false\n assets:\n enabled: false\n", |
| 33 | + ), 0o644)) |
| 34 | + require.NoError(t, os.MkdirAll(filepath.Join(dir, "src"), 0o755)) |
| 35 | + require.NoError(t, os.WriteFile(filepath.Join(dir, "src", "FroshTest.php"), |
| 36 | + []byte("<?php\nnamespace FroshTest;\nuse Shopware\\Core\\Framework\\Plugin;\nclass FroshTest extends Plugin {}\n"), 0o644)) |
| 37 | + |
| 38 | + return dir |
| 39 | +} |
| 40 | + |
| 41 | +// TestReadConfigWithEnvironmentPropagatesConfigError ensures a config-read |
| 42 | +// failure is surfaced rather than swallowed by the environment resolution. |
| 43 | +func TestReadConfigWithEnvironmentPropagatesConfigError(t *testing.T) { |
| 44 | + previousConfigPath := projectConfigPath |
| 45 | + previousEnvironmentName := environmentName |
| 46 | + t.Cleanup(func() { |
| 47 | + projectConfigPath = previousConfigPath |
| 48 | + environmentName = previousEnvironmentName |
| 49 | + }) |
| 50 | + |
| 51 | + projectConfigPath = filepath.Join(t.TempDir(), "does-not-exist.yml") |
| 52 | + environmentName = "staging" |
| 53 | + |
| 54 | + cmd := &cobra.Command{} |
| 55 | + cmd.SetContext(t.Context()) |
| 56 | + |
| 57 | + _, err := readConfigWithEnvironment(cmd, false) |
| 58 | + require.Error(t, err) |
| 59 | + assert.Contains(t, err.Error(), "cannot find project configuration file") |
| 60 | +} |
| 61 | + |
| 62 | +// TestNoEnvFlagKeepsBaseConfig ensures that without -e the base url and |
| 63 | +// credentials are used even when an environments.local entry exists, so the |
| 64 | +// fix does not silently retarget existing setups. |
| 65 | +func TestNoEnvFlagKeepsBaseConfig(t *testing.T) { |
| 66 | + configPath := filepath.Join(t.TempDir(), ".shopware-project.yml") |
| 67 | + require.NoError(t, os.WriteFile(configPath, []byte(` |
| 68 | +url: http://127.0.0.1:9 |
| 69 | +compatibility_date: "2026-01-01" |
| 70 | +admin_api: |
| 71 | + client_id: base-id |
| 72 | + client_secret: base-secret |
| 73 | +environments: |
| 74 | + local: |
| 75 | + url: http://127.0.0.1:7 |
| 76 | +`), 0o644)) |
| 77 | + |
| 78 | + previousConfigPath := projectConfigPath |
| 79 | + previousEnvironmentName := environmentName |
| 80 | + projectConfigPath = configPath |
| 81 | + environmentName = "" |
| 82 | + t.Cleanup(func() { |
| 83 | + projectConfigPath = previousConfigPath |
| 84 | + environmentName = previousEnvironmentName |
| 85 | + }) |
| 86 | + |
| 87 | + projectExtensionListCmd.SetContext(t.Context()) |
| 88 | + |
| 89 | + err := projectExtensionListCmd.RunE(projectExtensionListCmd, []string{}) |
| 90 | + require.Error(t, err) |
| 91 | + assert.Contains(t, err.Error(), "127.0.0.1:9", "without -e the base URL must be used") |
| 92 | + assert.NotContains(t, err.Error(), "127.0.0.1:7", "environments.local must not silently retarget commands") |
| 93 | +} |
| 94 | + |
| 95 | +// TestAdminAPICommandsResolveEnvironment verifies every project command that |
| 96 | +// talks to a shop honors the -e/--env flag: an unknown environment is rejected, |
| 97 | +// and a known environment's URL is the one contacted. |
| 98 | +func TestAdminAPICommandsResolveEnvironment(t *testing.T) { |
| 99 | + pluginDir := writeMinimalPlugin(t) |
| 100 | + |
| 101 | + cases := []struct { |
| 102 | + name string |
| 103 | + cmd *cobra.Command |
| 104 | + args []string |
| 105 | + }{ |
| 106 | + {"admin-api", projectAdminApiCmd, []string{"GET", "/_info/config"}}, |
| 107 | + {"clear-cache", projectClearCacheCmd, nil}, |
| 108 | + {"extension activate", projectExtensionActivateCmd, []string{"Foo"}}, |
| 109 | + {"extension deactivate", projectExtensionDeactivateCmd, []string{"Foo"}}, |
| 110 | + {"extension delete", projectExtensionDeleteCmd, []string{"Foo"}}, |
| 111 | + {"extension install", projectExtensionInstallCmd, []string{"Foo"}}, |
| 112 | + {"extension list", projectExtensionListCmd, nil}, |
| 113 | + {"extension outdated", projectExtensionOutdatedCmd, nil}, |
| 114 | + {"extension uninstall", projectExtensionUninstallCmd, []string{"Foo"}}, |
| 115 | + {"extension update", projectExtensionUpdateCmd, []string{"Foo"}}, |
| 116 | + {"extension upload", projectExtensionUploadCmd, []string{pluginDir}}, |
| 117 | + {"upgrade-check", projectUpgradeCheckCmd, nil}, |
| 118 | + } |
| 119 | + |
| 120 | + for _, tc := range cases { |
| 121 | + t.Run(tc.name+"/rejects unknown environment", func(t *testing.T) { |
| 122 | + setupEnvironmentConfig(t) |
| 123 | + environmentName = "nonexistent" |
| 124 | + tc.cmd.SetContext(t.Context()) |
| 125 | + |
| 126 | + err := tc.cmd.RunE(tc.cmd, tc.args) |
| 127 | + require.Error(t, err) |
| 128 | + assert.Contains(t, err.Error(), `environment "nonexistent" not found`, |
| 129 | + "command must reject an unknown environment instead of silently using the base config") |
| 130 | + }) |
| 131 | + |
| 132 | + t.Run(tc.name+"/targets selected environment", func(t *testing.T) { |
| 133 | + setupEnvironmentConfig(t) |
| 134 | + environmentName = "staging" |
| 135 | + tc.cmd.SetContext(t.Context()) |
| 136 | + |
| 137 | + err := tc.cmd.RunE(tc.cmd, tc.args) |
| 138 | + require.Error(t, err) |
| 139 | + assert.Contains(t, err.Error(), "127.0.0.1:29", |
| 140 | + "command must contact the staging environment URL") |
| 141 | + assert.NotContains(t, err.Error(), "127.0.0.1:9/", |
| 142 | + "command must not contact the base URL when -e staging is given") |
| 143 | + }) |
| 144 | + } |
| 145 | +} |
0 commit comments