Skip to content

Commit 7541e96

Browse files
authored
Merge pull request #70 from SparkPost/feature/FAD-3797
Add unit test framework
2 parents 24ec365 + 9006409 commit 7541e96

18 files changed

+2281
-18
lines changed

.coveralls.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
service_name: travis-ci
2+
coverage_clover: tests/coverage/clover.xml
3+
json_path: tests/coverage/coveralls-upload.json

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
.idea
22
.DS_Store
33
.svn
4+
coverage
5+
tmp
6+
tests/vendor

.svnignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@
33
readme.md
44
.idea
55
trunk/.idea
6+
tests
7+
bin
8+
phpunit.xml.dist
9+
.travis.yml
10+
coverage

.travis.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
language: php
2+
3+
notifications:
4+
email:
5+
on_success: never
6+
on_failure: change
7+
8+
branches:
9+
only:
10+
- master
11+
12+
php:
13+
- 5.6
14+
- 7.0
15+
16+
env:
17+
- WP_VERSION=latest WP_MULTISITE=0
18+
- WP_VERSION=4.3 WP_MULTISITE=0
19+
- WP_VERSION=4.4 WP_MULTISITE=0
20+
- WP_VERSION=4.5 WP_MULTISITE=0
21+
- WP_VERSION=4.6 WP_MULTISITE=0
22+
23+
before_script:
24+
- bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
25+
- cd tests
26+
- composer install --no-interaction
27+
28+
script: composer test
29+
30+
after_success:
31+
- cd ../; php tests/vendor/bin/coveralls -c ./.coveralls.yml

README.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
# WordPress SparkPost
22

3-
Use SparkPost emails right from your WordPress site.
3+
Use SparkPost emails right from your WordPress site.
44

55
## Usages
66

77
### Installation
88

99
**Option 1**
1010

