diff --git a/composer.json b/composer.json
index 30d829d..d21f81c 100644
--- a/composer.json
+++ b/composer.json
@@ -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"
}
}
diff --git a/lib/WPStrava/SOM.php b/lib/WPStrava/SOM.php
index 7730606..485bfb8 100644
--- a/lib/WPStrava/SOM.php
+++ b/lib/WPStrava/SOM.php
@@ -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}";
}
}
diff --git a/lib/WPStrava/SOMEnglish.php b/lib/WPStrava/SOMEnglish.php
index abf6ca1..477d9a5 100644
--- a/lib/WPStrava/SOMEnglish.php
+++ b/lib/WPStrava/SOMEnglish.php
@@ -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 );
diff --git a/lib/WPStrava/SOMMetric.php b/lib/WPStrava/SOMMetric.php
index 7c969df..5a239c1 100644
--- a/lib/WPStrava/SOMMetric.php
+++ b/lib/WPStrava/SOMMetric.php
@@ -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 ) {
diff --git a/lib/autoload.php b/lib/autoload.php
new file mode 100644
index 0000000..c1a9a6c
--- /dev/null
+++ b/lib/autoload.php
@@ -0,0 +1,24 @@
+./lib
./wp-strava.php
- */tests/*
*/vendor/*
diff --git a/phpunit.xml b/phpunit.xml
new file mode 100644
index 0000000..45eedc3
--- /dev/null
+++ b/phpunit.xml
@@ -0,0 +1,22 @@
+
+
+
+
+ tests
+
+
+
+
+
+ lib
+
+
+
diff --git a/tests/README.md b/tests/README.md
new file mode 100644
index 0000000..e0a0875
--- /dev/null
+++ b/tests/README.md
@@ -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`
diff --git a/tests/WPStrava/SOMEnglishTest.php b/tests/WPStrava/SOMEnglishTest.php
new file mode 100644
index 0000000..0ac92e8
--- /dev/null
+++ b/tests/WPStrava/SOMEnglishTest.php
@@ -0,0 +1,83 @@
+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
+ * @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
+ * @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
+ * @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
+ * @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
+ * @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
+ * @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 ) );
+
+ }
+}
diff --git a/tests/WPStrava/SOMMetricTest.php b/tests/WPStrava/SOMMetricTest.php
new file mode 100644
index 0000000..24a99f9
--- /dev/null
+++ b/tests/WPStrava/SOMMetricTest.php
@@ -0,0 +1,82 @@
+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
+ * @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
+ * @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
+ * @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
+ * @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
+ * @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
+ * @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 ) );
+ }
+}
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
new file mode 100644
index 0000000..a5fbe64
--- /dev/null
+++ b/tests/bootstrap.php
@@ -0,0 +1,7 @@
+