Skip to content

Commit 0ec8871

Browse files
committed
Merge branch 'main' into fix/behat-3.15
2 parents 94304b0 + ce7412d commit 0ec8871

28 files changed

+2941
-303
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ phpunit.xml
1212
phpcs.xml
1313
.phpcs.xml
1414
.phpunit.result.cache
15+
.phpunit.cache
16+
build/logs

bin/install-package-tests

Lines changed: 116 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
is_numeric() {
1313
case $1 in
1414
''|*[!0-9]*) return 1;; # returns 1 if not numeric
15-
*) return 0;; # returns 0 if numeric
15+
*) return 0;; # returns 0 if numeric
1616
esac
1717
}
18+
# Promt color vars.
19+
C_RED="\033[31m"
20+
C_BLUE="\033[34m"
21+
NO_FORMAT="\033[0m"
1822

1923
HOST=localhost
2024
PORT=""
@@ -28,83 +32,162 @@ if [ -n "${WP_CLI_TEST_DBHOST}" ]; then
2832
if [ -n "${PORT}" ]; then
2933
# If the port is not numeric, then we assume it is a socket path.
3034
if is_numeric "${PORT}"; then
35+
echo "Connecting to custom host: ${C_BLUE}${HOST}${NO_FORMAT} on port ${C_BLUE}${PORT}${NO_FORMAT}"
3136
HOST_STRING="${HOST_STRING} --port=${PORT} --protocol=tcp"
3237
else
38+
echo "Connecting to custom host: ${C_BLUE}${HOST}${NO_FORMAT} on socket ${C_BLUE}${PORT}${NO_FORMAT}"
3339
HOST_STRING="${HOST_STRING} --socket=${PORT} --protocol=socket"
3440
fi
41+
else
42+
echo "Connecting to custom host: ${C_BLUE}${HOST}${NO_FORMAT}"
3543
fi
44+
else
45+
echo "Connecting to default host: ${C_BLUE}${HOST}${NO_FORMAT}"
3646
fi
3747

3848
USER=root
3949
if [ -n "${WP_CLI_TEST_DBROOTUSER}" ]; then
40-
USER="${WP_CLI_TEST_DBROOTUSER}"
50+
echo "Connecting with custom root user: ${C_BLUE}${WP_CLI_TEST_DBROOTUSER}${NO_FORMAT}"
51+
USER="${WP_CLI_TEST_DBROOTUSER}"
52+
else
53+
echo "Connecting with default root user: ${C_BLUE}${USER}${NO_FORMAT}"
4154
fi
4255

4356
PASSWORD_STRING=""
4457
if [ -n "${WP_CLI_TEST_DBROOTPASS}" ]; then
45-
PASSWORD_STRING="-p${WP_CLI_TEST_DBROOTPASS}"
58+
echo "Connecting with custom root password: ${C_BLUE}${WP_CLI_TEST_DBROOTPASS}${NO_FORMAT}"
59+
PASSWORD_STRING="-p${WP_CLI_TEST_DBROOTPASS}"
60+
else
61+
echo "Connecting with default root password: ${C_BLUE}empty${NO_FORMAT}"
4662
fi
4763

4864
TEST_DB=wp_cli_test
4965
if [ -n "${WP_CLI_TEST_DBNAME}" ]; then
50-
TEST_DB="${WP_CLI_TEST_DBNAME}"
66+
echo "Using custom test database: ${C_BLUE}${WP_CLI_TEST_DBNAME}${NO_FORMAT}"
67+
TEST_DB="${WP_CLI_TEST_DBNAME}"
68+
else
69+
echo "Using default test database: ${C_BLUE}${TEST_DB}${NO_FORMAT}"
5170
fi
5271

5372
TEST_USER=wp_cli_test
5473
if [ -n "${WP_CLI_TEST_DBUSER}" ]; then
55-
TEST_USER="${WP_CLI_TEST_DBUSER}"
74+
echo "Using custom test user: ${C_BLUE}${WP_CLI_TEST_DBUSER}${NO_FORMAT}"
75+
TEST_USER="${WP_CLI_TEST_DBUSER}"
76+
else
77+
echo "Using default test user: ${C_BLUE}${TEST_USER}${NO_FORMAT}"
5678
fi
5779

