Skip to content

Commit

Permalink
First pass at unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattheu committed Oct 7, 2014
1 parent d3219e7 commit c707eee
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 1 deletion.
78 changes: 78 additions & 0 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/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

# create database
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
}

install_wp
install_test_suite
install_db
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name":"humanmade/high-resoloution-images-with-srcset",
"description":"Automatic high resolution retina images using srcset.",
"homepage":"https://github.com/humanmade/wordpress-srcset",
"keywords":[
"wordpress", "retina", "images", "srcset", "picturefill"
],
"license":"GPL-2.0+",
"authors":[
{
"name":"Human Made Limited",
"email":"support@humanmade.co.uk",
"homepage":"http://hmn.md/"
}
],
"autoload":{
"files":[
"high-resoloution-images-with-srcset.php"
]
},
"require": {
"composer/installers": "~1.0"
}
}
2 changes: 1 addition & 1 deletion high-resoloution-images-with-srcset.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class HM_WordPress_Srcset {
function __construct() {

$this->plugin_url = plugin_dir_url( __FILE__ );
$this->multipliers = apply_filters( 'hm_wp_srcset', array( 1, 2 ) );
$this->multipliers = apply_filters( 'hm_wp_srcset', array( 2 ) );

register_activation_hook( __FILE__ , array( $this, 'plugin_activation_check' ) );

Expand Down
14 changes: 14 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -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__ ) . '/../high-resoloution-images-with-srcset.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

require $_tests_dir . '/includes/bootstrap.php';

Binary file added tests/resources/image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
89 changes: 89 additions & 0 deletions tests/testSrcset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

class FieldTestCase extends WP_UnitTestCase {

private $attachment_id;

function setUp() {

parent::setUp();

add_image_size( 'test', '100', '100', true );
add_image_size( 'test2x', '200', '200', true );

$filepath = __DIR__ . '/resources/image.jpg';
$this->attachment_id = $this->create_attachment_from_local_file( $filepath );

if ( ! is_numeric( $this->attachment_id ) ) {
wp_die( 'Creating test image failed' );
}

}

function tearDown() {
wp_delete_attachment( $this->attachment_id, true );
unset( $this->attachment_id );
parent::tearDown();
}

function test2x() {

$image = wp_get_attachment_image( $this->attachment_id );
$result = preg_match( "/srcset=\"(.+?)\"/u", $image, $matches );

print_r( $image );
if ( ! $result ) {
$this->assertFalse( 'srcset attribute not created.' );
}

// Convert the 'srcset' attribute to array of single srcsets and multiplier
$srcset = array_map( function( $single_srcset ) {

$multiplier = preg_match( '/ (\d+?)x/', $single_srcset, $multiplier_matches );

if ( ! $multiplier ) {
$this->assertFalse( 'srcset multiplier not set.' );
}

return array(
'multiplier' => $multiplier_matches[1],
'src' => trim( str_replace( $multiplier_matches[0] , '', $single_srcset ) ),
);

}, explode( ',', $matches[1] ) );


print_r( $srcset );
}

function create_attachment_from_local_file( $filepath ) {

$uploads = wp_upload_dir();
$filetype = wp_check_filetype( basename( $filepath ), null );

copy( $filepath, trailingslashit( $uploads['path'] ) . basename( $filepath ) );
$filepath = trailingslashit( $uploads['path'] ) . basename( $filepath );

// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => trailingslashit( $wp_upload_dir['url'] ) . basename( $filepath ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filepath ) ),
'post_content' => '',
'post_status' => 'inherit'
);

$attachment_id = wp_insert_attachment( $attachment, $filepath );

// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );

// Generate the metadata for the attachment, and update the database record.
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filepath );
wp_update_attachment_metadata( $attachment_id, $attachment_data );

return $attachment_id;

}

}
19 changes: 19 additions & 0 deletions travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
language: php

php:
- 5.2
- 5.3
- 5.4
- 5.5
- hhvm

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

0 comments on commit c707eee

Please sign in to comment.