Skip to content

Add first integration tests #435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/integration_test.yaml
Original file line number Diff line number Diff line change
@@ -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/...
22 changes: 22 additions & 0 deletions integration/hello_world/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2024-present Fabien Potencier <fabien@symfony.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/

<?php

echo 'Hello world!';
37 changes: 37 additions & 0 deletions integration/integration_cleanup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//go:build integration
// +build integration

/*
* Copyright (c) 2024-present Fabien Potencier <fabien@symfony.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/

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)
}
}
65 changes: 65 additions & 0 deletions integration/local_server_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//go:build integration
// +build integration

/*
* Copyright (c) 2024-present Fabien Potencier <fabien@symfony.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/

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:]...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to get more control I would rather directly use the code here (starting the server in a goroutine and issuing http request, worst case: starting the application instead of directly the HTTP server).

Copy link
Contributor Author

@alexandre-daubois alexandre-daubois Feb 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you do this in all functional tests or only this one? About issuing HTTP request, it is only relevant for starting and stopping the server (and not listing running ones), right?

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)
}
48 changes: 48 additions & 0 deletions integration/local_server_start_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//go:build integration
// +build integration

/*
* Copyright (c) 2024-present Fabien Potencier <fabien@symfony.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/

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)
}
117 changes: 117 additions & 0 deletions integration/local_server_stop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//go:build integration
// +build integration

/*
* Copyright (c) 2024-present Fabien Potencier <fabien@symfony.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/

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)
}
}
22 changes: 22 additions & 0 deletions integration/phpinfo/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2024-present Fabien Potencier <fabien@symfony.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/

<?php

echo phpinfo();
Loading