-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathContextTest.php
More file actions
382 lines (349 loc) · 14.4 KB
/
Copy pathContextTest.php
File metadata and controls
382 lines (349 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
/*
Copyright 2014 Rustici Software
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
namespace TinCanTest;
use PHPUnit\Framework\TestCase;
use TinCan\Agent;
use TinCan\Context;
use TinCan\ContextActivities;
use TinCan\Extensions;
use TinCan\Group;
use TinCan\StatementRef;
use TinCan\Util;
class ContextTest extends TestCase {
use TestCompareWithSignatureTrait;
private $emptyProperties = array(
'registration',
'revision',
'platform',
'language',
);
private $nonEmptyProperties = array(
'contextActivities',
'extensions',
);
public function testInstantiation() {
$obj = new Context();
$this->assertInstanceOf('TinCan\Context', $obj);
foreach ($this->emptyProperties as $property) {
$this->assertAttributeEmpty($property, $obj, "$property empty");
}
foreach ($this->nonEmptyProperties as $property) {
$this->assertAttributeNotEmpty($property, $obj, "$property not empty");
}
}
public function testUsesArraySetterTrait() {
$this->assertContains('TinCan\ArraySetterTrait', class_uses('TinCan\Context'));
}
public function testUsesFromJSONTrait() {
$this->assertContains('TinCan\FromJSONTrait', class_uses('TinCan\Context'));
}
public function testUsesAsVersionTrait() {
$this->assertContains('TinCan\AsVersionTrait', class_uses('TinCan\Context'));
}
/*
// TODO: need to loop possible configs
public function testFromJSONInstantiations() {
$obj = Context::fromJSON('{"mbox":"' . COMMON_GROUP_MBOX . '", "member":[{"mbox":"' . COMMON_MBOX . '"}]}');
$this->assertInstanceOf('TinCan\Context', $obj);
$this->assertSame(COMMON_GROUP_MBOX, $obj->getMbox(), 'mbox value');
$this->assertEquals([['mbox' => COMMON_MBOX]], $obj->getMember(), 'member list');
}
*/
public function testAsVersion() {
$args = [
'registration' => Util::getUUID(),
'instructor' => [
'name' => 'test agent'
],
'team' => [
'name' => 'test group'
],
'contextActivities' => [
'category' => [
[
'id' => 'test category'
]
]
],
'revision' => 'test revision',
'platform' => 'test platform',
'language' => 'test language',
'statement' => [
'id' => Util::getUUID()
],
'extensions' => [],
];
$args['extensions'][COMMON_EXTENSION_ID_1] = "test";
$obj = Context::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');
$args['instructor']['objectType'] = 'Agent';
$args['team']['objectType'] = 'Group';
$args['contextActivities']['category'][0]['objectType'] = 'Activity';
$args['statement']['objectType'] = 'StatementRef';
$this->assertEquals($versioned, $args, "serialized version matches corrected");
}
public function testAsVersionEmpty() {
$args = [];
$obj = Context::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');
$this->assertEquals($versioned, $args, "serialized version matches original");
}
public function testAsVersionEmptyLists() {
$args = [
'contextActivities' => [
'category' => []
],
'extensions' => [],
];
$obj = Context::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
$versioned = $obj->asVersion('1.0.0');
unset($args['contextActivities']);
unset($args['extensions']);
$this->assertEquals($versioned, $args, "serialized version matches corrected");
}
public function testSetInstructor() {
$common_agent_cfg = [ 'mbox' => COMMON_MBOX ];
$common_agent = new Agent($common_agent_cfg);
$common_group_cfg = [ 'mbox' => COMMON_MBOX, 'objectType' => 'Group' ];
$common_group = new Group($common_agent_cfg);
$obj = new Context();
$obj->setInstructor($common_agent_cfg);
$this->assertEquals($common_agent, $obj->getInstructor(), "agent config");
$obj->setInstructor(null);
$this->assertEmpty($obj->getInstructor(), "empty");
}
public function testCompareWithSignature() {
$registration1 = Util::getUUID();
$registration2 = Util::getUUID();
$instructor1 = new Agent(
[ 'mbox' => COMMON_MBOX ]
);
$instructor2 = new Agent(
[ 'account' => [ 'homePage' => COMMON_ACCT_HOMEPAGE, 'name' => COMMON_ACCT_NAME ]]
);
$team1 = new Agent(
[ 'mbox' => COMMON_MBOX ]
);
$team2 = new Agent(
[ 'account' => [ 'homePage' => COMMON_ACCT_HOMEPAGE, 'name' => COMMON_ACCT_NAME ]]
);
$contextActivities1 = new ContextActivities(
[ 'parent' => [ COMMON_ACTIVITY_ID ]]
);
$contextActivities2 = new ContextActivities(
[ 'parent' => [ COMMON_ACTIVITY_ID . '/parent' ]],
[ 'grouping' => [ COMMON_ACTIVITY_ID ]]
);
$ref1 = new StatementRef(
[ 'id' => Util::getUUID() ]
);
$ref2 = new StatementRef(
[ 'id' => Util::getUUID() ]
);
$extensions1 = new Extensions(
[
COMMON_EXTENSION_ID_1 => 'test1',
COMMON_EXTENSION_ID_2 => 'test2'
]
);
$extensions2 = new Extensions(
[
COMMON_EXTENSION_ID_1 => 'test1'
]
);
$full = [
'registration' => $registration1,
'instructor' => $instructor1,
'team' => $team1,
'contextActivities' => $contextActivities1,
'revision' => '1',
'platform' => 'mobile',
'language' => 'en-US',
'statement' => $ref1,
'extensions' => $extensions1
];
$cases = [
[
'description' => 'all null',
'objArgs' => []
],
[
'description' => 'registration',
'objArgs' => ['registration' => $registration1]
],
[
'description' => 'instructor',
'objArgs' => ['instructor' => $instructor1]
],
[
'description' => 'team',
'objArgs' => ['team' => $team1]
],
[
'description' => 'contextActivities',
'objArgs' => ['contextActivities' => $contextActivities1]
],
[
'description' => 'revision',
'objArgs' => ['revision' => '1']
],
[
'description' => 'platform',
'objArgs' => ['platform' => 'mobile']
],
[
'description' => 'language',
'objArgs' => ['language' => 'en-US']
],
[
'description' => 'statement',
'objArgs' => ['statement' => $ref1]
],
[
'description' => 'extensions',
'objArgs' => ['extensions' => $extensions1]
],
[
'description' => 'all',
'objArgs' => $full
],
[
'description' => 'registration only: mismatch',
'objArgs' => ['registration' => $registration1 ],
'sigArgs' => ['registration' => $registration2 ],
'reason' => 'Comparison of registration failed: value is not the same'
],
[
'description' => 'instructor only: mismatch',
'objArgs' => ['instructor' => $instructor1 ],
'sigArgs' => ['instructor' => $instructor2 ],
'reason' => 'Comparison of instructor failed: Comparison of mbox failed: value is not the same'
],
[
'description' => 'team only: mismatch',
'objArgs' => ['team' => $team1 ],
'sigArgs' => ['team' => $team2 ],
'reason' => 'Comparison of team failed: Comparison of mbox failed: value is not the same'
],
[
'description' => 'contextActivities only: mismatch',
'objArgs' => ['contextActivities' => $contextActivities1 ],
'sigArgs' => ['contextActivities' => $contextActivities2 ],
'reason' => 'Comparison of contextActivities failed: Comparison of parent failed: array lengths differ'
],
[
'description' => 'revision only: mismatch',
'objArgs' => ['revision' => '1' ],
'sigArgs' => ['revision' => '2' ],
'reason' => 'Comparison of revision failed: value is not the same'
],
[
'description' => 'platform only: mismatch',
'objArgs' => ['platform' => 'mobile' ],
'sigArgs' => ['platform' => 'desktop' ],
'reason' => 'Comparison of platform failed: value is not the same'
],
[
'description' => 'language only: mismatch',
'objArgs' => ['language' => 'en-US' ],
'sigArgs' => ['language' => 'en-GB' ],
'reason' => 'Comparison of language failed: value is not the same'
],
[
'description' => 'statement only: mismatch',
'objArgs' => ['statement' => $ref1 ],
'sigArgs' => ['statement' => $ref2 ],
'reason' => 'Comparison of statement failed: Comparison of id failed: value is not the same'
],
[
'description' => 'extensions only: mismatch',
'objArgs' => ['extensions' => $extensions1 ],
'sigArgs' => ['extensions' => $extensions2 ],
'reason' => 'Comparison of extensions failed: http://id.tincanapi.com/extension/location not in signature'
],
[
'description' => 'full: registration mismatch',
'objArgs' => $full,
'sigArgs' => array_replace($full, ['registration' => $registration2]),
'reason' => 'Comparison of registration failed: value is not the same'
],
[
'description' => 'full: instructor mismatch',
'objArgs' => $full,
'sigArgs' => array_replace($full, ['instructor' => $instructor2]),
'reason' => 'Comparison of instructor failed: Comparison of mbox failed: value is not the same'
],
[
'description' => 'full: team mismatch',
'objArgs' => $full,
'sigArgs' => array_replace($full, ['team' => $team2]),
'reason' => 'Comparison of team failed: Comparison of mbox failed: value is not the same'
],
[
'description' => 'full: contextActivities mismatch',
'objArgs' => $full,
'sigArgs' => array_replace($full, ['contextActivities' => $contextActivities2]),
'reason' => 'Comparison of contextActivities failed: Comparison of parent failed: array lengths differ'
],
[
'description' => 'full: revision mismatch',
'objArgs' => $full,
'sigArgs' => array_replace($full, ['revision' => '2']),
'reason' => 'Comparison of revision failed: value is not the same'
],
[
'description' => 'full: platform mismatch',
'objArgs' => $full,
'sigArgs' => array_replace($full, ['platform' => 'desktop']),
'reason' => 'Comparison of platform failed: value is not the same'
],
[
'description' => 'full: language mismatch',
'objArgs' => $full,
'sigArgs' => array_replace($full, ['language' => 'en-GB']),
'reason' => 'Comparison of language failed: value is not the same'
],
[
'description' => 'full: statement mismatch',
'objArgs' => $full,
'sigArgs' => array_replace($full, ['statement' => $ref2]),
'reason' => 'Comparison of statement failed: Comparison of id failed: value is not the same'
],
[
'description' => 'full: extensions mismatch',
'objArgs' => $full,
'sigArgs' => array_replace($full, ['extensions' => $extensions2]),
'reason' => 'Comparison of extensions failed: http://id.tincanapi.com/extension/location not in signature'
]
];
$this->runSignatureCases("TinCan\Context", $cases);
}
public function testSetInstructorConvertToGroup() {
$obj = new Context();
$obj->setInstructor(
[
'objectType' => 'Group'
]
);
$this->assertInstanceOf('TinCan\Group', $obj->getInstructor());
}
public function testSetRegistrationInvalidArgumentException() {
$this->setExpectedException(
'InvalidArgumentException',
'arg1 must be a UUID'
);
$obj = new Context();
$obj->setRegistration('232....3.3..3./2/2/1m3m3m3');
}
}