|
From: <gem...@li...> - 2012-01-23 15:27:47
|
Revision: 416
http://gemstracker.svn.sourceforge.net/gemstracker/?rev=416&view=rev
Author: mennodekker
Date: 2012-01-23 15:27:41 +0000 (Mon, 23 Jan 2012)
Log Message:
-----------
Fix for event calculation helper 'sumInt' and a testcase (all event calculation helper methods should be made public to be testable)
Modified Paths:
--------------
trunk/library/classes/Gems/Event/EventCalculations.php
trunk/test/classes/Gems/Event/Gems_Event_EventCalculationsTest.php
Modified: trunk/library/classes/Gems/Event/EventCalculations.php
===================================================================
--- trunk/library/classes/Gems/Event/EventCalculations.php 2012-01-23 11:04:04 UTC (rev 415)
+++ trunk/library/classes/Gems/Event/EventCalculations.php 2012-01-23 15:27:41 UTC (rev 416)
@@ -106,7 +106,7 @@
foreach ($fieldNames as $name) {
if (isset($tokenAnswers[$name]) && is_int(intval($tokenAnswers[$name]))) {
$count++;
- $sum += $tokenAnswers[$name];
+ $sum += intval($tokenAnswers[$name]);
}
}
return $count ? $sum / $count : null;
@@ -206,7 +206,7 @@
* @param mixed $fieldNames An array of those names that should be used or a string that should occur in all names that have to be selected.
* @return int
*/
- protected function sumInt(array $tokenAnswers, $fieldNames)
+ public function sumInt(array $tokenAnswers, $fieldNames)
{
if (is_string($fieldNames)) {
$fieldNames = $this->_arrayFindName($tokenAnswers, $fieldNames);
@@ -214,7 +214,7 @@
$sum = 0;
foreach ($fieldNames as $name) {
- if (isset($tokenAnswers[$name]) && is_int(intval($tokenAnswers[$name]))) {
+ if (isset($tokenAnswers[$name]) && intval($tokenAnswers[$name])== $tokenAnswers[$name]) {
$sum += $tokenAnswers[$name];
}
}
Modified: trunk/test/classes/Gems/Event/Gems_Event_EventCalculationsTest.php
===================================================================
--- trunk/test/classes/Gems/Event/Gems_Event_EventCalculationsTest.php 2012-01-23 11:04:04 UTC (rev 415)
+++ trunk/test/classes/Gems/Event/Gems_Event_EventCalculationsTest.php 2012-01-23 15:27:41 UTC (rev 416)
@@ -30,6 +30,27 @@
$this->assertEquals(0, $this->object->reverseCode(5, 0, 5));
}
+ public function testSumInt() {
+ $tokenAnswers = array(
+ 'fld_1'=>1,
+ 'fld_2'=>'2',
+ 'fld_3'=>1.5, //not integer
+ 'fld_4'=>'a', //not integer
+ 'fld_5'=>1.4, //not integer
+ 'fld_6'=>1.6 //not integer
+ );
+
+ //Initial check
+ $this->assertEquals(3, $this->object->sumInt($tokenAnswers, array('fld_1', 'fld_2')));
+
+ //Check if non-int will be left out
+ $this->assertEquals(3, $this->object->sumInt($tokenAnswers, array('fld_1', 'fld_2', 'fld_3')));
+ $this->assertEquals(3, $this->object->sumInt($tokenAnswers, array('fld_1', 'fld_2', 'fld_4')));
+ //Make sure there are no rounding issues
+ $this->assertEquals(3, $this->object->sumInt($tokenAnswers, array('fld_1', 'fld_2', 'fld_5')));
+ $this->assertEquals(3, $this->object->sumInt($tokenAnswers, array('fld_1', 'fld_2', 'fld_6')));
+ }
+
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|