Skip to content

Commit f282b67

Browse files
committed
Merge branch 'main' into 203-add-phpstan
2 parents 241db60 + 2b45c1d commit f282b67

20 files changed

+1443
-117
lines changed

.github/workflows/testing.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
name: Testing
22

33
on:
4+
workflow_dispatch:
45
pull_request:
56
push:
67
branches:

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ composer.lock
1111
phpunit.xml
1212
phpcs.xml
1313
.phpcs.xml
14+
.phpunit.result.cache
15+
build/logs

bin/install-package-tests

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,60 @@ if [ -n "${WP_CLI_TEST_DBPASS}" ]; then
6060
TEST_PASSWORD="${WP_CLI_TEST_DBPASS}"
6161
fi
6262

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
63+
echo "Detecting database version..."
64+
65+
TYPE="MySQL"
66+
CLIENT_VERSION=$(mysql --version 2>/dev/null)
67+
68+
case "${CLIENT_VERSION}" in
69+
*"MariaDB"*)
70+
TYPE="MariaDB"
71+
;;
72+
esac
73+
74+
if [ "${TYPE}" = "MySQL" ]; then
75+
SERVER_VERSION=$(mysql -e "SELECT VERSION()" --skip-column-names ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}")
76+
else
77+
SERVER_VERSION=$(mariadb -e "SELECT VERSION()" --skip-column-names ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}")
78+
fi
79+
80+
VERSION=$(echo "${SERVER_VERSION}" | grep -o '^[^-]*')
81+
MAJOR=$(echo "${VERSION}" | cut -d. -f1)
82+
MINOR=$(echo "${VERSION}" | cut -d. -f2)
83+
84+
echo "Detected ${TYPE} at version ${MAJOR}.${MINOR}"
85+
86+
echo 'Checking if database is ready...'
87+
88+
if [ "${TYPE}" = "MySQL" ]; then
89+
while ! mysql ${HOST_STRING} --user="${USER}" "${PASSWORD_STRING}" --execute="SHOW DATABASES;" | grep 'information_schema' >/dev/null;
90+
do
91+
echo 'Waiting for database...'
92+
sleep 5
93+
i=$((i+1))
94+
if [ $i -gt 36 ]; then
95+
echo 'Database failed to start. Aborting.'
96+
exit 1
97+
fi
98+
done
99+
else
100+
while ! mariadb ${HOST_STRING} --user="${USER}" "${PASSWORD_STRING}" --execute="SHOW DATABASES;" | grep 'information_schema' >/dev/null;
101+
do
102+
echo 'Waiting for database...'
103+
sleep 5
104+
i=$((i+1))
105+
if [ $i -gt 36 ]; then
106+
echo 'Database failed to start. Aborting.'
107+
exit 1
108+
fi
109+
done
110+
fi
74111

75112
# Prepare the database for running the tests with a MySQL version 8.0 or higher.
76113
install_mysql_db_8_0_plus() {
77114
set -ex
78115
mysql -e "CREATE DATABASE IF NOT EXISTS \`${TEST_DB}\`;" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
79-
mysql -e "CREATE USER IF NOT EXISTS \`${TEST_USER}\`@'%' IDENTIFIED WITH mysql_native_password BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
116+
mysql -e "CREATE USER IF NOT EXISTS \`${TEST_USER}\`@'%' IDENTIFIED WITH caching_sha2_password BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
80117
mysql -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
81118
mysql -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
82119
}
@@ -89,21 +126,18 @@ install_mysql_db_lower_than_8_0() {
89126
mysql -e "GRANT ALL ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
90127
}
91128

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-
129+
# Prepare the database for running the tests with MariaDB
130+
install_mariadb() {
131+
set -ex
132+
mariadb -e "CREATE DATABASE IF NOT EXISTS \`${TEST_DB}\`;" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
133+
mariadb -e "CREATE USER IF NOT EXISTS \`${TEST_USER}\`@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
134+
mariadb -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
135+
mariadb -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
136+
}
105137

