From c5fdb24244b4b0943a2601e4a42401f5d3fcf091 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Fri, 1 Mar 2019 11:41:41 -0600 Subject: [PATCH] Added System of Measure (English) unit tests; Fixed a bug in pace formatting --- composer.json | 6 +-- lib/WPStrava/SOM.php | 14 ++++++- lib/WPStrava/SOMEnglish.php | 24 +++++------ lib/WPStrava/SOMMetric.php | 22 +++++------ tests/README.md | 3 ++ tests/WPStrava/SOMEnglishTest.php | 66 +++++++++++++++++++++++++++++-- 6 files changed, 104 insertions(+), 31 deletions(-) diff --git a/composer.json b/composer.json index 4b78448..d21f81c 100644 --- a/composer.json +++ b/composer.json @@ -5,8 +5,8 @@ "php": ">=5.2.4" }, "require-dev": { - "wp-coding-standards/wpcs": "1.*", - "phpunit/phpunit": "^6", - "10up/wp_mock": "0.3.0" + "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..65d9715 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,8 +59,8 @@ 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 ) { 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/tests/README.md b/tests/README.md index e5d6966..e0a0875 100644 --- a/tests/README.md +++ b/tests/README.md @@ -1,5 +1,8 @@ # 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: diff --git a/tests/WPStrava/SOMEnglishTest.php b/tests/WPStrava/SOMEnglishTest.php index 79b5ae5..7cc60e9 100644 --- a/tests/WPStrava/SOMEnglishTest.php +++ b/tests/WPStrava/SOMEnglishTest.php @@ -4,8 +4,68 @@ use \WP_Mock\Tools\TestCase; class WPStrava_SOMEnglishTest extends TestCase { - public function test_true() { - $som = new WPStrava_SOMEnglish(); - $this->assertInstanceOf( 'WPStrava_SOMEnglish', $som ); + 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 + * @since NEXT + */ + 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 NEXT + */ + 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 NEXT + */ + 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 NEXT + */ + 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 NEXT + */ + public function test_elevation() { + $this->assertEquals( '200.00', $this->som->elevation( '60.96' ) ); + $this->assertEquals( '200.00', $this->som->elevation( 60.96 ) ); } }