13 use MockChain\Options;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
16 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\HttpFoundation\RequestStack;
28 $json =
'{"name": "hello"}';
29 $object = json_decode($json);
30 $objects = [$object, $object, $object];
31 $mockChain = $this->getCommonMockChain();
32 $mockChain->add(Service::class,
'getAll', [$object, $object, $object]);
35 $response = $controller->getAll(
'dataset');
36 $this->assertEquals(json_encode($objects), $response->getContent());
43 $json =
'{"name": "hello"}';
44 $mockChain = $this->getCommonMockChain();
45 $mockChain->add(Service::class,
'get', $json);
48 $response = $controller->get(1,
'dataset');
49 $this->assertEquals(
'{"name":"hello"}', $response->getContent());
56 $mockChain = $this->getCommonMockChain();
57 $distributions = [(object) [
"title" =>
"Foo"], (
object) [
"title" =>
"Bar"]];
58 $mockChain->add(Service::class,
'getResources', $distributions);
61 $response = $controller->getResources(1,
'dataset');
62 $this->assertEquals(json_encode($distributions), $response->getContent());
69 $mockChain = $this->getCommonMockChain();;
70 $mockChain->add(Service::class,
'getResources',
new \Exception(
"bad"));
73 $response = $controller->getResources(1,
'dataset');
74 $this->assertEquals(
'{"message":"bad"}', $response->getContent());
81 $mockChain = $this->getCommonMockChain();;
82 $mockChain->add(Service::class,
'get',
new \Exception(
"bad"));
85 $response = $controller->get(1,
'dataset');
86 $this->assertEquals(
'{"message":"bad"}', $response->getContent());
93 $mockChain = $this->getCommonMockChain();
94 $mockChain->add(RequestStack::class,
'getCurrentRequest', Request::class);
95 $mockChain->add(Request::class,
'getRequestUri',
"http://blah");
96 $mockChain->add(Request::class,
'getContent',
'{"identifier": "1"}');
97 $mockChain->add(Service::class,
'post',
"1");
100 $response = $controller->post(
'dataset');
101 $this->assertEquals(
'{"endpoint":"http:\/\/blah\/1","identifier":"1"}', $response->getContent());
108 $mockChain = $this->getCommonMockChain();
109 $mockChain->add(RequestStack::class,
'getCurrentRequest', Request::class);
110 $mockChain->add(Request::class,
'getRequestUri',
"http://blah");
111 $mockChain->add(Request::class,
'getContent',
'{ }');
112 $mockChain->add(Service::class,
'post',
"1");
115 $response = $controller->post(
'dataset');
116 $this->assertEquals(
'{"endpoint":"http:\/\/blah\/1","identifier":"1"}', $response->getContent());
123 $mockChain = $this->getCommonMockChain();
124 $mockChain->add(RequestStack::class,
'getCurrentRequest', Request::class);
125 $mockChain->add(Request::class,
'getRequestUri',
"http://blah");
126 $mockChain->add(Request::class,
'getContent',
'{ }');
127 $mockChain->add(Service::class,
'post',
new \Exception(
"bad"));
130 $response = $controller->post(
'dataset');
131 $this->assertEquals(
'{"message":"bad"}', $response->getContent());
138 $mockChain = $this->getCommonMockChain()
139 ->add(RequestStack::class,
'getCurrentRequest', Request::class)
140 ->add(Request::class,
'getRequestUri',
"http://blah")
141 ->add(Request::class,
'getContent',
'{"identifier": "1"}')
145 $response = $controller->post(
'dataset');
146 $this->assertEquals(
'{"message":"Already exists"}', $response->getContent());
153 $mockChain = $this->getCommonMockChain();
154 $mockChain->add(RequestStack::class,
'getCurrentRequest', Request::class);
155 $mockChain->add(Request::class,
'getContent',
"{ }");
156 $mockChain->add(Request::class,
'getRequestUri',
"http://blah");
157 $mockChain->add(Service::class,
"put", [
"identifier" =>
"1",
"new" => FALSE]);
160 $response = $controller->put(1,
'dataset');
161 $this->assertEquals(
'{"endpoint":"http:\/\/blah","identifier":"1"}', $response->getContent());
168 $mockChain = $this->getCommonMockChain()
169 ->add(RequestStack::class,
'getCurrentRequest', Request::class)
170 ->add(Request::class,
'getContent',
"{");
173 $response = $controller->put(1,
'dataset');
174 $this->assertEquals(
'{"message":"Invalid JSON"}', $response->getContent());
181 $mockChain = $this->getCommonMockChain()
182 ->add(RequestStack::class,
'getCurrentRequest', Request::class)
183 ->add(Request::class,
'getContent',
"");
186 $response = $controller->put(1,
'dataset');
187 $this->assertEquals(
'{"message":"Empty body"}', $response->getContent());
194 $existing =
'{"identifier":"1","title":"Foo"}';
202 $mockChain = $this->getCommonMockChain()
203 ->add(RequestStack::class,
'getCurrentRequest', Request::class)
204 ->add(Request::class,
'getContent', $updating)
205 ->add(Request::class,
'getRequestUri',
"http://blah")
209 $response = $controller->put(
'dataset', 1);
210 $this->assertEquals(
'{"message":"No changes"}', $response->getContent());
217 $mockChain = $this->getCommonMockChain()
218 ->add(RequestStack::class,
'getCurrentRequest',
new \Exception(
"Unknown error"));
221 $response = $controller->put(
'dataset', 1);
222 $this->assertEquals(
'{"message":"Unknown error"}', $response->getContent());
229 $thing = (object) [];
231 $mockChain = $this->getCommonMockChain();
232 $mockChain->add(RequestStack::class,
'getCurrentRequest', Request::class);
233 $mockChain->add(Request::class,
'getContent', json_encode($thing));
234 $mockChain->add(Request::class,
'getRequestUri',
"http://blah");
235 $mockChain->add(Service::class,
"patch",
"1");
238 $response = $controller->patch(
'dataset', 1);
239 $this->assertEquals(
'{"endpoint":"http:\/\/blah","identifier":1}', $response->getContent());
246 $thing = [
'identifier' => 1];
248 $mockChain = $this->getCommonMockChain();
249 $mockChain->add(RequestStack::class,
'getCurrentRequest', Request::class);
250 $mockChain->add(Request::class,
'getContent', json_encode($thing));
251 $mockChain->add(Request::class,
'getRequestUri',
"http://blah");
252 $mockChain->add(Service::class,
"patch",
"1");
255 $response = $controller->patch(1,
'dataset');
256 $this->assertEquals(
'{"message":"Identifier cannot be modified"}', $response->getContent());
263 $mockChain = $this->getCommonMockChain();
264 $mockChain->add(Data::class,
'retrieve',
"{ }");
265 $mockChain->add(Data::class,
'store',
new \Exception(
"Could not store"));
266 $mockChain->add(RequestStack::class,
'getCurrentRequest', Request::class);
267 $mockChain->add(Request::class,
'getContent',
"{");
268 $mockChain->add(Request::class,
'getRequestUri',
"http://blah");
271 $response = $controller->patch(1,
'dataset');
272 $this->assertEquals(
'{"message":"Invalid JSON"}', $response->getContent());
279 $mockChain = $this->getCommonMockChain()
280 ->add(RequestStack::class,
'getCurrentRequest', Request::class)
281 ->add(Request::class,
'getContent',
'{"identifier":"1","title":"foo"}')
285 $response = $controller->patch(
'dataset', 1);
286 $this->assertEquals(
'{"message":"Not found"}', $response->getContent());
293 $mockChain = $this->getCommonMockChain()
294 ->add(RequestStack::class,
'getCurrentRequest',
new \Exception(
"Unknown error"));
297 $response = $controller->patch(
'dataset', 1);
298 $this->assertEquals(
'{"message":"Unknown error"}', $response->getContent());
305 $mockChain = $this->getCommonMockChain();
306 $mockChain->add(Service::class,
'delete',
"1");
309 $response = $controller->delete(
'dataset',
"1");
310 $this->assertEquals(
'{"message":"Dataset 1 has been deleted."}', $response->getContent());
317 $mockChain = $this->getCommonMockChain()
318 ->add(Service::class,
'delete',
new \Exception(
"Unknown error"));
321 $response = $controller->delete(
'dataset', 1);
322 $this->assertEquals(
'{"message":"Unknown error"}', $response->getContent());
329 $catalog = (object) [
"foo" =>
"bar"];
331 $mockChain = $this->getCommonMockChain();
332 $mockChain->add(Service::class,
'getCatalog', $catalog);
335 $response = $controller->getCatalog();
336 $this->assertEquals(json_encode($catalog), $response->getContent());
343 $mockChain = $this->getCommonMockChain();
344 $mockChain->add(Service::class,
'getCatalog',
new \Exception(
"bad"));
347 $response = $controller->getCatalog();
348 $this->assertEquals(
'{"message":"bad"}', $response->getContent());
354 private function getCommonMockChain() {
355 $mockChain =
new Chain($this);
356 $mockChain->add(ContainerInterface::class,
'get',
357 (
new Options)->add(
'request_stack', RequestStack::class)
358 ->add(
"dkan_metastore.service", Service::class)
361 $mockChain->add(SchemaRetriever::class,
'retrieve',
"{}");