add unit testing

This commit is contained in:
info@mschuette.name 2014-05-26 13:35:17 +00:00
parent 5d5f3d07eb
commit ec84f3d91b
8 changed files with 324 additions and 0 deletions

17
.travis.yml Normal file
View file

@ -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

80
bin/install-wp-tests.sh Normal file
View file

@ -0,0 +1,80 @@
#!/usr/bin/env bash
if [ $# -lt 3 ]; then
echo "usage: $0 <db-name> <db-user> <db-pass> [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

14
phpunit.xml Normal file
View file

@ -0,0 +1,14 @@
<phpunit
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite>
<directory prefix="test-" suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>

14
tests/bootstrap.php Normal file
View file

@ -0,0 +1,14 @@
<?php
$_tests_dir = getenv('WP_TESTS_DIR');
if ( !$_tests_dir ) $_tests_dir = '/tmp/wordpress-tests-lib';
require_once $_tests_dir . '/includes/functions.php';
function _manually_load_plugin() {
require dirname( __FILE__ ) . '/../freifunkmeta.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
require $_tests_dir . '/includes/bootstrap.php';

41
tests/example_ffffm.json Normal file
View file

@ -0,0 +1,41 @@
{
"name": "Freifunk Frankfurt am Main - ffffm",
"url": "http://www.wifi-frankfurt.de/",
"state": {
"nodes": 4,
"lastchange": 0,
"message": "open for public"
},
"location": {
"city": "Frankfurt am Main",
"lat": 50.110556,
"lon": 8.682222
},
"contact": {
"email": "info@wifi-frankfurt.de",
"irc": "#freifunk on freenode",
"ml": "user@wifi-frankfurt.de",
"twitter": "https://twitter.com/FreiFunkFFM"
},
"techDetails": {
"stoererhaftung": "VPN",
"firmware": {
"url": "http://wiki.freifunk.net/Firmware_Flashen"
},
"networks": {
"ipv4": [
{
"netmask": 16,
"network": "10.126.0.0"
}
]
},
"routing": "BATMAN",
"splashpage": "none"
},
"state": {
"lastchange": 1391267102
},
"api": "0.1"
}

107
tests/example_ffhh.json Normal file
View file

@ -0,0 +1,107 @@
{
"name": "Freifunk Hamburg",
"api": "0.3.0",
"url": "https://hamburg.freifunk.net",
"techDetails": {
"updatemode": "autoupdater",
"bootstrap": "http://formular.hamburg.freifunk.net",
"splashpage": "none",
"firmware": {
"url": "https://hamburg.freifunk.net/anleitung",
"name": "gluon"
},
"routing": "batman-adv",
"tld": {
"nameserver": [
"10.112.1.1",
"10.112.14.1"
],
"domainname": "ffhh"
},
"vpn": "fastd",
"stoererhaftung": "VPN in die Niederlande",
"networks": {
"ipv4": [
{
"network": "10.112.0.0/18"
}
],
"ipv6": [
{
"network": "fd51:2bb2:fd0d::/48"
}
]
}
},
"state": {
"nodes": 429,
"lastchange": "2014-04-27T12:50:01.842182",
"message": "open for public"
},
"contact": {
"ml": "freifunk@hamburg.ccc.de",
"twitter": "@FreifunkHH",
"irc": "irc://irc.hackint.net/ffhh",
"facebook": "https://www.facebook.com/FreifunkHamburg",
"email": "kontakt@hamburg.freifunk.net"
},
"location": {
"lat": 53.574267,
"city": "Hamburg",
"lon": 10.024418,
"address": {
"Street": "Humboldtstr. 53",
"Name": "Chaos Computer Club Hansestadt Hamburg",
"Zipcode": "22083"
}
},
"services": [
{
"internalUri": "xmpp://jabber.ffhh",
"serviceName": "jabber",
"serviceDescription": "Chat"
},
{
"internalUri": "nntp://news.services.ffhh",
"serviceName": "news",
"serviceDescription": "Newsserver"
},
{
"internalUri": "http://radio.ffhh",
"serviceName": "radio",
"serviceDescription": "CC Musik und freisprech podcast"
}
],
"nodeMaps": [
{
"url": "http://knotenkarte.de",
"interval": "1 min",
"technicalType": "ffmap",
"mapType": "geographical"
},
{
"url": "http://knotengraph.de",
"interval": "1 min",
"technicalType": "ffmap",
"mapType": "structural"
},
{
"url": "http://graph.hamburg.freifunk.net/list.html",
"interval": "1 min",
"technicalType": "ffmap",
"mapType": "list/status"
}
],
"feeds": [
{
"category": "blog",
"url": "https://hamburg.freifunk.net/?feed=rss2",
"type": "rss"
},
{
"category": "others",
"url": "https://hamburg.freifunk.net/feed/freisprech-mp3/?feed=rss2",
"type": "rss"
}
]
}

43
tests/test-nonwp.php Normal file
View file

@ -0,0 +1,43 @@
<?php
# low level test of PHP functions & methods w/o WP integration
class LowLevelTest extends PHPUnit_Framework_TestCase {
function setUp() {
$this->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);
}
}

8
tests/test-wpsample.php Normal file
View file

@ -0,0 +1,8 @@
<?php
# all tests with WP integration
class SampleTest extends WP_UnitTestCase {
function test_sample() {
$this->assertTrue(true);
}
}