5880
TEST_PASSWORD=password1
5981
if [ -n "${WP_CLI_TEST_DBPASS}" ]; then
60-
TEST_PASSWORD="${WP_CLI_TEST_DBPASS}"
82+
echo "Using custom test password: ${C_BLUE}${WP_CLI_TEST_DBPASS}${NO_FORMAT}"
83+
TEST_PASSWORD="${WP_CLI_TEST_DBPASS}"
84+
else
85+
echo "Using default test password: ${C_BLUE}${TEST_PASSWORD}${NO_FORMAT}"
6186
fi
6287

63-
echo 'Checking if MySQL is ready...'
64-
while ! mysql ${HOST_STRING} --user="${USER}" "${PASSWORD_STRING}" --execute="SHOW DATABASES;" | grep 'information_schema' >/dev/null;
65-
do
66-
echo 'Waiting for MySQL...'
67-
sleep 5
68-
i=$((i+1))
69-
if [ $i -gt 36 ]; then
70-
echo 'MySQL failed to start. Aborting.'
71-
exit 1
72-
fi
73-
done
88+
echo "Detecting database version..."
89+
90+
TYPE="MySQL"
91+
CLIENT_VERSION=$(mysql --version 2>/dev/null)
92+
93+
case "${CLIENT_VERSION}" in
94+
*"MariaDB"*)
95+
TYPE="MariaDB"
96+
;;
97+
esac
98+
99+
if [ -z "$PS1" ]; then
100+
# These vars are because github actions gave problems in the past.
101+
MYSQL_TRIES=36
102+
MYSQL_WAIT=5
103+
else
104+
MYSQL_TRIES=1
105+
MYSQL_WAIT=0
106+
fi
107+
108+
if [ "${TYPE}" = "MySQL" ]; then
109+
SERVER_VERSION=$(mysql -e "SELECT VERSION()" --skip-column-names ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}")
110+
else
111+
SERVER_VERSION=$(mariadb -e "SELECT VERSION()" --skip-column-names ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}")
112+
fi
113+
114+
VERSION=$(echo "${SERVER_VERSION}" | grep -o '^[^-]*')
115+
MAJOR=$(echo "${VERSION}" | cut -d. -f1)
116+
MINOR=$(echo "${VERSION}" | cut -d. -f2)
117+
118+
echo "Detected ${TYPE} at version ${MAJOR}.${MINOR}"
119+
120+
echo 'Checking if database is ready...'
121+
122+
if [ "${TYPE}" = "MySQL" ]; then
123+
while ! mysql ${HOST_STRING} --user="${USER}" "${PASSWORD_STRING}" --execute="SHOW DATABASES;" | grep 'information_schema' >/dev/null;
124+
do
125+
i=$((i+1))
126+
if [ "${MYSQL_TRIES}" -gt 1 ]; then
127+
echo "Waiting for MySQL(${i}/${MYSQL_TRIES})..."
128+
sleep ${MYSQL_WAIT}
129+
fi
130+
131+
if [ $i -ge $MYSQL_TRIES ]; then
132+
echo "${C_RED}MySQL failed to start. Aborting.${NO_FORMAT}"
133+
echo "Cannot connect to MySQL server. For all available variables, check the documentation at:"
134+
echo " ${C_BLUE}https://github.com/wp-cli/wp-cli-tests?tab=readme-ov-file#the-database-credentials${NO_FORMAT}"
135+
exit 1
136+
fi
137+
done
138+
else
139+
while ! mariadb ${HOST_STRING} --user="${USER}" "${PASSWORD_STRING}" --execute="SHOW DATABASES;" | grep 'information_schema' >/dev/null;
140+
do
141+
i=$((i+1))
142+
if [ "${MYSQL_TRIES}" -gt 1 ]; then
143+
echo "Waiting for MariaDB(${i}/${MYSQL_TRIES})..."
144+
sleep ${MYSQL_WAIT}
145+
fi
146+
147+
if [ $i -ge $MYSQL_TRIES ]; then
148+
echo "${C_RED}MariaDB failed to start. Aborting.${NO_FORMAT}"
149+
echo "Cannot connect to MariaDB server. For all available variables, check the documentation at:"
150+
echo " ${C_BLUE}https://github.com/wp-cli/wp-cli-tests?tab=readme-ov-file#the-database-credentials${NO_FORMAT}"
151+
exit 1
152+
fi
153+
done
154+
fi
74155

75156
# Prepare the database for running the tests with a MySQL version 8.0 or higher.
76157
install_mysql_db_8_0_plus() {
77-
set -ex
158+
set -ex # print all the commands.
78159
mysql -e "CREATE DATABASE IF NOT EXISTS \`${TEST_DB}\`;" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
79160
mysql -e "CREATE USER IF NOT EXISTS \`${TEST_USER}\`@'%' IDENTIFIED WITH caching_sha2_password BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
80161
mysql -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
81162
mysql -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
163+
{ set +ex; } 2> /dev/null # stop printing the commands
82164
}
83165

