Skip to content

Commit bfceb00

Browse files
authored
Merge pull request #4 from smuuf/improve_support_for_redis_sentinel
Improved (fixed) support for Redis Sentinel
2 parents 488c6ac + a6ee8d9 commit bfceb00

20 files changed

Lines changed: 344 additions & 105 deletions

.github/workflows/php.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@ jobs:
2424
with:
2525
php-version: ${{ matrix.php-versions }}
2626
coverage: xdebug
27-
- name: Prepare
27+
- name: "Prepare: Install PHP dependencies + prepare scripts"
2828
run: |
2929
composer install --optimize-autoloader
3030
chmod +x ./bin/*.sh
31+
- name: "Infra: Setup services"
32+
run: ./bin/tests.sh -s infra:setup
3133
- name: Run tests
32-
run: ./bin/tests.sh
34+
run: ./bin/tests.sh -s tests
3335
env:
3436
C4P_TESTS_TASK_MESSAGE_PROTOCOL_VERSION: ${{ matrix.c4p-tests-task-message-protocol-version }}
3537
C4P_TESTS_BROKER_DRIVER: ${{ matrix.c4p-tests-broker-driver }}
3638
C4P_TESTS_BACKEND_DRIVER: ${{ matrix.c4p-tests-backend-driver }}
39+
- name: "Infra: Tear down services"
40+
run: ./bin/tests.sh -s infra:teardown
3741
- name: Run code style analysis
3842
continue-on-error: false
3943
run: ./bin/code-style.sh

bin/phpstan.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/bash
22

33
cd $(dirname $0)/../
4-
./vendor/bin/phpstan analyze --level=5 src
4+
./vendor/bin/phpstan analyze --configuration ./phpstan.neon

bin/tests.sh

Lines changed: 101 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,66 @@
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+
324
set -e
425

526
cd $(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

965
function _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

composer.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
"symfony/polyfill-php81": "^1.26"
2323
},
2424
"require-dev": {
25-
"nette/tester": "^2.4",
26-
"phpstan/phpstan": "^1.8",
27-
"predis/predis": "^2",
28-
"php-amqplib/php-amqplib": "^3.6",
25+
"nette/tester": "^2.5",
26+
"phpstan/phpstan": "^2.1",
27+
"predis/predis": "^3.0",
28+
"php-amqplib/php-amqplib": "^3.7",
2929
"friendsofphp/php-cs-fixer": "^3.37"
3030
},
3131
"suggest": {
32-
"predis/predis": "Adds support for Predis - a PHP library providing a Redis result backend"
32+
"predis/predis": "Adds support for Predis - a PHP library providing a Redis broker and result backend",
33+
"php-amqplib/php-amqplib": "Adds support for php-amqplib - a PHP library providing RabbitMQ broker"
3334
}
3435
}

0 commit comments

Comments
 (0)