'changeformat' => 1, 'context' => $context, 'noclean' => 1, 'trusttext' => 0);
}
+ /**
+ * Given the percent and the total, returns the number
+ *
+ * @param float $percent from 0 to 100
+ * @param float $total the 100% value
+ * @return float
+ */
+ public static function percent_to_value($percent, $total) {
+ if ($percent < 0 or $percent > 100) {
+ throw new coding_exception('The percent can not be less than 0 or higher than 100');
+ }
+
+ return $total * $percent / 100;
+ }
+
////////////////////////////////////////////////////////////////////////////////
// Workshop API //
////////////////////////////////////////////////////////////////////////////////
$this->workshop = null;
}
+ public function test_percent_to_value() {
+ // fixture setup
+ $total = 185;
+ $percent = 56.6543;
+ // exercise SUT
+ $part = workshop::percent_to_value($percent, $total);
+ // verify
+ $this->assertEqual($part, $total * $percent / 100);
+ }
+
+ public function test_percent_to_value_negative() {
+ // fixture setup
+ $total = 185;
+ $percent = -7.098;
+ // set expectation
+ $this->expectException('coding_exception');
+ // exercise SUT
+ $part = workshop::percent_to_value($percent, $total);
+ }
+
+ public function test_percent_to_value_over_hundred() {
+ // fixture setup
+ $total = 185;
+ $percent = 121.08;
+ // set expectation
+ $this->expectException('coding_exception');
+ // exercise SUT
+ $part = workshop::percent_to_value($percent, $total);
+ }
+
}