84166
# Prepare the database for running the tests with a MySQL version lower than 8.0.
85167
install_mysql_db_lower_than_8_0() {
86-
set -ex
168+
set -ex # print all the commands.
87169
mysql -e "CREATE DATABASE IF NOT EXISTS \`${TEST_DB}\`;" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
88170
mysql -e "GRANT ALL ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
89171
mysql -e "GRANT ALL ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
172+
{ set +ex; } 2> /dev/null # stop printing the commands
90173
}
91174

92-
VERSION_STRING=$(mysql -e "SELECT VERSION()" --skip-column-names ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}")
93-
VERSION=$(echo "${VERSION_STRING}" | grep -o '^[^-]*')
94-
MAJOR=$(echo "${VERSION}" | cut -d. -f1)
95-
MINOR=$(echo "${VERSION}" | cut -d. -f2)
96-
TYPE="MySQL"
97-
case "${VERSION_STRING}" in
98-
*"MariaDB"*)
99-
TYPE="MariaDB"
100-
;;
101-
esac
102-
103-
echo "Detected ${TYPE} at version ${MAJOR}.${MINOR}"
104-
175+
# Prepare the database for running the tests with MariaDB
176+
install_mariadb() {
177+
set -ex
178+
mariadb -e "CREATE DATABASE IF NOT EXISTS \`${TEST_DB}\`;" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
179+
mariadb -e "CREATE USER IF NOT EXISTS \`${TEST_USER}\`@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
180+
mariadb -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
181+
mariadb -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
182+
}
105183

106-
if [ "${TYPE}" != "MariaDB" ] && [ "${MAJOR}" -ge 8 ]; then
184+
if [ "${TYPE}" = "MariaDB" ]; then
185+
install_mariadb
186+
elif [ "${MAJOR}" -ge 8 ]; then
107187
install_mysql_db_8_0_plus
108188
else
109189
install_mysql_db_lower_than_8_0
110190
fi
191+
192+
echo "Succesfully prepared the database for running tests."
193+
echo "This command does not have to be run again."

bin/run-phpstan-tests

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
# Run the code style check only if a configuration file exists.
4+
if [ -f "phpstan.dist.neon" ] || [ -f "phpstan.neon.dist" ] || [ -f "phpstan.neon" ]
5+
then
6+
vendor/bin/phpstan --memory-limit=2048M analyse "$@"
7+
fi