106-
if [ "${TYPE}" != "MariaDB" ] && [ "${MAJOR}" -ge 8 ]; then
138+
if [ "${TYPE}" = "MariaDB" ]; then
139+
install_mariadb
140+
elif [ "${MAJOR}" -ge 8 ]; then
107141
install_mysql_db_8_0_plus
108142
else
109143
install_mysql_db_lower_than_8_0

bin/run-php-unit-tests

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@
33
# Run the unit tests, if they exist
44
if [ -f "phpunit.xml" ] || [ -f "phpunit.xml.dist" ] || [ -f ".phpunit.xml" ] || [ -f ".phpunit.xml.dist" ]
55
then
6+
PHPUNIT_VERSION=$(vendor/bin/phpunit --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')
7+
EXTRA_ARGS=""
8+
9+
if [ "${PHPUNIT_VERSION#11}" != "$PHPUNIT_VERSION" ] || [ "${PHPUNIT_VERSION#10}" != "$PHPUNIT_VERSION" ]; then
10+
EXTRA_ARGS="--display-warnings --fail-on-warning --display-notices --fail-on-notice --display-deprecations --fail-on-deprecation"
11+
fi
12+
613
if [ -f "./vendor/wp-cli/wp-cli-tests/tests/bootstrap.php" ]; then
7-
vendor/bin/phpunit --color=always "$@" --bootstrap ./vendor/wp-cli/wp-cli-tests/tests/bootstrap.php
14+
vendor/bin/phpunit --color=always "$@" $EXTRA_ARGS --bootstrap ./vendor/wp-cli/wp-cli-tests/tests/bootstrap.php
815
else
9-
vendor/bin/phpunit --color=always "$@"
16+
vendor/bin/phpunit --color=always "$@" $EXTRA_ARGS
1017
fi
1118
fi

composer.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@
99
"license": "MIT",
1010
"type": "phpcodesniffer-standard",
1111
"require": {
12-
"php": ">=5.6",
12+
"php": ">=7.2.24",
1313
"behat/behat": "^3.7",
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",
17-
"phpcompatibility/php-compatibility": "^9.3.5",
17+
"phpcompatibility/php-compatibility": "dev-develop",
1818
"phpstan/extension-installer": "^1.4.3",
1919
"phpstan/phpstan": "^1.12.26",
2020
"szepeviktor/phpstan-wordpress": "^v1.3.5",
2121
"wp-cli/config-command": "^1 || ^2",
2222
"wp-cli/core-command": "^1 || ^2",
2323
"wp-cli/eval-command": "^1 || ^2",
24-
"wp-cli/wp-cli": "^2.5.1",
24+
"wp-cli/wp-cli": "^2.12",
2525
"wp-coding-standards/wpcs": "^3",
26-
"yoast/phpunit-polyfills": "^1.0.3"
26+
"yoast/phpunit-polyfills": "^1.0.3 || ^2.0.1"
2727
},
2828
"require-dev": {
2929
"roave/security-advisories": "dev-latest"
@@ -34,7 +34,8 @@
3434
"johnpbloch/wordpress-core-installer": true,
3535
"phpstan/extension-installer": true
3636
},
37-
"sort-packages": true
37+
"sort-packages": true,
38+
"lock": false
3839
},
3940
"extra": {
4041
"branch-alias": {

features/http-mocking.feature

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
Feature: HTTP request mocking
2+
3+
Scenario: Mock HTTP request in WP-CLI
4+
Given that HTTP requests to https://api.github.com/repos/wp-cli/wp-cli/releases?per_page=100 will respond with:
5+
"""
6+
HTTP/1.1 200
7+
Content-Type: application/json
8+
9+
[
10+
{
11+
"url": "https://api.github.com/repos/wp-cli/wp-cli/releases/169243978",
12+
"assets_url": "https://api.github.com/repos/wp-cli/wp-cli/releases/169243978/assets",
13+
"upload_url": "https://uploads.github.com/repos/wp-cli/wp-cli/releases/169243978/assets{?name,label}",
14+
"html_url": "https://github.com/wp-cli/wp-cli/releases/tag/v999.9.9",
15+
"id": 169243978,
16+
"author": {
17+
"login": "schlessera",
18+
"id": 83631,
19+
"node_id": "MDQ6VXNlcjgzNjMx",
20+
"avatar_url": "https://avatars.githubusercontent.com/u/83631?v=4",
21+
"gravatar_id": "",
22+
"url": "https://api.github.com/users/schlessera",
23+
"html_url": "https://github.com/schlessera",
24+
"followers_url": "https://api.github.com/users/schlessera/followers",
25+
"following_url": "https://api.github.com/users/schlessera/following{/other_user}",
26+
"gists_url": "https://api.github.com/users/schlessera/gists{/gist_id}",
27+
"starred_url": "https://api.github.com/users/schlessera/starred{/owner}{/repo}",
28+
"subscriptions_url": "https://api.github.com/users/schlessera/subscriptions",
29+
"organizations_url": "https://api.github.com/users/schlessera/orgs",
30+
"repos_url": "https://api.github.com/users/schlessera/repos",
31+
"events_url": "https://api.github.com/users/schlessera/events{/privacy}",
32+
"received_events_url": "https://api.github.com/users/schlessera/received_events",
33+
"type": "User",
34+
"user_view_type": "public",
35+
"site_admin": false
36+
},
37+
"node_id": "RE_kwDOACQFs84KFnVK",
38+
"tag_name": "v999.9.9",
39+
"target_commitish": "main",
40+
"name": "Version 999.9.9",
41+
"draft": false,
42+
"prerelease": false,
43+
"created_at": "2024-08-08T03:04:55Z",
44+
"published_at": "2024-08-08T03:51:13Z",
45+
"assets": [
46+
{
47+
"url": "https://api.github.com/repos/wp-cli/wp-cli/releases/assets/184590231",
48+
"id": 184590231,
49+
"node_id": "RA_kwDOACQFs84LAJ-X",
50+
"name": "wp-cli-999.9.9.phar",
51+
"label": null,
52+
"content_type": "application/octet-stream",
53+
"state": "uploaded",
54+
"size": 7048108,
55+
"download_count": 722639,
56+
"created_at": "2024-08-08T03:51:05Z",
57+
"updated_at": "2024-08-08T03:51:08Z",
58+
"browser_download_url": "https://github.com/wp-cli/wp-cli/releases/download/v999.9.9/wp-cli-999.9.9.phar"
59+
}
60+
],
61+
"tarball_url": "https://api.github.com/repos/wp-cli/wp-cli/tarball/v999.9.9",
62+
"zipball_url": "https://api.github.com/repos/wp-cli/wp-cli/zipball/v999.9.9",
63+
"body": "- Allow manually dispatching tests workflow [[#5965](https://github.com/wp-cli/wp-cli/pull/5965)]\r\n- Add fish shell completion [[#5954](https://github.com/wp-cli/wp-cli/pull/5954)]\r\n- Add defaults and accepted values for runcommand() options in doc [[#5953](https://github.com/wp-cli/wp-cli/pull/5953)]\r\n- Address warnings with filenames ending in fullstop on Windows [[#5951](https://github.com/wp-cli/wp-cli/pull/5951)]\r\n- Fix unit tests [[#5950](https://github.com/wp-cli/wp-cli/pull/5950)]\r\n- Update copyright year in license [[#5942](https://github.com/wp-cli/wp-cli/pull/5942)]\r\n- Fix breaking multi-line CSV values on reading [[#5939](https://github.com/wp-cli/wp-cli/pull/5939)]\r\n- Fix broken Gutenberg test [[#5938](https://github.com/wp-cli/wp-cli/pull/5938)]\r\n- Update docker runner to resolve docker path using `/usr/bin/env` [[#5936](https://github.com/wp-cli/wp-cli/pull/5936)]\r\n- Fix `inherit` path in nested directory [[#5930](https://github.com/wp-cli/wp-cli/pull/5930)]\r\n- Minor docblock improvements [[#5929](https://github.com/wp-cli/wp-cli/pull/5929)]\r\n- Add Signup fetcher [[#5926](https://github.com/wp-cli/wp-cli/pull/5926)]\r\n- Ensure the alias has the leading `@` symbol when added [[#5924](https://github.com/wp-cli/wp-cli/pull/5924)]\r\n- Include any non default hook information in CompositeCommand [[#5921](https://github.com/wp-cli/wp-cli/pull/5921)]\r\n- Correct completion case when ends in = [[#5913](https://github.com/wp-cli/wp-cli/pull/5913)]\r\n- Docs: Fixes for inline comments [[#5912](https://github.com/wp-cli/wp-cli/pull/5912)]\r\n- Update Inline comments [[#5910](https://github.com/wp-cli/wp-cli/pull/5910)]\r\n- Add a real-world example for `wp cli has-command` [[#5908](https://github.com/wp-cli/wp-cli/pull/5908)]\r\n- Fix typos [[#5901](https://github.com/wp-cli/wp-cli/pull/5901)]\r\n- Avoid PHP deprecation notices in PHP 8.1.x [[#5899](https://github.com/wp-cli/wp-cli/pull/5899)]",
64+
"reactions": {
65+
"url": "https://api.github.com/repos/wp-cli/wp-cli/releases/169243978/reactions",
66+
"total_count": 9,
67+
"+1": 4,
68+
"-1": 0,
69+
"laugh": 0,
70+
"hooray": 1,
71+
"confused": 0,
72+
"heart": 0,
73+
"rocket": 4,
74+
"eyes": 0
75+
}
76+
}
77+
]
78+
"""
79+
80+
When I try `wp cli check-update --format=csv`
81+
Then STDOUT should contain:
82+
"""
83+
999.9.9,major,https://github.com/wp-cli/wp-cli/releases/download/v999.9.9/wp-cli-999.9.9.phar,available
84+
"""
85+
86+
Scenario: Mock HTTP request in WordPress
87+
Given a WP install
88+
And that HTTP requests to https://api.wordpress.org/core/version-check/1.7/ will respond with:
89+
"""
90+
HTTP/1.1 200
91+
Content-Type: application/json
92+
93+
{
94+
"offers": [
95+
{
96+
"response": "latest",
97+
"download": "https:\/\/downloads.wordpress.org\/release\/wordpress-999.9.9.zip",
98+
"locale": "en_US",
99+
"packages": {
100+
"full": "https:\/\/downloads.wordpress.org\/release\/wordpress-999.9.9.zip",
101+
"no_content": "https:\/\/downloads.wordpress.org\/release\/wordpress-999.9.9-no-content.zip",
102+
"new_bundled": "https:\/\/downloads.wordpress.org\/release\/wordpress-999.9.9-new-bundled.zip",
103+
"partial": false,
104+
"rollback": false
105+
},
106+
"current": "999.9.9",
107+
"version": "999.9.9",
108+
"php_version": "7.2.24",
109+
"mysql_version": "5.5.5",
110+
"new_bundled": "6.7",
111+
"partial_version": false
112+
}
113+
],
114+
"translations": []
115+
}
116+
"""
117+
118+
When I run `wp core check-update`
119+
Then STDOUT should be a table containing rows:
120+
| version | update_type | package_url |
121+
| 999.9.9 | major | https://downloads.wordpress.org/release/wordpress-999.9.9.zip |

features/steps.feature

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
Feature: Make sure "Given", "When", "Then" steps work as expected
2+
3+
Scenario: Variable names can only contain uppercase letters, digits and underscores and cannot begin with a digit.
4+
5+
When I run `echo value`
6+
Then save STDOUT as {VARIABLE_NAME}
7+
And save STDOUT as {V}
8+
And save STDOUT as {_VARIABLE_NAME_STARTING_WITH_UNDERSCORE}
9+
And save STDOUT as {_}
10+
And save STDOUT as {VARIABLE_NAME_WITH_DIGIT_2}
11+
And save STDOUT as {V2}
12+
And save STDOUT as {_2}
13+
And save STDOUT as {2_VARIABLE_NAME_STARTING_WITH_DIGIT}
14+
And save STDOUT as {2}
15+
And save STDOUT as {VARIABLE_NAME_WITH_lowercase}
16+
And save STDOUT as {v}
17+
# Note this would give behat "undefined step" message as "save" step uses "\w+"
18+
#And save STDOUT as {VARIABLE_NAME_WITH_PERCENT_%}
19+
20+
When I run `echo {VARIABLE_NAME}`
21+
Then STDOUT should match /^value$/
22+
And STDOUT should be:
23+
"""
24+
value
25+
"""
26+
27+
When I run `echo {V}`
28+
Then STDOUT should match /^value$/
29+
30+
When I run `echo {_VARIABLE_NAME_STARTING_WITH_UNDERSCORE}`
31+
Then STDOUT should match /^value$/
32+
33+
When I run `echo {_}`
34+
Then STDOUT should match /^value$/
35+
36+
When I run `echo {VARIABLE_NAME_WITH_DIGIT_2}`
37+
Then STDOUT should match /^value$/
38+
39+
When I run `echo {V2}`
40+
Then STDOUT should match /^value$/
41+
42+
When I run `echo {_2}`
43+
Then STDOUT should match /^value$/
44+
45+
When I run `echo {2_VARIABLE_NAME_STARTING_WITH_DIGIT}`
46+
Then STDOUT should match /^\{2_VARIABLE_NAME_STARTING_WITH_DIGIT}$/
47+
And STDOUT should contain:
48+
"""
49+
{
50+
"""
51+
52+
When I run `echo {2}`
53+
Then STDOUT should match /^\{2}$/
54+
55+
When I run `echo {VARIABLE_NAME_WITH_lowercase}`
56+
Then STDOUT should match /^\{VARIABLE_NAME_WITH_lowercase}$/
57+
58+
When I run `echo {v}`
59+
Then STDOUT should match /^\{v}$/
60+
61+
Scenario: Special variables
62+
63+
When I run `echo {INVOKE_WP_CLI_WITH_PHP_ARGS-} cli info`
64+
Then STDOUT should match /wp cli info/
65+
And STDERR should be empty
66+
67+
When I run `echo {WP_VERSION-latest}`
68+
Then STDOUT should match /\d\.\d/
69+
And STDERR should be empty
70+
71+
@require-mysql-or-mariadb
72+
Scenario: SQL related variables
73+
When I run `echo {MYSQL_BINARY}`
74+
Then STDOUT should match /(mysql|mariadb)/
75+
76+
When I run `echo {SQL_DUMP_COMMAND}`
77+
Then STDOUT should match /(mysqldump|mariadb-dump)/

features/testing.feature

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ Feature: Test that WP-CLI loads.
1212
Scenario: WP Cron is disabled by default
1313
Given a WP install
1414
And a test_cron.php file:
15-
"""
16-
<?php
17-
$cron_disabled = defined( "DISABLE_WP_CRON" ) ? DISABLE_WP_CRON : false;
18-
echo 'DISABLE_WP_CRON is: ' . ( $cron_disabled ? 'true' : 'false' );
19-
"""
15+
"""
16+
<?php
17+
$cron_disabled = defined( "DISABLE_WP_CRON" ) ? DISABLE_WP_CRON : false;
18+
echo 'DISABLE_WP_CRON is: ' . ( $cron_disabled ? 'true' : 'false' );
19+
"""
2020

2121
When I run `wp eval-file test_cron.php`
2222
Then STDOUT should be:

0 commit comments

Comments
 (0)