11-
* Download the plugin from [WordPress's plugins repository](https://wordpress.org/plugins/sparkpost/).
11+
* Download the plugin from [WordPress's plugins repository](https://wordpress.org/plugins/sparkpost/).
1212
* Upload to plugins directory of your WordPress installation which, usually, is `wp-content/plugins`.
13-
* Activate the plugin from admin panel.
13+
* Activate the plugin from admin panel.
1414

1515
**Option 2**
16-
* From your WordPress site's admin panel go to **Plugins -> Add New**.
17-
* Enter *sparkpost* in *Search Plugins* text field and hit Enter.
18-
* It should show pluging titled *SparkPost SMTP*. Click **Install Now** button.
19-
* In next page, upon successful downloading click **Activate Plugin**.
16+
* From your WordPress site's admin panel go to **Plugins -> Add New**.
17+
* Enter *sparkpost* in *Search Plugins* text field and hit Enter.
18+
* It should show pluging titled *SparkPost SMTP*. Click **Install Now** button.
19+
* In next page, upon successful downloading click **Activate Plugin**.
2020

2121
### Configuration
2222
Once plugin is installed, you need some quick **but important** configuration. Click **SparkPost SMTP** from Settings menu
23-
* In the form put SparkPost API key, sender name and email.
23+
* In the form put SparkPost API key, sender name and email.
2424
* Click **Save Changes**
2525

2626
### Test Email
27-
From Test Email section, try sending a test email to yourself to make sure the credentials are working fine.
27+
From Test Email section, try sending a test email to yourself to make sure the credentials are working fine.
2828

2929

3030
## Development
@@ -37,6 +37,15 @@ git clone git@github.com:SparkPost/wordpress-sparkpost.git ~/src/wordpress/wp-co
3737

3838
* Activate the plugin from admin panel
3939

40+
## Running Tests
41+
* Make sure you're using PHP 5.6 or above.
42+
* Install test files by running `bash bin/install-wp-tests.sh wordpress_test root '' localhost latest`. Details on [wp-cli.org](http://wp-cli.org/docs/plugin-unit-tests/).
43+
* [Install composer](https://getcomposer.org/doc/00-intro.md)
44+
* Go to `./tests` directory.
45+
* Run `composer install` to install required packages.
46+
* To run tests, run `composer test`.
47+
* Add your tests in `tests/specs` directory. Upon pushing the branch, Travis will automatically run it and generate reports (tests and coverage).
48+
4049
## Releasing
4150

4251
* Create a branch off master: `git checkout -b bump`

admin.widget.class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
namespace WPSparkPost;
23
// If ABSPATH is defined, we assume WP is calling us.
34
// Otherwise, this could be an illicit direct request.
45
if (!defined('ABSPATH')) exit();

bin/install-wp-tests.sh

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#!/usr/bin/env bash
2+
3+
if [ $# -lt 3 ]; then
4+
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation]"
5+
exit 1
6+
fi
7+
8+
DB_NAME=$1
9+
DB_USER=$2
10+
DB_PASS=$3
11+
DB_HOST=${4-localhost}
12+
WP_VERSION=${5-latest}
13+
SKIP_DB_CREATE=${6-false}
14+
15+
WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib}
16+
WP_CORE_DIR=${WP_CORE_DIR-/tmp/wordpress/}
17+
18+
download() {
19+
if [ `which curl` ]; then
20+
curl -s "$1" > "$2";
21+
elif [ `which wget` ]; then
22+
wget -nv -O "$2" "$1"
23+
fi
24+
}
25+
26+
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+(\.[0-9]+)? ]]; then
27+
WP_TESTS_TAG="tags/$WP_VERSION"
28+
elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
29+
WP_TESTS_TAG="trunk"
30+
else
31+
# http serves a single offer, whereas https serves multiple. we only want one
32+
download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
33+
grep '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json
34+
LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//')
35+
if [[ -z "$LATEST_VERSION" ]]; then
36+
echo "Latest WordPress version could not be found"
37+
exit 1
38+
fi
39+
WP_TESTS_TAG="tags/$LATEST_VERSION"
40+
fi
41+
42+
set -ex
43+
44+
install_wp() {
45+
46+
if [ -d $WP_CORE_DIR ]; then
47+
return;
48+
fi
49+
50+
mkdir -p $WP_CORE_DIR
51+
52+
if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
53+
mkdir -p /tmp/wordpress-nightly
54+
download https://wordpress.org/nightly-builds/wordpress-latest.zip /tmp/wordpress-nightly/wordpress-nightly.zip
55+
unzip -q /tmp/wordpress-nightly/wordpress-nightly.zip -d /tmp/wordpress-nightly/
56+
mv /tmp/wordpress-nightly/wordpress/* $WP_CORE_DIR
57+
else
58+
if [ $WP_VERSION == 'latest' ]; then
59+
local ARCHIVE_NAME='latest'
60+
else
61+
local ARCHIVE_NAME="wordpress-$WP_VERSION"
62+
fi
63+
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz
64+
tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR
65+
fi
66+
67+
download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
68+
}
69+
70+
install_test_suite() {
71+
# portable in-place argument for both GNU sed and Mac OSX sed
72+
if [[ $(uname -s) == 'Darwin' ]]; then
73+
local ioption='-i .bak'
74+
else
75+
local ioption='-i'
76+
fi
77+
78+
# set up testing suite if it doesn't yet exist
79+
if [ ! -d $WP_TESTS_DIR ]; then
80+
# set up testing suite
81+
mkdir -p $WP_TESTS_DIR
82+
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
83+
fi
84+
85+
if [ ! -f wp-tests-config.php ]; then
86+
download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
87+
# remove all forward slashes in the end
88+
WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::")
89+
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
90+
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
91+
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
92+
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
93+
sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php
94+
fi
95+
96+
}
97+
98+
install_db() {
99+
100+
if [ ${SKIP_DB_CREATE} = "true" ]; then
101+
return 0
102+
fi
103+
104+
# parse DB_HOST for port or socket references
105+
local PARTS=(${DB_HOST//\:/ })
106+
local DB_HOSTNAME=${PARTS[0]};
107+
local DB_SOCK_OR_PORT=${PARTS[1]};
108+
local EXTRA=""
109+
110+
if ! [ -z $DB_HOSTNAME ] ; then
111+
if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then
112+
EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp"
113+
elif ! [ -z $DB_SOCK_OR_PORT ] ; then
114+
EXTRA=" --socket=$DB_SOCK_OR_PORT"
115+
elif ! [ -z $DB_HOSTNAME ] ; then
116+
EXTRA=" --host=$DB_HOSTNAME --protocol=tcp"
117+
fi
118+
fi
119+
120+
# create database
121+
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
122+
}
123+
124+
install_wp
125+
install_test_suite
126+
install_db

mailer.http.class.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
2+
namespace WPSparkPost;
23
// If ABSPATH is defined, we assume WP is calling us.
34
// Otherwise, this could be an illicit direct request.
45
if (!defined('ABSPATH')) exit();
56

6-
77
require_once ABSPATH . WPINC . '/class-phpmailer.php';
88

9-
class SparkPostHTTPMailer extends PHPMailer
9+
class SparkPostHTTPMailer extends \PHPMailer
1010
{
1111
protected $endpoint = 'https://api.sparkpost.com/api/v1/transmissions';
1212
private $settings;
@@ -33,10 +33,10 @@ function __construct($exceptions = false)
3333
*/
3434
protected function mailSend($header, $body)
3535
{
36-
return $this->sparkpostSend();
36+
return $this->sparkpost_send();
3737
}
3838

39-
function sparkpostSend()
39+
function sparkpost_send()
4040
{
4141
$this->edebug('Preparing request data');
4242

mailer.smtp.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
2+
namespace WPSparkPost;
23
// If ABSPATH is defined, we assume WP is calling us.
34
// Otherwise, this could be an illicit direct request.
45
if (!defined('ABSPATH')) exit();
56

6-
77
/**
88
* @package wp-sparkpost
99
*/

sparkpost.class.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
namespace WPSparkPost;
3+
24
// If ABSPATH is defined, we assume WP is calling us.
35
// Otherwise, this could be an illicit direct request.
46
if (!defined('ABSPATH')) exit();

0 commit comments

Comments
 (0)