Skip to content

Commit 9f1dc44

Browse files
authored
Workflow
1 parent f3030b1 commit 9f1dc44

File tree

1 file changed

+223
-2
lines changed

1 file changed

+223
-2
lines changed

.github/workflows/php74-latest-wp-test.yml

Lines changed: 223 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,232 @@ jobs:
6161
# Drop the database if it exists to avoid creation error
6262
mysql -u root --password=root --host=127.0.0.1 --port=3306 -e "DROP DATABASE IF EXISTS wordpress_test;"
6363
mysql -u root --password=root --host=127.0.0.1 --port=3306 -e "CREATE DATABASE IF NOT EXISTS wordpress_test;"
64+
65+
- name: Create tests directory structure
66+
run: |
67+
mkdir -p tests/bin
68+
mkdir -p tests/bootstrap
69+
70+
- name: Create WP tests install script
71+
run: |
72+
cat > tests/bin/install-wp-tests.sh << 'EOF'
73+
#!/usr/bin/env bash
74+
75+
if [ $# -lt 3 ]; then
76+
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation]"
77+
exit 1
78+
fi
79+
80+
DB_NAME=$1
81+
DB_USER=$2
82+
DB_PASS=$3
83+
DB_HOST=${4-localhost}
84+
WP_VERSION=${5-latest}
85+
SKIP_DB_CREATE=${6-false}
86+
87+
WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib}
88+
WP_CORE_DIR=${WP_CORE_DIR-/tmp/wordpress/}
89+
90+
download() {
91+
if [ $(which curl) ]; then
92+
curl -s "$1" > "$2";
93+
elif [ $(which wget) ]; then
94+
wget -nv -O "$2" "$1"
95+
fi
96+
}
97+
98+
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+(\.[0-9]+)? ]]; then
99+
WP_TESTS_TAG="tags/$WP_VERSION"
100+
elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
101+
WP_TESTS_TAG="trunk"
102+
else
103+
# http serves a single offer, whereas https serves multiple. we only want one
104+
download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
105+
LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//')
106+
if [[ -z "$LATEST_VERSION" ]]; then
107+
echo "Latest WordPress version could not be found"
108+
exit 1
109+
fi
110+
WP_TESTS_TAG="tags/$LATEST_VERSION"
111+
fi
112+
113+
set -ex
114+
115+
install_wp() {
116+
117+
if [ -d $WP_CORE_DIR ]; then
118+
return;
119+
fi
120+
121+
mkdir -p $WP_CORE_DIR
122+
123+
if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
124+
mkdir -p /tmp/wordpress-nightly
125+
download https://wordpress.org/nightly-builds/wordpress-latest.zip /tmp/wordpress-nightly/wordpress-nightly.zip
126+
unzip -q /tmp/wordpress-nightly/wordpress-nightly.zip -d /tmp/wordpress-nightly/
127+
mv /tmp/wordpress-nightly/wordpress/* $WP_CORE_DIR
128+
else
129+
if [ $WP_VERSION == 'latest' ]; then
130+
local ARCHIVE_NAME='latest'
131+
else
132+
local ARCHIVE_NAME="wordpress-$WP_VERSION"
133+
fi
134+
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz
135+
tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR
136+
fi
137+
138+
download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
139+
}
140+
141+
install_test_suite() {
142+
# portable in-place argument for both GNU sed and Mac OSX sed
143+
if [[ $(uname -s) == 'Darwin' ]]; then
144+
local ioption='-i.bak'
145+
else
146+
local ioption='-i'
147+
fi
148+
149+
# set up testing suite if it doesn't yet exist
150+
if [ ! -d $WP_TESTS_DIR ]; then
151+
# set up testing suite
152+
mkdir -p $WP_TESTS_DIR
153+
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
154+
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data
155+
fi
156+
157+
if [ ! -f wp-tests-config.php ]; then
158+
download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
159+
# remove all forward slashes in the end
160+
WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::")
161+
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
162+
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
163+
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
164+
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
165+
sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php
166+
fi
167+
}
168+
169+
install_db() {
170+
if [ ${SKIP_DB_CREATE} = "true" ]; then
171+
return 0
172+
fi
173+
174+
# parse DB_HOST for port or socket references
175+
local PARTS=(${DB_HOST//\:/ })
176+
local DB_HOSTNAME=${PARTS[0]};
177+
local DB_SOCK_OR_PORT=${PARTS[1]};
178+
local EXTRA=""
179+
180+
if ! [ -z $DB_HOSTNAME ] ; then
181+
if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then
182+
EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp"
183+
elif ! [ -z $DB_SOCK_OR_PORT ] ; then
184+
EXTRA=" --socket=$DB_SOCK_OR_PORT"
185+
elif ! [ -z $DB_HOSTNAME ] ; then
186+
EXTRA=" --host=$DB_HOSTNAME --protocol=tcp"
187+
fi
188+
fi
189+
190+
# Create database
191+
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
192+
}
193+
194+
install_wp
195+
install_test_suite
196+
install_db
197+
EOF
198+
chmod +x tests/bin/install-wp-tests.sh
199+
200+
- name: Create Bootstrap File
201+
run: |
202+
mkdir -p tests
203+
cat > tests/bootstrap.php << 'EOF'
204+
<?php
205+
/**
206+
* PHPUnit bootstrap file for plugin tests.
207+
*
208+
* @package Simple_WP_Optimizer
209+
*/
210+
211+
// Define global $wp_widget_factory to prevent null reference errors
212+
global $wp_widget_factory;
213+
$wp_widget_factory = (object) ['widgets' => []];
214+
215+
// Add a mock register_widget function that works with our fake widget factory
216+
if (!function_exists('register_widget')) {
217+
function register_widget($widget) {
218+
global $wp_widget_factory;
219+
$wp_widget_factory->widgets[] = $widget;
220+
return true;
221+
}
222+
}
223+
224+
// Give access to tests_add_filter() function.
225+
require_once '/tmp/wordpress-tests-lib/includes/functions.php';
226+
227+
/**
228+
* Manually load the plugin being tested.
229+
*/
230+
function _manually_load_plugin() {
231+
require dirname( dirname( __FILE__ ) ) . '/simple-wp-optimizer.php';
232+
}
233+
234+
// Start up the WP testing environment.
235+
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
236+
237+
require '/tmp/wordpress-tests-lib/includes/bootstrap.php';
238+
EOF
239+
240+
- name: Create Test File
241+
run: |
242+
cat > tests/test-plugin.php << 'EOF'
243+
<?php
244+
/**
245+
* Class Test_Simple_WP_Optimizer
246+
*
247+
* @package Simple_WP_Optimizer
248+
*/
249+
250+
/**
251+
* Simple test case for Simple WP Optimizer plugin.
252+
*/
253+
class Test_Simple_WP_Optimizer extends WP_UnitTestCase {
254+
/**
255+
* Test that the plugin can be loaded correctly.
256+
*
257+
* This test simply checks that the plugin loads in WordPress
258+
* without causing any errors.
259+
*/
260+
public function test_plugin_loaded() {
261+
// Check for at least one function to verify the plugin loaded
262+
$this->assertTrue(function_exists('es_optimizer_init_settings'), 'Plugin was not loaded correctly');
263+
}
264+
}
265+
EOF
266+
267+
- name: Create PHPUnit Config
268+
run: |
269+
cat > phpunit.xml << 'EOF'
270+
<?xml version="1.0"?>
271+
<phpunit
272+
bootstrap="tests/bootstrap.php"
273+
backupGlobals="false"
274+
colors="true"
275+
convertErrorsToExceptions="true"
276+
convertNoticesToExceptions="true"
277+
convertWarningsToExceptions="true"
278+
>
279+
<testsuites>
280+
<testsuite name="Simple WP Optimizer">
281+
<directory prefix="test-" suffix=".php">./tests/</directory>
282+
</testsuite>
283+
</testsuites>
284+
</phpunit>
285+
EOF
64286
65287
- name: Setup WP Tests
66288
run: |
67-
bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1:3306 latest
68-
working-directory: ${{ github.workspace }}/tests
289+
bash tests/bin/install-wp-tests.sh wordpress_test root root 127.0.0.1:3306 latest
69290
70291
- name: Run plugin test
71292
run: vendor/bin/phpunit --config phpunit.xml

0 commit comments

Comments
 (0)