From ec84f3d91bd5ea0560af54b9e5fe24d16fabb08a Mon Sep 17 00:00:00 2001 From: "info@mschuette.name" Date: Mon, 26 May 2014 13:35:17 +0000 Subject: [PATCH] add unit testing --- .travis.yml | 17 +++++++ bin/install-wp-tests.sh | 80 +++++++++++++++++++++++++++++ phpunit.xml | 14 +++++ tests/bootstrap.php | 14 +++++ tests/example_ffffm.json | 41 +++++++++++++++ tests/example_ffhh.json | 107 +++++++++++++++++++++++++++++++++++++++ tests/test-nonwp.php | 43 ++++++++++++++++ tests/test-wpsample.php | 8 +++ 8 files changed, 324 insertions(+) create mode 100644 .travis.yml create mode 100644 bin/install-wp-tests.sh create mode 100644 phpunit.xml create mode 100644 tests/bootstrap.php create mode 100644 tests/example_ffffm.json create mode 100644 tests/example_ffhh.json create mode 100644 tests/test-nonwp.php create mode 100644 tests/test-wpsample.php diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8db483c --- /dev/null +++ b/.travis.yml @@ -0,0 +1,17 @@ +language: php + +php: + - 5.3 + - 5.4 + - 5.5 + +env: + - WP_VERSION=latest WP_MULTISITE=0 + - WP_VERSION=latest WP_MULTISITE=1 + - WP_VERSION=3.8 WP_MULTISITE=0 + - WP_VERSION=3.8 WP_MULTISITE=1 + +before_script: + - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION + +script: phpunit diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh new file mode 100644 index 0000000..397fa68 --- /dev/null +++ b/bin/install-wp-tests.sh @@ -0,0 +1,80 @@ +#!/usr/bin/env bash + +if [ $# -lt 3 ]; then + echo "usage: $0 [db-host] [wp-version]" + exit 1 +fi + +DB_NAME=$1 +DB_USER=$2 +DB_PASS=$3 +DB_HOST=${4-localhost} +WP_VERSION=${5-latest} + +WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib} +WP_CORE_DIR=/tmp/wordpress/ + +set -ex + +install_wp() { + mkdir -p $WP_CORE_DIR + + if [ $WP_VERSION == 'latest' ]; then + local ARCHIVE_NAME='latest' + else + local ARCHIVE_NAME="wordpress-$WP_VERSION" + fi + + wget -nv -O /tmp/wordpress.tar.gz http://wordpress.org/${ARCHIVE_NAME}.tar.gz + tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR + + wget -nv -O $WP_CORE_DIR/wp-content/db.php https://raw.github.com/markoheijnen/wp-mysqli/master/db.php +} + +install_test_suite() { + # portable in-place argument for both GNU sed and Mac OSX sed + if [[ $(uname -s) == 'Darwin' ]]; then + local ioption='-i .bak' + else + local ioption='-i' + fi + + # set up testing suite + mkdir -p $WP_TESTS_DIR + cd $WP_TESTS_DIR + svn co --quiet http://develop.svn.wordpress.org/trunk/tests/phpunit/includes/ + + wget -nv -O wp-tests-config.php http://develop.svn.wordpress.org/trunk/wp-tests-config-sample.php + sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR':" wp-tests-config.php + sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" wp-tests-config.php + sed $ioption "s/yourusernamehere/$DB_USER/" wp-tests-config.php + sed $ioption "s/yourpasswordhere/$DB_PASS/" wp-tests-config.php + sed $ioption "s|localhost|${DB_HOST}|" wp-tests-config.php +} + +install_db() { + # parse DB_HOST for port or socket references + local PARTS=(${DB_HOST//\:/ }) + local DB_HOSTNAME=${PARTS[0]}; + local DB_SOCK_OR_PORT=${PARTS[1]}; + local EXTRA="" + + if ! [ -z $DB_HOSTNAME ] ; then + if [[ "$DB_SOCK_OR_PORT" =~ ^[0-9]+$ ]] ; then + EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp" + elif ! [ -z $DB_SOCK_OR_PORT ] ; then + EXTRA=" --socket=$DB_SOCK_OR_PORT" + elif ! [ -z $DB_HOSTNAME ] ; then + EXTRA=" --host=$DB_HOSTNAME --protocol=tcp" + fi + fi + + # delete old database + mysqladmin drop $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA + # create database + mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA +} + +install_wp +install_test_suite +install_db diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..44f0fdb --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,14 @@ + + + + ./tests/ + + + diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..c23c9cf --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,14 @@ +FFM = new FF_Meta(); + } + + function test_output_ff_state() { + $ret = $this->FFM->output_ff_state(array("state" => array("nodes" => 429))); + $this->assertRegExp('/429/', $ret); + } + + function test_basic_json_parsing() { + $json = file_get_contents(__DIR__.'/example_ffhh.json'); + $data = json_decode($json, $assoc = true); + + $this->assertArrayHasKey('name', $data); + $this->assertArrayHasKey('state', $data); + $this->assertArrayHasKey('location', $data); + $this->assertArrayHasKey('services', $data); + } + + function test_externaldata() { + $json = file_get_contents(__DIR__.'/example_ffhh.json'); + $stubdata = json_decode($json, $assoc = true); + + $stub = $this->getMockBuilder('ff_meta_externaldata') + ->disableOriginalConstructor() + ->getMock(); + $stub->expects($this->any()) + ->method('get') + ->will($this->returnValue($stubdata)); + + $data = $stub->get('http://meta.hamburg.freifunk.net/ffhh.json'); + + $this->assertArrayHasKey('name', $data); + $this->assertArrayHasKey('state', $data); + $this->assertArrayHasKey('location', $data); + $this->assertArrayHasKey('services', $data); + } + +} \ No newline at end of file diff --git a/tests/test-wpsample.php b/tests/test-wpsample.php new file mode 100644 index 0000000..a71e228 --- /dev/null +++ b/tests/test-wpsample.php @@ -0,0 +1,8 @@ +assertTrue(true); + } +} \ No newline at end of file