Skip to content
This repository was archived by the owner on May 8, 2025. It is now read-only.

Commit 0bfebc7

Browse files
committed
testSetUnits_validate_options
1 parent 481fbc6 commit 0bfebc7

File tree

2 files changed

+80
-8
lines changed

2 files changed

+80
-8
lines changed

src/php-FIT-File-Analysis.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,22 +1065,32 @@ public function sport() {
10651065
* Transform the values read from the FIT file into the units requested by the user.
10661066
*/
10671067
private function set_units($options) {
1068-
$units = '';
1069-
1070-
if(isset($options['units'])) {
1068+
if(!empty($options['units'])) {
10711069
// Handle $options['units'] not being passed as array and/or not in lowercase.
10721070
$units = strtolower((is_array($options['units'])) ? $options['units'][0] : $options['units']);
10731071
}
1072+
else {
1073+
$units = 'metric';
1074+
}
10741075

10751076
// Handle $options['pace'] being pass as array and/or boolean vs string and/or lowercase.
10761077
$bPace = false;
10771078
if(isset($options['pace'])) {
10781079
$pace = is_array($options['pace']) ? $options['pace'][0] : $options['pace'];
1079-
if(is_bool($options['pace'])) {
1080+
if(is_bool($pace)) {
10801081
$bPace = $pace;
10811082
}
1082-
else if(is_string($options['pace'])) {
1083-
$bPace = (strtolower($pace) === 'true') ? true : false;
1083+
else if(is_string($pace)) {
1084+
$pace = strtolower($pace);
1085+
if($pace === 'true' || $pace === 'false') {
1086+
$bPace = $pace;
1087+
}
1088+
else {
1089+
throw new Exception('phpFITFileAnalysis->set_units(): pace option not valid!');
1090+
}
1091+
}
1092+
else {
1093+
throw new Exception('phpFITFileAnalysis->set_units(): pace option not valid!');
10841094
}
10851095
}
10861096

@@ -1160,12 +1170,12 @@ private function set_units($options) {
11601170
case 'raw':
11611171
// Do nothing - leave values as read from file.
11621172
break;
1163-
default: // Assume 'metric'.
1173+
case 'metric':
11641174
if(isset($this->data_mesgs['record']['speed'])) { // convert meters per second to kilometers per hour
11651175
if(is_array($this->data_mesgs['record']['speed'])) {
11661176
foreach($this->data_mesgs['record']['speed'] as &$value) {
11671177
if($bPace) {
1168-
$value = round(60 / 3.6 / $value, 3);
1178+
$value = ($value != 0) ? round(60 / 3.6 / $value, 3) : 0;
11691179
}
11701180
else {
11711181
$value = round($value * 3.6, 3);
@@ -1212,6 +1222,9 @@ private function set_units($options) {
12121222
}
12131223
}
12141224
break;
1225+
default:
1226+
throw new Exception('phpFITFileAnalysis->set_units(): units option not valid!');
1227+
break;
12151228
}
12161229
}
12171230

tests/pFFA-Set_Units-Test.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
error_reporting(E_ALL);
3+
require __DIR__ . '/../src/php-FIT-File-Analysis.php';
4+
5+
class FitTest extends PHPUnit_Framework_TestCase
6+
{
7+
private $base_dir;
8+
private $filename = 'road-cycling.fit';
9+
10+
public function setUp()
11+
{
12+
$this->base_dir = __DIR__ . '/../demo/fit_files/';
13+
}
14+
15+
public function testSetUnits_validate_options_pass()
16+
{
17+
$valid_options = ['raw', 'statute', 'metric'];
18+
foreach($valid_options as $valid_option) {
19+
$pFFA = new phpFITFileAnalysis($this->base_dir . $this->filename, ['units' => $valid_option]);
20+
21+
if($valid_option === 'raw') {
22+
$this->assertEquals(1.286, reset($pFFA->data_mesgs['record']['speed']));
23+
}
24+
if($valid_option === 'statute') {
25+
$this->assertEquals(2.877, reset($pFFA->data_mesgs['record']['speed']));
26+
}
27+
if($valid_option === 'metric') {
28+
$this->assertEquals(4.63, reset($pFFA->data_mesgs['record']['speed']));
29+
}
30+
}
31+
}
32+
33+
/**
34+
* @expectedException Exception
35+
*/
36+
public function testSetUnits_validate_options_fail()
37+
{
38+
$pFFA = new phpFITFileAnalysis($this->base_dir . $this->filename, ['units' => 'INVALID']);
39+
}
40+
41+
public function testSetUnits_validate_pace_option_pass()
42+
{
43+
$valid_options = [true, false];
44+
foreach($valid_options as $valid_option) {
45+
$pFFA = new phpFITFileAnalysis($this->base_dir . $this->filename, ['units' => 'raw', 'pace' => $valid_option]);
46+
47+
$this->assertEquals(1.286, reset($pFFA->data_mesgs['record']['speed']));
48+
}
49+
}
50+
51+
/**
52+
* @expectedException Exception
53+
*/
54+
public function testSetUnits_validate_pace_option_fail()
55+
{
56+
$pFFA = new phpFITFileAnalysis($this->base_dir . $this->filename, ['pace' => 'INVALID']);
57+
}
58+
}
59+
?>

0 commit comments

Comments
 (0)