|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the xAPI package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Christian Flothmann <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Xabbuh\XApi\Client\Tests\Api; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
15
|
|
|
use Xabbuh\XApi\Client\Request\HandlerInterface; |
|
16
|
|
|
use Xabbuh\XApi\Common\Exception\NotFoundException; |
|
17
|
|
|
use Xabbuh\XApi\Serializer\ActorSerializer; |
|
18
|
|
|
use Xabbuh\XApi\Serializer\DocumentDataSerializer; |
|
19
|
|
|
use Xabbuh\XApi\Serializer\SerializerRegistry; |
|
20
|
|
|
use Xabbuh\XApi\Serializer\StatementResultSerializer; |
|
21
|
|
|
use Xabbuh\XApi\Serializer\StatementSerializer; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @author Christian Flothmann <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract class ApiClientTest extends \PHPUnit_Framework_TestCase |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var HandlerInterface|\PHPUnit_Framework_MockObject_MockObject |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $requestHandler; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $serializer; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var SerializerRegistry |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $serializerRegistry; |
|
42
|
|
|
|
|
43
|
|
|
protected function setUp() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->requestHandler = $this->createRequestHandlerMock(); |
|
46
|
|
|
$this->serializer = $this->createSerializerMock(); |
|
47
|
|
|
$this->serializerRegistry = $this->createSerializerRegistry(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected function createRequestHandlerMock() |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->getMock('\Xabbuh\XApi\Client\Request\HandlerInterface'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function createSerializerRegistry() |
|
56
|
|
|
{ |
|
57
|
|
|
$registry = new SerializerRegistry(); |
|
58
|
|
|
$registry->setStatementSerializer(new StatementSerializer($this->serializer)); |
|
59
|
|
|
$registry->setStatementResultSerializer(new StatementResultSerializer($this->serializer)); |
|
60
|
|
|
$registry->setActorSerializer(new ActorSerializer($this->serializer)); |
|
61
|
|
|
$registry->setDocumentDataSerializer(new DocumentDataSerializer($this->serializer)); |
|
62
|
|
|
|
|
63
|
|
|
return $registry; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function createSerializerMock() |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->getMock('\Symfony\Component\Serializer\SerializerInterface'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected function validateDeserializer($data, $type, $returnValue) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->serializer |
|
74
|
|
|
->expects($this->once()) |
|
75
|
|
|
->method('deserialize') |
|
76
|
|
|
->with($data, 'Xabbuh\XApi\Model\\'.$type, 'json') |
|
77
|
|
|
->will($this->returnValue($returnValue)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected function validateSerializer(array $serializerMap) |
|
81
|
|
|
{ |
|
82
|
|
|
$this |
|
83
|
|
|
->serializer |
|
84
|
|
|
->expects($this->any()) |
|
85
|
|
|
->method('serialize') |
|
86
|
|
|
->will($this->returnCallback(function ($data) use ($serializerMap) { |
|
87
|
|
|
foreach ($serializerMap as $entry) { |
|
88
|
|
|
if ($data == $entry['data']) { |
|
89
|
|
|
return $entry['result']; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return ''; |
|
94
|
|
|
})); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function createRequestMock($response = null) |
|
98
|
|
|
{ |
|
99
|
|
|
$request = $this->getMock('\Guzzle\Http\Message\RequestInterface'); |
|
100
|
|
|
|
|
101
|
|
|
if (null !== $response) { |
|
102
|
|
|
$request->expects($this->any()) |
|
103
|
|
|
->method('send') |
|
104
|
|
|
->will($this->returnValue($response)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $request; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
protected function createResponseMock($statusCode, $body) |
|
111
|
|
|
{ |
|
112
|
|
|
$response = $this->getMock( |
|
113
|
|
|
'\Guzzle\Http\Message\Response', |
|
114
|
|
|
array(), |
|
115
|
|
|
array($statusCode) |
|
116
|
|
|
); |
|
117
|
|
|
$response->expects($this->any()) |
|
118
|
|
|
->method('getStatusCode') |
|
119
|
|
|
->will($this->returnValue($statusCode)); |
|
120
|
|
|
$response->expects($this->any()) |
|
121
|
|
|
->method('getBody') |
|
122
|
|
|
->will($this->returnValue($body)); |
|
123
|
|
|
|
|
124
|
|
|
return $response; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
protected function validateRequest($method, $uri, array $urlParameters, $body = null, $response = null) |
|
128
|
|
|
{ |
|
129
|
|
|
$request = $this->createRequestMock($response); |
|
130
|
|
|
$this |
|
131
|
|
|
->requestHandler |
|
132
|
|
|
->expects($this->once()) |
|
133
|
|
|
->method('createRequest') |
|
134
|
|
|
->with($method, $uri, $urlParameters, $body) |
|
135
|
|
|
->will($this->returnValue($request)); |
|
136
|
|
|
|
|
137
|
|
|
return $request; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
protected function validateRetrieveApiCall($method, $uri, array $urlParameters, $statusCode, $type, $transformedResult, array $serializerMap = array()) |
|
141
|
|
|
{ |
|
142
|
|
|
$rawResponse = 'the-server-response'; |
|
143
|
|
|
$response = $this->createResponseMock($statusCode, $rawResponse); |
|
144
|
|
|
$request = $this->validateRequest($method, $uri, $urlParameters, null, $response); |
|
145
|
|
|
|
|
146
|
|
|
if (404 === $statusCode) { |
|
147
|
|
|
$this |
|
148
|
|
|
->requestHandler |
|
149
|
|
|
->expects($this->once()) |
|
150
|
|
|
->method('executeRequest') |
|
151
|
|
|
->with($request) |
|
152
|
|
|
->will($this->throwException(new NotFoundException('Not found'))); |
|
153
|
|
|
} else { |
|
154
|
|
|
$this |
|
155
|
|
|
->requestHandler |
|
156
|
|
|
->expects($this->once()) |
|
157
|
|
|
->method('executeRequest') |
|
158
|
|
|
->with($request) |
|
159
|
|
|
->will($this->returnValue($response)); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$this->validateSerializer($serializerMap); |
|
163
|
|
|
|
|
164
|
|
|
if ($statusCode < 400) { |
|
165
|
|
|
$this->validateDeserializer($rawResponse, $type, $transformedResult); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
protected function validateStoreApiCall($method, $uri, array $urlParameters, $statusCode, $rawResponse, $object, array $serializerMap = array()) |
|
170
|
|
|
{ |
|
171
|
|
|
$rawRequest = 'the-request-body'; |
|
172
|
|
|
$response = $this->createResponseMock($statusCode, $rawResponse); |
|
173
|
|
|
$request = $this->validateRequest($method, $uri, $urlParameters, $rawRequest, $response); |
|
174
|
|
|
$this |
|
175
|
|
|
->requestHandler |
|
176
|
|
|
->expects($this->once()) |
|
177
|
|
|
->method('executeRequest') |
|
178
|
|
|
->with($request, array($statusCode)) |
|
179
|
|
|
->will($this->returnValue($response)); |
|
180
|
|
|
$serializerMap[] = array('data' => $object, 'result' => $rawRequest); |
|
181
|
|
|
$this->validateSerializer($serializerMap); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
protected function validateDeleteDocumentCall($uri, array $urlParameters, array $serializerMap = array()) |
|
185
|
|
|
{ |
|
186
|
|
|
$response = $this->createResponseMock(204, ''); |
|
187
|
|
|
$this->validateRequest('delete', $uri, $urlParameters, '', $response); |
|
188
|
|
|
$this->validateSerializer($serializerMap); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|