Merge pull request #25 from cmanon/feature/wp_mock-tests

Feature/wp mock tests
This commit is contained in:
Justin Foell
2019-03-01 12:33:09 -06:00
committed by GitHub
12 changed files with 284 additions and 55 deletions
+3 -1
View File
@@ -5,6 +5,8 @@
"php": ">=5.2.4"
},
"require-dev": {
"wp-coding-standards/wpcs": "~0.14"
"wp-coding-standards/wpcs": "^2",
"phpunit/phpunit": "^8",
"10up/wp_mock": "^0.4"
}
}
+19 -5
View File
@@ -29,10 +29,20 @@ abstract class WPStrava_SOM {
abstract public function pace( $mps );
abstract public function get_pace_label();
/**
* Create a time string of hours:minutes:seconds from just seconds.
*
* @return string Time formatted as 'H:i:s'.
*/
public function time( $seconds ) {
return date( 'H:i:s', mktime( 0, 0, $seconds ) );
}
/**
* Label for hours.
*
* @return string 'hours'
*/
public function get_time_label() {
return __( 'hours', 'wp-strava' );
}
@@ -49,14 +59,18 @@ abstract class WPStrava_SOM {
/**
* Change meters per second to Minutes Per 100 Meters. Same for English/metric.
*
* @param float $mps Meters per second.
* @return float Minutes Per 100 Meters.
* @param float|string $mps Meters per second.
* @return string Minutes Per 100 Meters.
*/
public function swimpace( $mps ) {
$kmh = $mps * 3.6;
$min100m = 60 / $kmh / 10;
$kmh = $mps * 3.6;
$s = 3600 / $kmh / 10;
$ss = $s / 60;
$ms = floor( $ss ) * 60;
$sec = sprintf( '%02d', round( $s - $ms ) );
$min = floor( $ss );
return number_format( $min100m, 2 );
return "{$min}:{$sec}";
}
}
+12 -12
View File
@@ -10,8 +10,8 @@ class WPStrava_SOMEnglish extends WPStrava_SOM {
/**
* Change meters to miles.
*
* @param float $m Distance in meters.
* @return float Distance in miles.
* @param float|string $m Distance in meters.
* @return string Distance in miles.
*/
public function distance( $m ) {
return number_format( $m / 1609.344, 2 );
@@ -20,11 +20,11 @@ class WPStrava_SOMEnglish extends WPStrava_SOM {
/**
* Change miles to meters.
*
* @param float $dist Distance in miles.
* @return float Distance in meters.
* @param float|string $dist Distance in miles.
* @return string Distance in meters.
*/
public function distance_inverse( $dist ) {
return $dist * 1609.344;
return number_format( $dist * 1609.344, 2 );
}
/**
@@ -39,8 +39,8 @@ class WPStrava_SOMEnglish extends WPStrava_SOM {
/**
* Change meters per second to miles per hour.
*
* @param float $mps Meters per second.
* @return float Miles per hour.
* @param float|string $mps Meters per second.
* @return string Miles per hour.
*/
public function speed( $mps ) {
return number_format( $mps * 2.2369, 2 );
@@ -58,8 +58,8 @@ class WPStrava_SOMEnglish extends WPStrava_SOM {
/**
* Change meters per second to minutes per mile.
*
* @param float $mps Meters per second.
* @return float Minutes Per Mile.
* @param float|string $mps Meters per second.
* @return string Minutes Per Mile.
*/
public function pace( $mps ) {
@@ -71,7 +71,7 @@ class WPStrava_SOMEnglish extends WPStrava_SOM {
$s = 3600 / $mph;
$ss = $s / 60;
$ms = floor( $ss ) * 60;
$sec = round( $s - $ms );
$sec = sprintf( '%02d', round( $s - $ms ) );
$min = floor( $ss );
return "{$min}:{$sec}";
@@ -89,8 +89,8 @@ class WPStrava_SOMEnglish extends WPStrava_SOM {
/**
* Change meters to feet.
*
* @param float $m Elevation in meters.
* @return float Elevation in feet.
* @param float|string $m Elevation in meters.
* @return string Elevation in feet.
*/
public function elevation( $m ) {
return number_format( $m / 0.3048, 2 );
+11 -11
View File
@@ -10,8 +10,8 @@ class WPStrava_SOMMetric extends WPStrava_SOM {
/**
* Change meters to kilometers.
*
* @param float $m Distance in meters.
* @return float Distance in kilometers.
* @param float|string $m Distance in meters.
* @return string Distance in kilometers.
*/
public function distance( $m ) {
return number_format( $m / 1000, 2 );
@@ -20,11 +20,11 @@ class WPStrava_SOMMetric extends WPStrava_SOM {
/**
* Change kilometers to meters.
*
* @param float $dist Distance in kilometers.
* @return float Distance in meters.
* @param float|string $dist Distance in kilometers.
* @return string Distance in meters.
*/
public function distance_inverse( $dist ) {
return $dist * 1000;
return number_format( $dist * 1000, 2 );
}
/**
@@ -39,8 +39,8 @@ class WPStrava_SOMMetric extends WPStrava_SOM {
/**
* Change meters per second to kilometers per hour.
*
* @param float $mps Meters per second.
* @return float Kilometers per hour.
* @param float|string $mps Meters per second.
* @return string Kilometers per hour.
*/
public function speed( $mps ) {
return number_format( $mps * 3.6, 2 );
@@ -58,8 +58,8 @@ class WPStrava_SOMMetric extends WPStrava_SOM {
/**
* Change meters per second to minutes per kilometer.
*
* @param float $mps Meters per second.
* @return float Kilometers per hour.
* @param float|string $mps Meters per second.
* @return string Minutes per kilometer.
*/
public function pace( $mps ) {
@@ -72,7 +72,7 @@ class WPStrava_SOMMetric extends WPStrava_SOM {
$s = 3600 / $kmh;
$ss = $s / 60;
$ms = floor( $ss ) * 60;
$sec = round( $s - $ms );
$sec = sprintf( '%02d', round( $s - $ms ) );
$min = floor( $ss );
return "{$min}:{$sec}";
@@ -90,7 +90,7 @@ class WPStrava_SOMMetric extends WPStrava_SOM {
/**
* Change meters to meters };^)
*
* @param $m Elevation in meters.
* @param float|string $m Elevation in meters.
* @return string Elevation in meters.
*/
public function elevation( $m ) {
+24
View File
@@ -0,0 +1,24 @@
<?php
/**
* Autoloads files with classes when needed.
*
* @since 1.6.0
* @param string $class_name Name of the class being requested.
* @return void
*/
function wpstrava_autoload_classes( $class_name ) {
$parts = explode( '_', $class_name );
// If our class doesn't have our namespace, don't load it.
if ( empty( $parts[0] ) || 'WPStrava' !== $parts[0] ) {
return;
}
// @TODO Add directory searching if they get created.
$file = WPSTRAVA_PLUGIN_DIR . '/lib/' . implode( '/', $parts ) . '.php';
if ( file_exists( $file ) ) {
include_once $file;
}
}
spl_autoload_register( 'wpstrava_autoload_classes' );
-1
View File
@@ -11,7 +11,6 @@
<file>./lib</file>
<file>./wp-strava.php</file>
<exclude-pattern>*/tests/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
<config name="installed_paths" value="../../../vendor/wp-coding-standards/wpcs/" />
+22
View File
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.5/phpunit.xsd"
bootstrap="./tests/bootstrap.php"
colors="true"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">lib</directory>
</whitelist>
</filter>
</phpunit>
+17
View File
@@ -0,0 +1,17 @@
# Unit Tests
While the main WP-Strava plugin code itself is PHP 5.2 compatible, you will need at least PHP 7.2 to run unit tests.
## Installation
In the main plugin directory run the following commands from the terminal:
`composer install`
## Running Tests
In the main plugin directory run the following commands from the terminal:
`vendor/bin/phpunit`
+83
View File
@@ -0,0 +1,83 @@
<?php
use \WP_Mock\Tools\TestCase;
class WPStrava_SOMEnglishTest extends TestCase {
private $som;
public function setUp() : void {
$this->som = new WPStrava_SOMEnglish();
}
public function test_object() {
$this->assertInstanceOf( 'WPStrava_SOMEnglish', $this->som );
}
/**
* Test that 10,000 meters is 6.21 miles using both string and float inputs.
*
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 1.7.1
*/
public function test_distance() {
$this->assertEquals( '6.21', $this->som->distance( '10000' ) );
$this->assertEquals( '6.21', $this->som->distance( 10000 ) );
}
/**
* Test that 6.213712 miles is 10,000.00 meters using both string and float inputs.
*
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 1.7.1
*/
public function test_distance_inverse() {
$this->assertEquals( '10,000.00', $this->som->distance_inverse( '6.213712' ) );
$this->assertEquals( '10,000.00', $this->som->distance_inverse( 6.213712 ) );
}
/**
* Test that 6.705 meters per second is 15.00 mph using both string and float inputs.
*
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 1.7.1
*/
public function test_speed() {
$this->assertEquals( '15.00', $this->som->speed( '6.705' ) );
$this->assertEquals( '15.00', $this->som->speed( 6.705 ) );
}
/**
* Test that 2.68224 meters per second is a 10:00 minute/mile pace using both string and float inputs.
*
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 1.7.1
*/
public function test_pace() {
$this->assertEquals( '10:00', $this->som->pace( '2.68224' ) );
$this->assertEquals( '10:00', $this->som->pace( 2.68224 ) );
}
/**
* Test that 60.96 meters is 200.00 feet using both string and float inputs.
*
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 1.7.1
*/
public function test_elevation() {
$this->assertEquals( '200.00', $this->som->elevation( '60.96' ) );
$this->assertEquals( '200.00', $this->som->elevation( 60.96 ) );
}
/**
* Test that 4805 seconds is 01:20:05 time (H:i:s) using both string and float inputs.
*
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 1.7.1
*/
public function test_time() {
$this->assertEquals( '01:20:05', $this->som->time( '4805' ) );
$this->assertEquals( '01:20:05', $this->som->time( 4805 ) );
}
}
+82
View File
@@ -0,0 +1,82 @@
<?php
use \WP_Mock\Tools\TestCase;
class WPStrava_SOMMetricTest extends TestCase {
private $som;
public function setUp() : void {
$this->som = new WPStrava_SOMMetric();
}
public function test_object() {
$this->assertInstanceOf( 'WPStrava_SOMMetric', $this->som );
}
/**
* Test that 10,000 meters is 10.00 kilometers using both string and float inputs.
*
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 1.7.1
*/
public function test_distance() {
$this->assertEquals( '10.00', $this->som->distance( '10000' ) );
$this->assertEquals( '10.00', $this->som->distance( 10000 ) );
}
/**
* Test that 42.195 km is 42,195.00 meters using both string and float inputs.
*
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 1.7.1
*/
public function test_distance_inverse() {
$this->assertEquals( '42,195.00', $this->som->distance_inverse( '42.195' ) );
$this->assertEquals( '42,195.00', $this->som->distance_inverse( 42.195 ) );
}
/**
* Test that 4.47 meters per second is 16.09 kmh using both string and float inputs.
*
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 1.7.1
*/
public function test_speed() {
$this->assertEquals( '16.09', $this->som->speed( '4.47' ) );
$this->assertEquals( '16.09', $this->som->speed( 4.47 ) );
}
/**
* Test that 2.2352 meters per second is a 7:27 minute/kilometer pace using both string and float inputs.
*
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 1.7.1
*/
public function test_pace() {
$this->assertEquals( '7:27', $this->som->pace( '2.2352' ) );
$this->assertEquals( '7:27', $this->som->pace( 2.2352 ) );
}
/**
* Test that 70 meters is 70.00 meters using both string and float inputs.
*
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 1.7.1
*/
public function test_elevation() {
$this->assertEquals( '70.00', $this->som->elevation( '70' ) );
$this->assertEquals( '70.00', $this->som->elevation( 70 ) );
}
/**
* Test that 1.66 meters per second is a 1:00 minute/100m pace using both string and float inputs.
*
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 1.7.1
*/
public function test_swimpace() {
$this->assertEquals( '1:00', $this->som->swimpace( '1.66' ) );
$this->assertEquals( '1:00', $this->som->swimpace( 1.66 ) );
}
}
+7
View File
@@ -0,0 +1,7 @@
<?php
if ( ! defined( 'WPSTRAVA_PLUGIN_DIR' ) ) define( 'WPSTRAVA_PLUGIN_DIR', dirname( __FILE__ ) . '/../' );
require_once dirname( __FILE__ ) . '/../lib/autoload.php';
require_once dirname( __FILE__ ) . '/../vendor/autoload.php';
WP_Mock::bootstrap();
+4 -25
View File
@@ -3,7 +3,7 @@
* Plugin Name: WP Strava
* Plugin URI: https://wordpress.org/plugins/wp-strava/
* Description: Show your strava.com activity on your WordPress site. Some Icons are Copyright © Yusuke Kamiyamane. All rights reserved. Licensed under a Creative Commons Attribution 3.0 license.
* Version: 1.7.0
* Version: 1.7.1-b1
* Author: Carlos Santa Cruz, Justin Foell, Lance Willett, Daniel Lintott, Sebastian Erb
* License: GPL2
* Text Domain: wp-strava
@@ -35,33 +35,12 @@ if ( ! defined( 'WPSTRAVA_DEBUG' ) ) {
define( 'WPSTRAVA_DEBUG', false );
}
// Load the multilingual support.
require_once WPSTRAVA_PLUGIN_DIR . 'lib/autoload.php';
// Load the plugin and multilingual support.
function wpstrava_load_plugin_textdomain() {
load_plugin_textdomain( 'wp-strava', false, WPSTRAVA_PLUGIN_DIR . 'lang/' );
}
add_action( 'plugins_loaded', 'wpstrava_load_plugin_textdomain' );
/**
* Autoloads files with classes when needed.
*
* @since 1.6.0
* @param string $class_name Name of the class being requested.
* @return void
*/
function wpstrava_autoload_classes( $class_name ) {
$parts = explode( '_', $class_name );
// If our class doesn't have our namespace, don't load it.
if ( empty( $parts[0] ) || 'WPStrava' !== $parts[0] ) {
return;
}
// @TODO Add directory searching if they get created.
$file = dirname( __FILE__ ) . '/lib/' . implode( '/', $parts ) . '.php';
if ( file_exists( $file ) ) {
include_once $file;
}
}
spl_autoload_register( 'wpstrava_autoload_classes' );
$wpstrava = WPStrava::get_instance();