Skip to content

Commit

Permalink
Adding unit tests
Browse files Browse the repository at this point in the history
Updating .gitignore and .distignore files
  • Loading branch information
jonathanbossenger committed May 27, 2018
1 parent 0e9851e commit e398d22
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 3 deletions.
7 changes: 6 additions & 1 deletion .distignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/bin
/tests
.git
.gitignore
.idea
.idea
travis.yml
phpcs.xml.dist
phpunit.xml.dist
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.bin
.idea
*.iml
.gitignore
67 changes: 67 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

language: php

notifications:
email:
on_success: never
on_failure: change

branches:
only:
- master

cache:
directories:
- vendor
- $HOME/.composer/cache

matrix:
include:
- php: 7.1
env: WP_VERSION=latest
- php: 7.0
env: WP_VERSION=latest
- php: 5.6
env: WP_VERSION=4.4
- php: 5.6
env: WP_VERSION=latest
- php: 5.6
env: WP_VERSION=trunk
- php: 5.6
env: WP_TRAVISCI=phpcs
- php: 5.3
env: WP_VERSION=latest

before_script:
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- |
if [ -f ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini ]; then
phpenv config-rm xdebug.ini
else
echo "xdebug.ini does not exist"
fi
- |
if [[ ! -z "$WP_VERSION" ]] ; then
bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
if [[ ${TRAVIS_PHP_VERSION:0:2} == "5." ]]; then
composer global require "phpunit/phpunit=4.8.*"
else
composer global require "phpunit/phpunit=5.7.*"
fi
fi
- |
if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then
composer global require wp-coding-standards/wpcs
phpcs --config-set installed_paths $HOME/.composer/vendor/wp-coding-standards/wpcs
fi
script:
- |
if [[ ! -z "$WP_VERSION" ]] ; then
phpunit
WP_MULTISITE=1 phpunit
fi
- |
if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then
phpcs
fi
17 changes: 17 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<ruleset name="WordPress Coding Standards for Plugins">
<description>Generally-applicable sniffs for WordPress plugins</description>

<rule ref="WordPress-Core" />
<rule ref="WordPress-Docs" />

<!-- Check all PHP files in directory tree by default. -->
<arg name="extensions" value="php"/>
<file>.</file>

<!-- Show progress and sniff codes in all reports -->
<arg value="ps"/>

<exclude-pattern>*/node_modules/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
</ruleset>
14 changes: 14 additions & 0 deletions phpunit.xml.dist
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>
5 changes: 3 additions & 2 deletions seriously-simple-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
require_once( 'ssp-includes/ssp-functions.php' );
}

if( is_ssp_active( '1.13.1' ) ) {
if ( is_ssp_active( '1.13.1' ) ) {

// Load plugin class files
require_once( 'includes/class-ssp-stats.php' );
Expand All @@ -42,8 +42,9 @@
* @since 1.0.0
* @return object SSP_Stats
*/
function SSP_Stats () {
function SSP_Stats() {
$instance = SSP_Stats::instance( __FILE__, SSP_STATS_VERSION, '1.0.0' );

return $instance;
}

Expand Down
30 changes: 30 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* PHPUnit bootstrap file
*
* @package Seriously_Simple_Stats
*/

$_tests_dir = getenv( 'WP_TESTS_DIR' );

if ( ! $_tests_dir ) {
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
}

if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
throw new Exception( "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?" );
}

// Give access to tests_add_filter() function.
require_once $_tests_dir . '/includes/functions.php';

/**
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
require dirname( dirname( __FILE__ ) ) . '/seriously-simple-stats.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';
26 changes: 26 additions & 0 deletions tests/test-functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Class FunctionsTest
*
* Used to test all generic functions
*
* @package Seriously_Simple_Stats
*/

/**
* Sample test case.
*/
class FunctionsTest extends WP_UnitTestCase {

/**
* A single example test.
*/
public function test_ss_stats_anonymise_ip() {
$ip = '192.168.0.233';
$new_ip = ss_stats_anonymise_ip( $ip );
// check that the string still resembles an ip address
$this->assertStringMatchesFormat( '%x.%x.%x.%x', $new_ip, 'The address does not match a valid ip address format.' );
// assert that the string ends with .0
$this->assertStringEndsWith( '.0', $new_ip, 'The address does not end with a zero.' );
}
}

0 comments on commit e398d22

Please sign in to comment.