21
21
require_once BASE_PATH .'/modules/api/tests/controllers/CallMethodsTestCase.php ' ;
22
22
23
23
/** API test for tracker module. */
24
- class Tracker_ApiControllerTest extends Api_CallMethodsTestCase
24
+ class Tracker_ApiComponentTest extends Api_CallMethodsTestCase
25
25
{
26
26
public $ moduleName = 'tracker ' ;
27
27
@@ -50,8 +50,10 @@ public function testUploadScalarWithSubmission()
50
50
51
51
$ outputs = array ();
52
52
$ 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 );
55
57
56
58
/** @var Tracker_SubmissionModel $submissionModel */
57
59
$ submissionModel = MidasLoader::loadModel ('Submission ' , 'tracker ' );
@@ -60,12 +62,67 @@ public function testUploadScalarWithSubmission()
60
62
$ submissionDao = $ submissionModel ->getSubmission ($ uuid );
61
63
$ scalarDaos = $ submissionModel ->getScalars ($ submissionDao );
62
64
65
+ // Maps the scalars for each metric.
66
+ $ metricToScalar = array ();
67
+
63
68
/** @var Tracker_ScalarDao $scalarDao */
64
69
foreach ($ scalarDaos as $ scalarDao ) {
65
70
$ curOutput = $ outputs [$ scalarDao ->getTrend ()->getMetricName ()];
71
+ $ metricToScalar [$ scalarDao ->getTrend ()->getMetricName ()] = $ scalarDao ;
66
72
$ this ->assertEquals ($ curOutput ->value , $ scalarDao ->getValue ());
67
73
$ this ->assertEquals ($ submissionDao ->getKey (), $ scalarDao ->getSubmissionId ());
68
74
}
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
+ }
69
126
}
70
127
71
128
/**
@@ -76,9 +133,10 @@ public function testUploadScalarWithSubmission()
76
133
* @param string $metric the metric name of the trend
77
134
* @param float $value the scalar value
78
135
* @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
79
137
* @return mixed response object from the API
80
138
*/
81
- private function _submitScalar ($ token , $ uuid , $ metric , $ value , $ unit = false )
139
+ private function _submitScalar ($ token , $ uuid , $ metric , $ value , $ unit = false , $ scalarParams = false )
82
140
{
83
141
$ this ->resetAll ();
84
142
$ this ->params ['method ' ] = 'midas.tracker.scalar.add ' ;
@@ -93,7 +151,9 @@ private function _submitScalar($token, $uuid, $metric, $value, $unit = false)
93
151
if ($ unit !== false ) {
94
152
$ this ->params ['unit ' ] = $ unit ;
95
153
}
96
-
154
+ if ($ scalarParams !== false ) {
155
+ $ this ->params ['params ' ] = json_encode ($ scalarParams );
156
+ }
97
157
$ res = $ this ->_callJsonApi ();
98
158
99
159
return $ res ->data ;
0 commit comments