Skip to content

Commit 5fd7ef4

Browse files
committed
Enhancement: Run end-to-end test asserting HTTP response status code
1 parent d8fdb48 commit 5fd7ef4

File tree

8 files changed

+260
-6
lines changed

8 files changed

+260
-6
lines changed

.github/workflows/integrate.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
uses: "shivammathur/setup-php@v2"
2828
with:
2929
coverage: "none"
30-
extensions: "none, json, mbstring, tokenizer"
30+
extensions: "none, curl, json, mbstring, tokenizer"
3131
php-version: "${{ matrix.php-version }}"
3232

3333
- name: "Set up problem matchers for PHP"
@@ -64,6 +64,9 @@ jobs:
6464
php-version:
6565
- "8.2"
6666

67+
env:
68+
HTTP_HOST: "localhost:8080"
69+
6770
steps:
6871
- name: "Checkout"
6972
uses: "actions/checkout@v4"
@@ -72,11 +75,14 @@ jobs:
7275
uses: "shivammathur/setup-php@v2"
7376
with:
7477
coverage: "none"
75-
extensions: "none"
78+
extensions: "none, curl"
7679
php-version: "${{ matrix.php-version }}"
7780

7881
- name: "Set up problem matchers for PHP"
7982
run: "echo \"::add-matcher::${{ runner.tool_cache }}/php.json\""
8083

84+
- name: "Start built-in web server for PHP"
85+
run: "php -S ${{ env.HTTP_HOST }} .router.php &"
86+
8187
- name: "Run tests"
8288
run: "php tests/run-tests.php -j3 -q --show-diff"

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ backend/mirror.gif
44
backend/mirror.png
55
backend/mirror.jpg
66
backend/GeoIP.dat
7+
tests/server.log
8+
tests/server.pid
79
.php-cs-fixer.cache

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
.EXPORT_ALL_VARIABLES:
2+
3+
HTTP_HOST:=localhost:8080
4+
15
.PHONY: help
26
help: ## Displays this list of targets with descriptions
37
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}'
@@ -7,8 +11,8 @@ coding-standards: vendor ## Fixes code style issues with friendsofphp/php-cs-fix
711
vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php --diff --verbose
812

913
.PHONY: tests
10-
tests: ## Runs tests
11-
php tests/run-tests.php -j3 -q
14+
tests: vendor ## Runs tests
15+
tests/server start; php tests/run-tests.php -j3 -q; tests/server stop
1216

