2121require_once BASE_PATH .'/modules/api/tests/controllers/CallMethodsTestCase.php ' ;
2222
2323/** API test for tracker module. */
24- class Tracker_ApiControllerTest extends Api_CallMethodsTestCase
24+ class Tracker_ApiComponentTest extends Api_CallMethodsTestCase
2525{
2626 public $ moduleName = 'tracker ' ;
2727
@@ -50,8 +50,10 @@ public function testUploadScalarWithSubmission()
5050
5151 $ outputs = array ();
5252 $ outputs ['metric_0 ' ] = $ this ->_submitScalar ($ token , $ uuid , 'metric_0 ' , '18 ' );
53- $ outputs ['metric_1 ' ] = $ this ->_submitScalar ($ token , $ uuid , 'metric_1 ' , '19 ' , 'meters ' );
54- $ outputs ['metric_2 ' ] = $ this ->_submitScalar ($ token , $ uuid , 'metric_2 ' , '20 ' , 'mm ' );
53+ $ metric1Params = array ('num_param ' => 19.0 , 'text_param ' => 'metric1 text ' , 'null_param ' => null );
54+ $ outputs ['metric_1 ' ] = $ this ->_submitScalar ($ token , $ uuid , 'metric_1 ' , '19 ' , 'meters ' , $ metric1Params );
55+ $ metric2Params = array ('num_param ' => 20.0 , 'text_param ' => 'metric2 text ' , 'null_param ' => null );
56+ $ outputs ['metric_2 ' ] = $ this ->_submitScalar ($ token , $ uuid , 'metric_2 ' , '20 ' , 'mm ' , $ metric2Params );
5557
5658 /** @var Tracker_SubmissionModel $submissionModel */
5759 $ submissionModel = MidasLoader::loadModel ('Submission ' , 'tracker ' );
@@ -60,12 +62,67 @@ public function testUploadScalarWithSubmission()
6062 $ submissionDao = $ submissionModel ->getSubmission ($ uuid );
6163 $ scalarDaos = $ submissionModel ->getScalars ($ submissionDao );
6264
65+ // Maps the scalars for each metric.
66+ $ metricToScalar = array ();
67+
6368 /** @var Tracker_ScalarDao $scalarDao */
6469 foreach ($ scalarDaos as $ scalarDao ) {
6570 $ curOutput = $ outputs [$ scalarDao ->getTrend ()->getMetricName ()];
71+ $ metricToScalar [$ scalarDao ->getTrend ()->getMetricName ()] = $ scalarDao ;
6672 $ this ->assertEquals ($ curOutput ->value , $ scalarDao ->getValue ());
6773 $ this ->assertEquals ($ submissionDao ->getKey (), $ scalarDao ->getSubmissionId ());
6874 }
75+
76+ // Params should be a zero element array here.
77+ $ this ->assertTrue (!($ metricToScalar ['metric_0 ' ]->getParams ()));
78+
79+ $ metric_1_params = $ metricToScalar ['metric_1 ' ]->getParams ();
80+ $ metric_1_paramChecks = array (
81+ 'num_param ' => array ('found ' => false , 'type ' => 'numeric ' , 'val ' => 19.0 ),
82+ 'text_param ' => array ('found ' => false , 'type ' => 'text ' , 'val ' => 'metric1 text ' ),
83+ 'null_param ' => array ('found ' => false , 'type ' => 'text ' , 'val ' => '' ),
84+ );
85+
86+ // Test that the params are saved as the correct type and value.
87+ foreach ($ metric_1_params as $ param ) {
88+ $ checks = $ metric_1_paramChecks [$ param ->getParamName ()];
89+ $ this ->assertEquals ($ checks ['type ' ], $ param ->getParamType ());
90+ if ($ checks ['type ' ] === 'numeric ' ) {
91+ $ this ->assertEquals ($ checks ['val ' ], $ param ->getNumericValue ());
92+ } else {
93+ $ this ->assertEquals ($ checks ['val ' ], $ param ->getTextValue ());
94+ }
95+ $ metric_1_paramChecks [$ param ->getParamName ()]['found ' ] = true ;
96+ }
97+
98+ // Test that all params are tested.
99+ foreach ($ metric_1_paramChecks as $ checks ) {
100+ $ this ->assertTrue ($ checks ['found ' ]);
101+ }
102+
103+ $ metric_2_params = $ metricToScalar ['metric_2 ' ]->getParams ();
104+ $ metric_2_paramChecks = array (
105+ 'num_param ' => array ('found ' => false , 'type ' => 'numeric ' , 'val ' => 20.0 ),
106+ 'text_param ' => array ('found ' => false , 'type ' => 'text ' , 'val ' => 'metric2 text ' ),
107+ 'null_param ' => array ('found ' => false , 'type ' => 'text ' , 'val ' => '' ),
108+ );
109+
110+ // Test that the params are saved as the correct type and value.
111+ foreach ($ metric_2_params as $ param ) {
112+ $ checks = $ metric_2_paramChecks [$ param ->getParamName ()];
113+ $ this ->assertEquals ($ checks ['type ' ], $ param ->getParamType ());
114+ if ($ checks ['type ' ] === 'numeric ' ) {
115+ $ this ->assertEquals ($ checks ['val ' ], $ param ->getNumericValue ());
116+ } else {
117+ $ this ->assertEquals ($ checks ['val ' ], $ param ->getTextValue ());
118+ }
119+ $ metric_2_paramChecks [$ param ->getParamName ()]['found ' ] = true ;
120+ }
121+
122+ // Test that all params are tested.
123+ foreach ($ metric_2_paramChecks as $ checks ) {
124+ $ this ->assertTrue ($ checks ['found ' ]);
125+ }
69126 }
70127
71128 /**
@@ -76,9 +133,10 @@ public function testUploadScalarWithSubmission()
76133 * @param string $metric the metric name of the trend
77134 * @param float $value the scalar value
78135 * @param false|string $unit (Optional) the unit of the trend, defaults to false
136+ * @param false|array $scalarParams (Optional) the params of the scalar, defaults to false
79137 * @return mixed response object from the API
80138 */
81- private function _submitScalar ($ token , $ uuid , $ metric , $ value , $ unit = false )
139+ private function _submitScalar ($ token , $ uuid , $ metric , $ value , $ unit = false , $ scalarParams = false )
82140 {
83141 $ this ->resetAll ();
84142 $ this ->params ['method ' ] = 'midas.tracker.scalar.add ' ;
@@ -93,7 +151,9 @@ private function _submitScalar($token, $uuid, $metric, $value, $unit = false)
93151 if ($ unit !== false ) {
94152 $ this ->params ['unit ' ] = $ unit ;
95153 }
96-
154+ if ($ scalarParams !== false ) {
155+ $ this ->params ['params ' ] = json_encode ($ scalarParams );
156+ }
97157 $ res = $ this ->_callJsonApi ();
98158
99159 return $ res ->data ;
0 commit comments