From 438f849fad0dc56b4eb499f730d1be1bdf980463 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Fri, 21 Sep 2018 10:28:46 -0500 Subject: [PATCH 1/5] Scaffolded unit tests --- composer.json | 4 +++- lib/autoload.php | 24 ++++++++++++++++++++++++ phpunit.xml | 22 ++++++++++++++++++++++ tests/WPStrava/SOMEnglishTest.php | 11 +++++++++++ tests/bootstrap.php | 6 ++++++ wp-strava.php | 29 ++++------------------------- 6 files changed, 70 insertions(+), 26 deletions(-) create mode 100644 lib/autoload.php create mode 100644 phpunit.xml create mode 100644 tests/WPStrava/SOMEnglishTest.php create mode 100644 tests/bootstrap.php diff --git a/composer.json b/composer.json index 30d829d..4b78448 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": "1.*", + "phpunit/phpunit": "^6", + "10up/wp_mock": "0.3.0" } } 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 @@ + + + + + tests + + + + + + lib + + + diff --git a/tests/WPStrava/SOMEnglishTest.php b/tests/WPStrava/SOMEnglishTest.php new file mode 100644 index 0000000..8e4cae1 --- /dev/null +++ b/tests/WPStrava/SOMEnglishTest.php @@ -0,0 +1,11 @@ +assertInstanceOf( 'WPStrava_SOMEnglish', $som ); + } +} \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..d84c73a --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,6 @@ + Date: Fri, 9 Nov 2018 13:45:37 -0600 Subject: [PATCH 2/5] Added unit test README --- phpunit.xml | 2 +- tests/README.md | 14 ++++++++++++++ tests/WPStrava/SOMEnglishTest.php | 2 +- tests/bootstrap.php | 3 ++- 4 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 tests/README.md diff --git a/phpunit.xml b/phpunit.xml index 26d5ffa..45eedc3 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,6 +1,6 @@ assertInstanceOf( 'WPStrava_SOMEnglish', $som ); } -} \ No newline at end of file +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d84c73a..a5fbe64 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -2,5 +2,6 @@ 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(); \ No newline at end of file +WP_Mock::bootstrap(); From c5fdb24244b4b0943a2601e4a42401f5d3fcf091 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Fri, 1 Mar 2019 11:41:41 -0600 Subject: [PATCH 3/5] 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 ) ); } } From a4e041805d19fb24b58e430759c2110779f18ec6 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Fri, 1 Mar 2019 12:23:34 -0600 Subject: [PATCH 4/5] Added System of Measure (Metric) unit tests; Reworked swimpace() function to show min:sec --- lib/WPStrava/SOM.php | 10 ++-- tests/WPStrava/SOMEnglishTest.php | 22 +++++++-- tests/WPStrava/SOMMetricTest.php | 82 +++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 8 deletions(-) create mode 100644 tests/WPStrava/SOMMetricTest.php diff --git a/lib/WPStrava/SOM.php b/lib/WPStrava/SOM.php index 65d9715..485bfb8 100644 --- a/lib/WPStrava/SOM.php +++ b/lib/WPStrava/SOM.php @@ -64,9 +64,13 @@ abstract class WPStrava_SOM { */ 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/tests/WPStrava/SOMEnglishTest.php b/tests/WPStrava/SOMEnglishTest.php index 7cc60e9..ad5a8c5 100644 --- a/tests/WPStrava/SOMEnglishTest.php +++ b/tests/WPStrava/SOMEnglishTest.php @@ -18,7 +18,7 @@ class WPStrava_SOMEnglishTest extends TestCase { * Test that 10,000 meters is 6.21 miles using both string and float inputs. * * @author Justin Foell - * @since NEXT + * @since 1.7.1 */ public function test_distance() { $this->assertEquals( '6.21' , $this->som->distance( '10000' ) ); @@ -29,7 +29,7 @@ class WPStrava_SOMEnglishTest extends TestCase { * Test that 6.213712 miles is 10,000.00 meters using both string and float inputs. * * @author Justin Foell - * @since NEXT + * @since 1.7.1 */ public function test_distance_inverse() { $this->assertEquals( '10,000.00' , $this->som->distance_inverse( '6.213712' ) ); @@ -40,7 +40,7 @@ class WPStrava_SOMEnglishTest extends TestCase { * Test that 6.705 meters per second is 15.00 mph using both string and float inputs. * * @author Justin Foell - * @since NEXT + * @since 1.7.1 */ public function test_speed() { $this->assertEquals( '15.00', $this->som->speed( '6.705' ) ); @@ -51,7 +51,7 @@ class WPStrava_SOMEnglishTest extends TestCase { * 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 + * @since 1.7.1 */ public function test_pace() { $this->assertEquals( '10:00', $this->som->pace( '2.68224' ) ); @@ -62,10 +62,22 @@ class WPStrava_SOMEnglishTest extends TestCase { * Test that 60.96 meters is 200.00 feet using both string and float inputs. * * @author Justin Foell - * @since NEXT + * @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..d5ad8be --- /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 ) ); + } +} From 85e75c5bdecc3f908d93a3601cf65a72485bfd65 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Fri, 1 Mar 2019 12:28:31 -0600 Subject: [PATCH 5/5] (Re)add tests directory to PHPCS checks; Fix formatting in tests --- phpcs.xml | 1 - tests/WPStrava/SOMEnglishTest.php | 8 ++++---- tests/WPStrava/SOMMetricTest.php | 8 ++++---- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/phpcs.xml b/phpcs.xml index 64d0ec4..3fe964c 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -11,7 +11,6 @@ ./lib ./wp-strava.php - */tests/* */vendor/* diff --git a/tests/WPStrava/SOMEnglishTest.php b/tests/WPStrava/SOMEnglishTest.php index ad5a8c5..0ac92e8 100644 --- a/tests/WPStrava/SOMEnglishTest.php +++ b/tests/WPStrava/SOMEnglishTest.php @@ -21,8 +21,8 @@ class WPStrava_SOMEnglishTest extends TestCase { * @since 1.7.1 */ public function test_distance() { - $this->assertEquals( '6.21' , $this->som->distance( '10000' ) ); - $this->assertEquals( '6.21' , $this->som->distance( 10000 ) ); + $this->assertEquals( '6.21', $this->som->distance( '10000' ) ); + $this->assertEquals( '6.21', $this->som->distance( 10000 ) ); } /** @@ -32,8 +32,8 @@ class WPStrava_SOMEnglishTest extends TestCase { * @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 ) ); + $this->assertEquals( '10,000.00', $this->som->distance_inverse( '6.213712' ) ); + $this->assertEquals( '10,000.00', $this->som->distance_inverse( 6.213712 ) ); } /** diff --git a/tests/WPStrava/SOMMetricTest.php b/tests/WPStrava/SOMMetricTest.php index d5ad8be..24a99f9 100644 --- a/tests/WPStrava/SOMMetricTest.php +++ b/tests/WPStrava/SOMMetricTest.php @@ -21,8 +21,8 @@ class WPStrava_SOMMetricTest extends TestCase { * @since 1.7.1 */ public function test_distance() { - $this->assertEquals( '10.00' , $this->som->distance( '10000' ) ); - $this->assertEquals( '10.00' , $this->som->distance( 10000 ) ); + $this->assertEquals( '10.00', $this->som->distance( '10000' ) ); + $this->assertEquals( '10.00', $this->som->distance( 10000 ) ); } /** @@ -32,8 +32,8 @@ class WPStrava_SOMMetricTest extends TestCase { * @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 ) ); + $this->assertEquals( '42,195.00', $this->som->distance_inverse( '42.195' ) ); + $this->assertEquals( '42,195.00', $this->som->distance_inverse( 42.195 ) ); } /**