8000 Add some tests · glengemann/symfony-cli@df11feb · GitHub
[go: up one dir, main page]

Skip to content

Commit df11feb

Browse files
committed
Add some tests
1 parent 5e6c0fa commit df11feb

File tree

19 files changed

+223
-11
lines changed

19 files changed

+223
-11
lines changed

commands/db_versions.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package commands
22

33
import (
4+
"errors"
45
"io/ioutil"
6+
"os"
57
"path/filepath"
68
"strings"
79

@@ -40,7 +42,12 @@ func readDBVersionFromPlatformServiceYAML(projectDir string) (string, string, er
4042
}
4143

4244
func readDBVersionFromDotEnv(projectDir string) (string, error) {
43-
dotEnv, err := ioutil.ReadFile(filepath.Join(projectDir, ".env"))
45+
path := filepath.Join(projectDir, ".env")
46+
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
47+
return "", nil
48+
}
49+
50+
dotEnv, err := ioutil.ReadFile(path)
4451
if err != nil {
4552
return "", err
4653
}
@@ -59,7 +66,12 @@ func readDBVersionFromDotEnv(projectDir string) (string, error) {
5966
}
6067

6168
func readDBVersionFromDoctrineConfigYAML(projectDir string) (string, error) {
62-
doctrineConfigYAML, err := ioutil.ReadFile(filepath.Join(projectDir, "config", "packages", "doctrine.yaml"))
69+
path := filepath.Join(projectDir, "config", "packages", "doctrine.yaml")
70+
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
71+
return "", nil
72+
}
73+
74+
doctrineConfigYAML, err := ioutil.ReadFile(path)
6375
if err != nil {
6476
return "", err
6577
}

commands/local_new_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ func TestParseDockerComposeServices(t *testing.T) {
3838
}
3939

4040
for dir, expected := range map[string]CloudService{
41-
"testdata/postgresql/noversion/": {
41+
"testdata/docker/postgresql/noversion/": {
4242
Name: "database",
4343
Type: "postgresql",
4444
Version: lastVersion,
4545
},
46-
"testdata/postgresql/10/": {
46+
"testdata/docker/postgresql/10/": {
4747
Name: "database",
4848
Type: "postgresql",
4949
Version: "10",
5050
},
51-
"testdata/postgresql/next/": {
51+
"testdata/docker/postgresql/next/": {
5252
Name: "database",
5353
Type: "postgresql",
5454
Version: lastVersion,

commands/platformsh_hooks.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1+
/*
2+
* Copyright (c) 2021-present Fabien Potencier <fabien@symfony.com>
3+
*
4+
* This file is part of Symfony CLI project
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as
8+
* published by the Free Software Foundation, either version 3 of the
9+
* License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
120
package commands
221

322
import (
4-
"errors"
523
"fmt"
624

725
"github.com/symfony-cli/console"
@@ -17,7 +35,7 @@ var platformshBeforeHooks = map[string]console.BeforeFunc{
1735
if err != nil {
1836
return err
1937
}
20-
if len(platformsh.FindLocalApplications(projectDir)) > 0 {
38+
if len(platformsh.FindLocalApplications(projectDir)) > 1 {
2139
// not implemented yet as more complex
2240
return nil
2341
}
@@ -49,23 +67,24 @@ Before deploying, fix the version mismatch.
4967

5068
doctrineConfigVersion, err := readDBVersionFromDoctrineConfigYAML(projectDir)
5169
if err != nil {
70+
fmt.Printf("%+v", err)
5271
return nil
5372
}
5473
if doctrineConfigVersion != "" && doctrineConfigVersion != dbVersion {
5574
return fmt.Errorf(errorTpl, fmt.Sprintf("the \"config/packages/doctrine.yaml\" file requires version %s", doctrineConfigVersion))
5675
}
5776

5877
if dotEnvVersion == "" && doctrineConfigVersion == "" {
59-
return errors.New(`
78+
return fmt.Errorf(`
6079
The ".platform/services.yaml" file defines a "%s" database service.
6180
6281
When deploying, Doctrine needs to know the database version to determine the supported SQL syntax.
6382
6483
As the database is not available when Doctrine is warming up its cache on Platform.sh,
65-
you need to explicitely set the database version in the ".env" or "config/packages/doctrine.yaml" file.
84+
you need to explicitly set the database version in the ".env" or "config/packages/doctrine.yaml" file.
6685
67-
The easiest is to set the "serverVersion" parameter in the "config/packages/doctrine.yaml" file.
68-
`)
86+
Set the "server_version" parameter to "%s" in "config/packages/doctrine.yaml".
87+
`, dbName, dbVersion)
6988
}
7089

7190
return nil

commands/platformsh_hooks_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2023-present Fabien Potencier <fabien@symfony.com>
3+
*
4+
* This file is part of Symfony CLI project
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as
8+
* published by the Free Software Foundation, either version 3 of the
9+
* License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
package commands
21+
22+
import (
23+
"flag"
24+
"strings"
25+
"testing"
26+
27+
"github.com/symfony-cli/console"
28+
)
29+
30+
func TestDeployHook(t *testing.T) {
31+
flags := flag.NewFlagSet("test", 0)
32+
flags.String("dir", "", "")
33+
c := console.NewContext(nil, flags, nil)
34+
35+
for dir, expected := range map[string]string{
36+
"testdata/platformsh/version-mismatch-env/": `The ".platform/services.yaml" file defines a "postgresql" version 14 database service but the ".env" file requires version 13.`,
37+
"testdata/platformsh/version-mismatch-config/": `The ".platform/services.yaml" file defines a "postgresql" version 14 database service but the "config/packages/doctrine.yaml" file requires version 13.`,
38+
"testdata/platformsh/ok/": ``,
39+
"testdata/platformsh/missing-version/": `Set the "server_version" parameter to "14" in "config/packages/doctrine.yaml".`,
40+
} {
41+
flags.Set("dir", dir)
42+
err := platformshBeforeHooks["cloud:environment:push"](c)
43+
if err == nil {
44+
if expected != "" {
45+
t.Errorf("TestDeployHook(%q): got %v, expected %v", dir, err, expected)
46+
}
47+
continue
48+
}
49+
errString := strings.ReplaceAll(err.Error(), "\n", " ")
50+
if !strings.Contains(errString, expected) {
51+
t.Errorf("TestDeployHook(%q): got %s, expected %s", dir, errString, expected)
52+
}
53+
}
54+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
###> doctrine/doctrine-bundle ###
2+
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
3+
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
4+
#
5+
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
6+
# DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name?serverVersion=5.7"
7+
DATABASE_URL="postgresql://db_user:db_password@127.0.0.1:5432/db_name?charset=utf8"
8+
###< doctrine/doctrine-bundle ###
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pgsqldb:
2+
type: postgresql:14
3+
disk: 512

commands/testdata/platformsh/ok/.env

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
###> doctrine/doctrine-bundle ###
2+
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
3+
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
4+
#
5+
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
6+
# DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name?serverVersion=5.7"
7+
DATABASE_URL="postgresql://db_user:db_password@127.0.0.1:5432/db_name?serverVersion=14&charset=utf8"
8+
###< doctrine/doctrine-bundle ###

0 commit comments

Comments
 (0)
0