1317
vendor: composer.json composer.lock
1418
composer validate --strict

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"php": "~8.2.0"
1212
},
1313
"require-dev": {
14+
"ext-curl": "*",
1415
"friendsofphp/php-cs-fixer": "^3.40.2"
1516
},
1617
"autoload": {

composer.lock

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
--TEST--
2+
paths return HTTP response status code 200
3+
--FILE--
4+
<?php
5+
6+
declare(strict_types=1);
7+
8+
$httpHost = getenv('HTTP_HOST');
9+
10+
if (!is_string($httpHost)) {
11+
throw new \RuntimeException('Environment variable "HTTP_HOST" is not set.');
12+
}
13+
14+
$pathToRoot = realpath(__DIR__ . '/../..');
15+
16+
$pathsToFiles = [
17+
...glob($pathToRoot . '/*.php'),
18+
...glob($pathToRoot . '/archive/*.php'),
19+
...glob($pathToRoot . '/conferences/*.php'),
20+
...glob($pathToRoot . '/license/*.php'),
21+
...glob($pathToRoot . '/manual/*.php'),
22+
...glob($pathToRoot . '/manual/en/*.php'),
23+
...glob($pathToRoot . '/releases/*.php'),
24+
...glob($pathToRoot . '/releases/*/*.php'),
25+
...glob($pathToRoot . '/releases/*/*/*.php'),
26+
];
27+
28+
$paths = str_replace($pathToRoot, '', $pathsToFiles);
29+
30+
$baseUrl = sprintf(
31+
'http://%s',
32+
$httpHost,
33+
);
34+
35+
$pathsToStatusCodes = array_combine(
36+
$paths,
37+
array_map(static function (string $url) use ($baseUrl): int {
38+
$handle = curl_init();
39+
40+
$options = [
41+
CURLOPT_RETURNTRANSFER => true,
42+
CURLOPT_URL => sprintf(
43+
'%s%s',
44+
$baseUrl,
45+
$url,
46+
),
47+
];
48+
49+
curl_setopt_array($handle, $options);
50+
51+
curl_exec($handle);
52+
53+
return curl_getinfo($handle, CURLINFO_HTTP_CODE);
54+
}, $paths),
55+
);
56+
57+
$pathsWithUnexpectedStatusCodes = array_filter($pathsToStatusCodes, static function (int $statusCode): bool {
58+
return !in_array($statusCode, [200, 301, 302], true);
59+
});
60+
61+
var_dump($pathsWithUnexpectedStatusCodes);
62+
?>
63+
--EXPECT--
64+
array(0) {
65+
}

tests/php.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[PHP]
2+
3+
display_errors = Off

tests/server

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
#!/bin/bash
2+
3+
# https://github.com/cubny/php-built-in-server-manager/blob/9a5cbeaad50a108d6058b882b83ba23fbd7722a9/server
4+
5+
# default hostname
6+
HOST=localhost
7+
# default port number
8+
PORT=8080
9+
# script name
10+
NAME=${0##*/}
11+
12+
usage () {
13+
cat <<EOF
14+
15+
$NAME (PHP built-in web server manager) Version 0.2.0
16+
PHP builtin server manager on port $PORT
17+
18+
usage: ./$NAME <command> [<hostname>:<port>]
19+
20+
Available commands:
21+
22+
start Starts PHP built-in web server server on specified hostname:port, default is localhost:$PORT
23+
stop Stops the PHP built-in web server
24+
restart Stops and Starts on previously specified hostname:port
25+
status Status of "$NAME" process
26+
log Show the PHP built-in web server logs. Use the -f option for a live update
27+
28+
29+
report bugs to [email protected]
30+
$NAME homepage: <https://github.com/cubny/php-built-in-server-manager>
31+
32+
EOF
33+
return 0
34+
}
35+
36+
setup_colors() {
37+
38+
if which tput >/dev/null 2>&1; then
39+
ncolors=$(tput colors)
40+
fi
41+
if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
42+
RED="$(tput setaf 1)"
43+
GREEN="$(tput setaf 2)"
44+
YELLOW="$(tput setaf 3)"
45+
BLUE="$(tput setaf 4)"
46+
BOLD="$(tput bold)"
47+
NORMAL="$(tput sgr0)"
48+
else
49+
RED=""
50+
GREEN=""
51+
YELLOW=""
52+
BLUE=""
53+
BOLD=""
54+
NORMAL=""
55+
fi
56+
}
57+
58+
# if no command specified exit and show usage
59+
if [[ $# < 1 ]]; then
60+
echo $NAME: no command specified
61+
usage
62+
exit 1
63+
fi
64+
65+
# if hostname:port specified override defaults
66+
if [[ $# > 1 ]]; then
67+
IFS=':' read -r -a hostport <<< "$2"
68+
if [[ ! -z "${hostport[0]}" ]]; then
69+
HOST=${hostport[0]}
70+
fi
71+
if [[ ! -z "${hostport[1]}" ]]; then
72+
PORT=${hostport[1]}
73+
fi
74+
fi
75+
76+
# pidfile contents would be hostname:port:pid
77+
PIDFILE=tests/server.pid
78+
LOGFILE=tests/server.log
79+
80+
validate_server () {
81+
which php &> /dev/null
82+
if [[ $? -eq 1 ]]; then
83+
printf "${YELLOW}Error: PHP not found. ${NORMAL}Please install PHP version 5.4 or greater!\n"
84+
return 1
85+
fi
86+
87+
php -h | grep -q -- '-S'
88+
if [[ $? -eq 1 ]]; then
89+
printf "${YELLOW}Error: PHP version must be 5.4 or greater!${NORMAL}\n"
90+
return 1
91+
fi
92+
93+
return 0
94+
}
95+
96+
start_server () {
97+
validate_server
98+
99+
if [[ $? -eq 1 ]]; then
100+
return 1
101+
fi
102+
103+
if [[ -e "$PIDFILE" ]]; then
104+
printf "${YELLOW}Server seems to be running!${NORMAL}\n"
105+
echo
106+
echo if not, there is probably a zombie "$PIDFILE" in this directory.
107+
echo if you are sure no server is running just remove "$PIDFILE" manually and start again
108+
return 1
109+
else
110+
printf "${GREEN}"$NAME" started on $HOST:$PORT${NORMAL}\n"
111+
php -S "$HOST":"$PORT" -c tests/php.ini >> "$LOGFILE" 2>&1 &
112+
echo "$HOST":"$PORT":$! > $PIDFILE
113+
return 0
114+
fi
115+
}
116+
117+
read_pidfile() {
118+
if [[ -e "$PIDFILE" ]]; then
119+
PIDFILECONTENT=`cat "$PIDFILE"`
120+
IFS=: read HOST PORT PID <<< "$PIDFILECONTENT:"
121+
return 0
122+
else
123+
return 1
124+
fi
125+
}
126+
127+
stop_server () {
128+
if read_pidfile; then
129+
kill -9 "$PID"
130+
rm -f "$PIDFILE"
131+
printf "${GREEN}"$NAME" stopped!${NORMAL}\n"
132+
return 0
133+
else
134+
printf "${YELLOW}"$NAME" is not running!${NORMAL}\n"
135+
return 1
136+
fi
137+
}
138+
139+
status_server() {
140+
if read_pidfile && kill -0 "$PID" ; then
141+
printf "${BLUE}"$NAME" is running on ${HOST}:${PORT}${NORMAL}\n"
142+
else
143+
printf "${YELLOW}"$NAME" is not running!${NORMAL}\n"
144+
fi
145+
}
146+
147+
148+
log_server() {
149+
if read_pidfile && kill -0 "$PID" ; then
150+
if [[ "$1" = "-f" ]]; then
151+
TAIL_OPTS="-f"
152+
fi
153+
tail $TAIL_OPTS "$LOGFILE"
154+
else
155+
printf "${YELLOW}"$NAME" is not running!${NORMAL}\n"
156+
fi
157+
}
158+
159+
160+
setup_colors
161+
162+
case $1 in
163+
start) start_server;;
164+
stop) stop_server;;
165+
restart) stop_server; start_server ;;
166+
status) status_server;;
167+
log) log_server $2;;
168+
-h) usage ;;
169+
--help) usage ;;
170+
*) usage;;
171+
esac

0 commit comments

Comments
 (0)