composer.json

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,34 @@
99
"license": "MIT",
1010
"type": "phpcodesniffer-standard",
1111
"require": {
12-
"php": ">=5.6",
12+
"php": ">=7.2.24",
1313
"behat/behat": "^v3.15.0",
1414
"dealerdirect/phpcodesniffer-composer-installer": "^0.4.3 || ^0.5 || ^0.6.2 || ^0.7.1 || ^1.0.0",
1515
"php-parallel-lint/php-console-highlighter": "^1.0",
1616
"php-parallel-lint/php-parallel-lint": "^1.3.1",
1717
"phpcompatibility/php-compatibility": "dev-develop",
18+
"phpstan/extension-installer": "^1.4",
19+
"phpstan/phpstan": "^1.12.26",
20+
"phpstan/phpstan-deprecation-rules": "^1.2",
21+
"phpstan/phpstan-phpunit": "^1.4",
22+
"phpstan/phpstan-strict-rules": "^1.6",
23+
"swissspidy/phpstan-no-private": "^0.2.1",
24+
"szepeviktor/phpstan-wordpress": "^v1.3.5",
1825
"wp-cli/config-command": "^1 || ^2",
1926
"wp-cli/core-command": "^1 || ^2",
2027
"wp-cli/eval-command": "^1 || ^2",
21-
"wp-cli/wp-cli": "^2.5.1",
28+
"wp-cli/wp-cli": "^2.12",
2229
"wp-coding-standards/wpcs": "^3",
23-
"yoast/phpunit-polyfills": "^1.0.3 || ^2.0.1"
30+
"yoast/phpunit-polyfills": "^4.0.0"
2431
},
2532
"require-dev": {
2633
"roave/security-advisories": "dev-latest"
2734
},
2835
"config": {
2936
"allow-plugins": {
3037
"dealerdirect/phpcodesniffer-composer-installer": true,
31-
"johnpbloch/wordpress-core-installer": true
38+
"johnpbloch/wordpress-core-installer": true,
39+
"phpstan/extension-installer": true
3240
},
3341
"sort-packages": true,
3442
"lock": false
@@ -37,6 +45,11 @@
3745
"branch-alias": {
3846
"dev-main": "4.0.x-dev"
3947
},
48+
"phpstan": {
49+
"includes": [
50+
"extension.neon"
51+
]
52+
},
4053
"readme": {
4154
"sections": [
4255
"Using",
@@ -54,6 +67,11 @@
5467
"WP_CLI\\Tests\\": "src"
5568
}
5669
},
70+
"autoload-dev": {
71+
"psr-4": {
72+
"WP_CLI\\Tests\\Tests\\": "tests/tests"
73+
}
74+
},
5775
"minimum-stability": "dev",
5876
"prefer-stable": true,
5977
"bin": [
@@ -63,19 +81,22 @@
6381
"bin/run-linter-tests",
6482
"bin/run-php-unit-tests",
6583
"bin/run-phpcs-tests",
66-
"bin/run-phpcbf-cleanup"
84+
"bin/run-phpcbf-cleanup",
85+
"bin/run-phpstan-tests"
6786
],
6887
"scripts": {
6988
"behat": "run-behat-tests",
7089
"behat-rerun": "rerun-behat-tests",
7190
"lint": "run-linter-tests",
7291
"phpcs": "run-phpcs-tests",
7392
"phpcbf": "run-phpcbf-cleanup",
93+
"phpstan": "run-phpstan-tests",
7494
"phpunit": "run-php-unit-tests",
7595
"prepare-tests": "install-package-tests",
7696
"test": [
7797
"@lint",
7898
"@phpcs",
99+
"@phpstan",
79100
"@phpunit",
80101
"@behat"
81102
]

extension.neon

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
services:
2+
-
3+
class: WP_CLI\Tests\PHPStan\ParseUrlFunctionDynamicReturnTypeExtension
4+
tags:
5+
- phpstan.broker.dynamicFunctionReturnTypeExtension
6+
-
7+
class: WP_CLI\Tests\PHPStan\GetFlagValueFunctionDynamicReturnTypeExtension
8+
tags:
9+
- phpstan.broker.dynamicFunctionReturnTypeExtension
10+
-
11+
class: WP_CLI\Tests\PHPStan\WPCliRuncommandDynamicReturnTypeExtension
12+
tags:
13+
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
14+
parameters:
15+
dynamicConstantNames:
16+
- FOO
17+
strictRules:
18+
allRules: false
19+
disallowedLooseComparison: false
20+
booleansInConditions: false
21+
uselessCast: false
22+
requireParentConstructorCall: false
23+
disallowedConstructs: false
24+
overwriteVariablesWithLoop: false
25+
closureUsesThis: false
26+
matchingInheritedMethodNames: false
27+
numericOperandsInArithmeticOperators: false
28+
strictCalls: false
29+
switchConditionsMatchingType: false
30+
noVariableVariables: false
31+
strictArrayFilter: false
32+
33+
# Add the schema from phpstan-strict-rules so it's available without loading the extension
34+
# and the above configuration works.
35+
parametersSchema:
36+
strictRules: structure([
37+
allRules: anyOf(bool(), arrayOf(bool())),
38+
disallowedLooseComparison: anyOf(bool(), arrayOf(bool())),
39+
booleansInConditions: anyOf(bool(), arrayOf(bool()))
40+
uselessCast: anyOf(bool(), arrayOf(bool()))
41+
requireParentConstructorCall: anyOf(bool(), arrayOf(bool()))
42+
disallowedConstructs: anyOf(bool(), arrayOf(bool()))
43+
overwriteVariablesWithLoop: anyOf(bool(), arrayOf(bool()))
44+
closureUsesThis: anyOf(bool(), arrayOf(bool()))
45+
matchingInheritedMethodNames: anyOf(bool(), arrayOf(bool()))
46+
numericOperandsInArithmeticOperators: anyOf(bool(), arrayOf(bool()))
47+
strictCalls: anyOf(bool(), arrayOf(bool()))
48+
switchConditionsMatchingType: anyOf(bool(), arrayOf(bool()))
49+
noVariableVariables: anyOf(bool(), arrayOf(bool()))
50+
strictArrayFilter: anyOf(bool(), arrayOf(bool()))
51+
])

0 commit comments

Comments
 (0)