diff --git a/.github/workflows/integration_test.yaml b/.github/workflows/integration_test.yaml new file mode 100644 index 00000000..d79ea417 --- /dev/null +++ b/.github/workflows/integration_test.yaml @@ -0,0 +1,41 @@ +name: Integration tests + +on: + pull_request: + push: + +jobs: + tests: + name: Integration test + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: '^1.21.1' + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.3 + + - uses: actions/cache@v3 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + - name: Build SUT + run: | + go build . + + - name: Run tests + run: go test -v --tags=integration ./integration/... diff --git a/integration/hello_world/index.php b/integration/hello_world/index.php new file mode 100644 index 00000000..698b8c4d --- /dev/null +++ b/integration/hello_world/index.php @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024-present Fabien Potencier + * + * This file is part of Symfony CLI project + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + + + * + * This file is part of Symfony CLI project + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package integration + +import ( + "os/exec" + "testing" +) + +func Cleanup(t *testing.T) { + cmd := exec.Command("../symfony-cli", "server:stop", "--all") + err := cmd.Run() + + if err != nil { + t.Errorf("Error cleaning up integration test %s: %s", t.Name(), err) + } +} diff --git a/integration/local_server_list_test.go b/integration/local_server_list_test.go new file mode 100644 index 00000000..4feca0b2 --- /dev/null +++ b/integration/local_server_list_test.go @@ -0,0 +1,65 @@ +//go:build integration +// +build integration + +/* + * Copyright (c) 2024-present Fabien Potencier + * + * This file is part of Symfony CLI project + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package integration + +import ( + "os/exec" + "regexp" + "strings" + "testing" +) + +func TestServerList(t *testing.T) { + startCommands := []string{ + "../symfony-cli server:start -d --dir=phpinfo/", + "../symfony-cli server:start -d --dir=hello_world/", + } + + for _, command := range startCommands { + cmd := exec.Command(strings.Split(command, " ")[0], strings.Split(command, " ")[1:]...) + err := cmd.Run() + if err != nil { + t.Errorf("Error running command: %s", err) + } + } + + cmd := exec.Command("../symfony-cli", "server:list", "--no-ansi") + output, err := cmd.Output() + if err != nil { + t.Errorf("Error listing servers: %s", err) + } + + expectedMatches := []string{ + "(.+)symfony-cli/integration/phpinfo/ | 8000", + "(.+)symfony-cli/integration/hello_world/ | 8001", + } + + for _, match := range expectedMatches { + matched, _ := regexp.Match(match, output) + if !matched { + t.Errorf("Expected server to be running while matching: %s", match) + } + } + + Cleanup(t) +} diff --git a/integration/local_server_start_test.go b/integration/local_server_start_test.go new file mode 100644 index 00000000..9e9b9680 --- /dev/null +++ b/integration/local_server_start_test.go @@ -0,0 +1,48 @@ +//go:build integration +// +build integration + +/* + * Copyright (c) 2024-present Fabien Potencier + * + * This file is part of Symfony CLI project + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package integration + +import ( + "net/http" + "os/exec" + "testing" +) + +func TestServerStartDaemon(t *testing.T) { + cmd := exec.Command("../symfony-cli", "server:start", "-d", "--dir=phpinfo/") + err := cmd.Run() + if err != nil { + t.Errorf("Error running command: %s", err) + } + + r, err := http.Head("http://localhost:8000") + if err != nil { + t.Errorf("Error sending request: %s", err) + } + + if r.StatusCode != 200 { + t.Errorf("Expected status code 200, got %d", r.StatusCode) + } + + Cleanup(t) +} diff --git a/integration/local_server_stop_test.go b/integration/local_server_stop_test.go new file mode 100644 index 00000000..31eb9e91 --- /dev/null +++ b/integration/local_server_stop_test.go @@ -0,0 +1,117 @@ +//go:build integration +// +build integration + +/* + * Copyright (c) 2024-present Fabien Potencier + * + * This file is part of Symfony CLI project + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package integration + +import ( + "os/exec" + "regexp" + "strings" + "testing" +) + +func TestServerStop_WithDir(t *testing.T) { + cmd := exec.Command("../symfony-cli", "server:start", "-d", "--dir=phpinfo/") + err := cmd.Run() + if err != nil { + t.Errorf("Error starting server: %s", err) + } + + // explicitly stop the server "contained" in the "phpinfo" directory + cmd = exec.Command("../symfony-cli", "server:stop", "--dir=phpinfo/") + err = cmd.Run() + if err != nil { + t.Errorf("Error stopping server: %s", err) + } + + cmd = exec.Command("../symfony-cli", "server:list", "--no-ansi") + output, _ := cmd.Output() + + if strings.Contains(string(output), "integration/phpinfo") { + t.Errorf("Expected no servers to be running, got %s", output) + + Cleanup(t) + } +} + +func TestServerStop_WithAll(t *testing.T) { + startCommands := []string{ + "../symfony-cli server:start -d --dir=phpinfo/", + "../symfony-cli server:start -d --dir=hello_world/", + } + + for _, command := range startCommands { + cmd := exec.Command(strings.Split(command, " ")[0], strings.Split(command, " ")[1:]...) + err := cmd.Run() + if err != nil { + t.Errorf("Error running command: %s", err) + } + } + + // only give the --all flag, which should stop all servers + cmd := exec.Command("../symfony-cli", "server:stop", "--all") + err := cmd.Run() + if err != nil { + t.Errorf("Error stopping server: %s", err) + } + + cmd = exec.Command("../symfony-cli", "server:list", "--no-ansi") + output, _ := cmd.Output() + + expectedToNotMatch := []string{ + "(.+)symfony-cli/integration/phpinfo/", + "(.+)symfony-cli/integration/hello_world/", + } + + for _, match := range expectedToNotMatch { + matched, _ := regexp.Match(match, output) + if matched { + t.Errorf("Expected server NOT to be running while matching: %s", match) + } + } +} + +func TestServerStop_CurrentDir(t *testing.T) { + cmd := exec.Command("../symfony-cli", "server:start", "-d", "--dir=phpinfo/") + + err := cmd.Run() + if err != nil { + t.Errorf("Error starting server: %s", err) + } + + cmd = exec.Command("../../symfony-cli", "server:stop") + // change the working directory to the "phpinfo" directory so the server can be stopped + // without needing to specify the --dir flag + cmd.Dir = "phpinfo/" + + err = cmd.Run() + if err != nil { + t.Errorf("Error stopping server: %s", err) + } + + cmd = exec.Command("../symfony-cli", "server:list", "--no-ansi") + output, _ := cmd.Output() + + if strings.Contains(string(output), "symfony-cli/integration/phpinfo") { + t.Errorf("Expected no servers to be running, got %s", output) + } +} diff --git a/integration/phpinfo/index.php b/integration/phpinfo/index.php new file mode 100644 index 00000000..6d6344b5 --- /dev/null +++ b/integration/phpinfo/index.php @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024-present Fabien Potencier + * + * This file is part of Symfony CLI project + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +