11#! /bin/bash
22
3+ #
4+ # Test-execution helper.
5+ #
6+ # This can be used both locally and in GHA workflows.
7+ #
8+ # Usage examples:
9+ # ./tests.sh
10+ # 1. Setup test services (Redis, RabbitMQ, etc.).
11+ # 2. Run tests in default path.
12+ # 3. Tear down test services.
13+ # ./tests.sh ./my/test/dir
14+ # 1. Setup test services (Redis, RabbitMQ, etc.).
15+ # 2. Run tests in specified path.
16+ # 3. Tear down test services.
17+ # ./tests.sh -s infra:setup|infra:teardown
18+ # 1. Only Setup or tear down test services (Redis, RabbitMQ, etc.).
19+ #
20+ # This is a bit complex to allow all-in-one testing (locally, for example) or
21+ # doing required stages separately (in GHA, where we want services
22+ # setup/teardown to be in separate workflow steps).
23+
324set -e
425
526cd $( dirname $0 ) /..
627
7- TEST_PATH=" ${1:- ./ tests/ suite} "
28+ # Constants.
29+ STAGE_INFRA_SETUP=' infra:setup'
30+ STAGE_INFRA_TEARDOWN=' infra:teardown'
31+ STAGE_TESTS=' tests'
32+
33+ # Defaults.
34+ STAGE=" "
35+ TEST_PATH=" ./tests/suite"
36+
37+ # Parse options
38+ while getopts " :s:" opt; do
39+ case $opt in
40+ s)
41+ STAGE=" $OPTARG "
42+ ;;
43+ \? )
44+ echo " Invalid option: -$OPTARG " >&2
45+ exit 1
46+ ;;
47+ :)
48+ echo " Option -$OPTARG requires an argument." >&2
49+ exit 1
50+ ;;
51+ esac
52+ done
53+ shift $(( OPTIND - 1 ))
54+
55+ # Positional argument (TEST_PATH)
56+ if [ $# -ge 1 ]; then
57+ TEST_PATH=" $1 "
58+ fi
59+
60+ if [[ -n " $STAGE " && " $STAGE " != " $STAGE_INFRA_SETUP " && " $STAGE " != " $STAGE_INFRA_TEARDOWN " && " $STAGE " != " $STAGE_TESTS " ]]; then
61+ echo " Error: $STAGE must be '$STAGE_INFRA_SETUP ' or '$STAGE_INFRA_TEARDOWN ' or '$STAGE_TESTS '"
62+ exit 1
63+ fi
864
965function _info {
1066 echo -e " ▄"
@@ -16,25 +72,47 @@ function _compose {
1672 docker compose -f ./tests/infra/docker/test-services.yml $@
1773}
1874
19- _info " Maybe cleaning up previous test services"
20- _compose down --timeout 0 1> /dev/null 2>&1 || true
21-
22- _info " Starting test services"
23- _compose up \
24- --detach \
25- --wait \
26- --quiet-pull \
27- --build
28-
29- _info " Running tests"
30- php ./vendor/nette/tester/src/tester \
31- -C ` # Use system-wide php-ini` \
32- --coverage ./tests/output/coverage.html \
33- --coverage-src ./src \
34- --log ./tests/output/tests.log \
35- -p phpdbg \
36- $TEST_PATH \
37- || true # Continue even with failed tests.
38-
39- _info " Stopping test services"
40- _compose down --timeout 2
75+ # Only if STAGE is empty or during a setup stage.
76+ if [[ -z " $STAGE " || " $STAGE " == " $STAGE_INFRA_SETUP " ]]; then
77+
78+ _info " Maybe cleaning up previous test services"
79+ _compose down --timeout 0 1> /dev/null 2>&1 || true
80+
81+ _info " Starting test services"
82+ _compose up \
83+ --detach \
84+ --wait \
85+ --quiet-pull \
86+ --build
87+
88+ fi
89+
90+ # NOTE: We would use Nette Tester argument "-p phpdbg" to run tests with phpdbg,
91+ # but phpdbg8.1 fails with segfaults. When we're past PHP 8.1, we can use phpdbg
92+ # again.
93+ TESTS_EXIST_CODE=0
94+
95+ # Only execute if STAGE is empty if tests should be executed.
96+ if [[ -z " $STAGE " || " $STAGE " == " $STAGE_TESTS " ]]; then
97+
98+ _info " Running tests"
99+ php ./vendor/nette/tester/src/tester \
100+ -C ` # Use system-wide php-ini` \
101+ -o console-lines \
102+ --coverage ./tests/output/coverage.html \
103+ --coverage-src ./src \
104+ --log ./tests/output/tests.log \
105+ $TEST_PATH \
106+ || TESTS_EXIST_CODE=$? # Continue even with failed tests but obtain exit code.
107+
108+ _info " Tests exit code: $TESTS_EXIST_CODE "
109+
110+ fi
111+
112+ # Only if STAGE is empty or during a teardown stage.
113+ if [[ -z " $STAGE " || " $STAGE " == " $STAGE_INFRA_TEARDOWN " ]]; then
114+ _info " Stopping test services"
115+ _compose down --timeout 2
116+ fi
117+
118+ exit $TESTS_EXIST_CODE
0 commit comments