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
|
<?php
/**
* Copyright (c) 2013 Tom Needham <tom@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
class Test_API extends PHPUnit_Framework_TestCase {
// Helps build a response variable
public function buildResponse($shipped=true, $data=null, $code=100) {
return array(
'shipped' => $shipped,
'response' => new OC_OCS_Result($data, $code),
'app' => uniqid('testapp_', true),
);
}
// Validate details of the result
public function checkResult($result, $success=true) {
// Check response is of correct type
$this->assertEquals('OC_OCS_Result', get_class($result));
// CHeck if it succeeded
$this->assertEquals($success, $result->succeeded());
}
// Test the merging of multiple responses
public function testMergeResponses(){
// Tests that app responses are merged correctly
// Setup some data arrays
$data1 = array(
'users' => array(
'tom' => array(
'key' => 'value',
),
'frank' => array(
'key' => 'value',
),
));
$data2 = array(
'users' => array(
'tom' => array(
'key' => 'newvalue',
),
'jan' => array(
'key' => 'value',
),
));
// Test merging one success result
$response = $this->buildResponse(true, $data1);
$result = OC_API::mergeResponses(array($response));
$this->assertEquals($response['response'], $result);
$this->checkResult($result);
$response = $this->buildResponse(true, $data1, 101);
$result = OC_API::mergeResponses(array($response));
$this->assertEquals($response['response'], $result);
$this->checkResult($result);
$response = $this->buildResponse(true, $data1, 997);
$result = OC_API::mergeResponses(array($response));
$this->assertEquals($response['response'], $result);
$this->checkResult($result, false);
// Two shipped success results
$result = OC_API::mergeResponses(array(
$this->buildResponse(true, $data1),
$this->buildResponse(true, $data2),
));
$this->checkResult($result);
$resultData = $result->getData();
$this->assertArrayHasKey('jan', $resultData['users']);
// Two shipped results, one success and one failure
$result = OC_API::mergeResponses(array(
$this->buildResponse(true, $data1),
$this->buildResponse(true, $data2, 997),
));
$this->checkResult($result, false);
$resultData = $result->getData();
$this->assertArrayHasKey('jan', $resultData['users']);
// Two shipped results, both failure
$result = OC_API::mergeResponses(array(
$this->buildResponse(true, $data1, 997),
$this->buildResponse(true, $data2, 997),
));
$this->checkResult($result, false);
$resultData = $result->getData();
$this->assertArrayHasKey('jan', $resultData['users']);
// Two third party success results
$result = OC_API::mergeResponses(array(
$this->buildResponse(false, $data1),
$this->buildResponse(false, $data2),
));
$this->checkResult($result);
$resultData = $result->getData();
$this->assertArrayHasKey('jan', $resultData['users']);
// Two third party results, one success and one failure
$result = OC_API::mergeResponses(array(
$this->buildResponse(false, $data1),
$this->buildResponse(false, $data2, 997),
));
$this->checkResult($result, false);
$resultData = $result->getData();
$this->assertArrayHasKey('jan', $resultData['users']);
// Two third party results, both failure
$result = OC_API::mergeResponses(array(
$this->buildResponse(false, $data1, 997),
$this->buildResponse(false, $data2, 997),
));
$this->checkResult($result, false);
$resultData = $result->getData();
$this->assertArrayHasKey('jan', $resultData['users']);
// One of each, both success
$result = OC_API::mergeResponses(array(
$this->buildResponse(false, $data1),
$this->buildResponse(true, $data2),
));
$this->checkResult($result);
$resultData = $result->getData();
$this->assertArrayHasKey('jan', $resultData['users']);
// One of each, both failure
$result = OC_API::mergeResponses(array(
$this->buildResponse(false, $data1, 997),
$this->buildResponse(true, $data2, 997),
));
$this->checkResult($result, false);
$resultData = $result->getData();
$this->assertArrayHasKey('jan', $resultData['users']);
// One of each, shipped success
$result = OC_API::mergeResponses(array(
$this->buildResponse(false, $data1, 997),
$this->buildResponse(true, $data2),
));
$this->checkResult($result);
$resultData = $result->getData();
$this->assertArrayHasKey('jan', $resultData['users']);
// One of each, third party success
$result = OC_API::mergeResponses(array(
$this->buildResponse(false, $data1),
$this->buildResponse(true, $data2, 997),
));
$this->checkResult($result, false);
$resultData = $result->getData();
$this->assertArrayHasKey('jan', $resultData['users']);
}
}
|