Open Data Catalog v2.0.0
WebServiceApiTest.php
Go to the documentation of this file.
1 <?php
2 
4 
12 use MockChain\Chain;
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;
18 
22 class WebServiceApiTest extends TestCase {
23 
27  public function testGetAll() {
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]);
33 
34  $controller = WebServiceApi::create($mockChain->getMock());
35  $response = $controller->getAll('dataset');
36  $this->assertEquals(json_encode($objects), $response->getContent());
37  }
38 
42  public function testGet() {
43  $json = '{"name": "hello"}';
44  $mockChain = $this->getCommonMockChain();
45  $mockChain->add(Service::class, 'get', $json);
46 
47  $controller = WebServiceApi::create($mockChain->getMock());
48  $response = $controller->get(1, 'dataset');
49  $this->assertEquals('{"name":"hello"}', $response->getContent());
50  }
51 
55  public function testGetResources() {
56  $mockChain = $this->getCommonMockChain();
57  $distributions = [(object) ["title" => "Foo"], (object) ["title" => "Bar"]];
58  $mockChain->add(Service::class, 'getResources', $distributions);
59 
60  $controller = WebServiceApi::create($mockChain->getMock());
61  $response = $controller->getResources(1, 'dataset');
62  $this->assertEquals(json_encode($distributions), $response->getContent());
63  }
64 
68  public function testGetResourcesException() {
69  $mockChain = $this->getCommonMockChain();;
70  $mockChain->add(Service::class, 'getResources', new \Exception("bad"));
71 
72  $controller = WebServiceApi::create($mockChain->getMock());
73  $response = $controller->getResources(1, 'dataset');
74  $this->assertEquals('{"message":"bad"}', $response->getContent());
75  }
76 
80  public function testGetException() {
81  $mockChain = $this->getCommonMockChain();;
82  $mockChain->add(Service::class, 'get', new \Exception("bad"));
83 
84  $controller = WebServiceApi::create($mockChain->getMock());
85  $response = $controller->get(1, 'dataset');
86  $this->assertEquals('{"message":"bad"}', $response->getContent());
87  }
88 
92  public function testPost() {
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");
98 
99  $controller = WebServiceApi::create($mockChain->getMock());
100  $response = $controller->post('dataset');
101  $this->assertEquals('{"endpoint":"http:\/\/blah\/1","identifier":"1"}', $response->getContent());
102  }
103 
107  public function testPostNoIdentifier() {
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");
113 
114  $controller = WebServiceApi::create($mockChain->getMock());
115  $response = $controller->post('dataset');
116  $this->assertEquals('{"endpoint":"http:\/\/blah\/1","identifier":"1"}', $response->getContent());
117  }
118 
122  public function testPostNoIdentifierException() {
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"));
128 
129  $controller = WebServiceApi::create($mockChain->getMock());
130  $response = $controller->post('dataset');
131  $this->assertEquals('{"message":"bad"}', $response->getContent());
132  }
133 
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"}')
142  ->add(Service::class, 'post', new ExistingObjectException("Already exists"));
143 
144  $controller = WebServiceApi::create($mockChain->getMock());
145  $response = $controller->post('dataset');
146  $this->assertEquals('{"message":"Already exists"}', $response->getContent());
147  }
148 
152  public function testPut() {
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]);
158 
159  $controller = WebServiceApi::create($mockChain->getMock());
160  $response = $controller->put(1, 'dataset');
161  $this->assertEquals('{"endpoint":"http:\/\/blah","identifier":"1"}', $response->getContent());
162  }
163 
167  public function testPutInvalidJsonException() {
168  $mockChain = $this->getCommonMockChain()
169  ->add(RequestStack::class, 'getCurrentRequest', Request::class)
170  ->add(Request::class, 'getContent', "{");
171 
172  $controller = WebServiceApi::create($mockChain->getMock());
173  $response = $controller->put(1, 'dataset');
174  $this->assertEquals('{"message":"Invalid JSON"}', $response->getContent());
175  }
176 
180  public function testPutMissingPayloadException() {
181  $mockChain = $this->getCommonMockChain()
182  ->add(RequestStack::class, 'getCurrentRequest', Request::class)
183  ->add(Request::class, 'getContent', "");
184 
185  $controller = WebServiceApi::create($mockChain->getMock());
186  $response = $controller->put(1, 'dataset');
187  $this->assertEquals('{"message":"Empty body"}', $response->getContent());
188  }
189 
193  public function testPutWithEquivalentData() {
194  $existing = '{"identifier":"1","title":"Foo"}';
195  $updating = <<<EOF
196  {
197  "title": "Foo",
198  "identifier": "1"
199  }
200 EOF;
201 
202  $mockChain = $this->getCommonMockChain()
203  ->add(RequestStack::class, 'getCurrentRequest', Request::class)
204  ->add(Request::class, 'getContent', $updating)
205  ->add(Request::class, 'getRequestUri', "http://blah")
206  ->add(Service::class, "put", new UnmodifiedObjectException("No changes"));
207 
208  $controller = WebServiceApi::create($mockChain->getMock());
209  $response = $controller->put('dataset', 1);
210  $this->assertEquals('{"message":"No changes"}', $response->getContent());
211  }
212 
217  $mockChain = $this->getCommonMockChain()
218  ->add(RequestStack::class, 'getCurrentRequest', new \Exception("Unknown error"));
219 
220  $controller = WebServiceApi::create($mockChain->getMock());
221  $response = $controller->put('dataset', 1);
222  $this->assertEquals('{"message":"Unknown error"}', $response->getContent());
223  }
224 
228  public function testPatch() {
229  $thing = (object) [];
230 
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");
236 
237  $controller = WebServiceApi::create($mockChain->getMock());
238  $response = $controller->patch('dataset', 1);
239  $this->assertEquals('{"endpoint":"http:\/\/blah","identifier":1}', $response->getContent());
240  }
241 
245  public function testPatchModifyId() {
246  $thing = ['identifier' => 1];
247 
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");
253 
254  $controller = WebServiceApi::create($mockChain->getMock());
255  $response = $controller->patch(1, 'dataset');
256  $this->assertEquals('{"message":"Identifier cannot be modified"}', $response->getContent());
257  }
258 
262  public function testPatchBadPayload() {
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");
269 
270  $controller = WebServiceApi::create($mockChain->getMock());
271  $response = $controller->patch(1, 'dataset');
272  $this->assertEquals('{"message":"Invalid JSON"}', $response->getContent());
273  }
274 
278  public function testPatchObjectNotFound() {
279  $mockChain = $this->getCommonMockChain()
280  ->add(RequestStack::class, 'getCurrentRequest', Request::class)
281  ->add(Request::class, 'getContent', '{"identifier":"1","title":"foo"}')
282  ->add(Service::class, "patch", new MissingObjectException("Not found"));
283 
284  $controller = WebServiceApi::create($mockChain->getMock());
285  $response = $controller->patch('dataset', 1);
286  $this->assertEquals('{"message":"Not found"}', $response->getContent());
287  }
288 
293  $mockChain = $this->getCommonMockChain()
294  ->add(RequestStack::class, 'getCurrentRequest', new \Exception("Unknown error"));
295 
296  $controller = WebServiceApi::create($mockChain->getMock());
297  $response = $controller->patch('dataset', 1);
298  $this->assertEquals('{"message":"Unknown error"}', $response->getContent());
299  }
300 
304  public function testDelete() {
305  $mockChain = $this->getCommonMockChain();
306  $mockChain->add(Service::class, 'delete', "1");
307 
308  $controller = WebServiceApi::create($mockChain->getMock());
309  $response = $controller->delete('dataset', "1");
310  $this->assertEquals('{"message":"Dataset 1 has been deleted."}', $response->getContent());
311  }
312 
317  $mockChain = $this->getCommonMockChain()
318  ->add(Service::class, 'delete', new \Exception("Unknown error"));
319 
320  $controller = WebServiceApi::create($mockChain->getMock());
321  $response = $controller->delete('dataset', 1);
322  $this->assertEquals('{"message":"Unknown error"}', $response->getContent());
323  }
324 
328  public function testGetCatalog() {
329  $catalog = (object) ["foo" => "bar"];
330 
331  $mockChain = $this->getCommonMockChain();
332  $mockChain->add(Service::class, 'getCatalog', $catalog);
333 
334  $controller = WebServiceApi::create($mockChain->getMock());
335  $response = $controller->getCatalog();
336  $this->assertEquals(json_encode($catalog), $response->getContent());
337  }
338 
342  public function testGetCatalogException() {
343  $mockChain = $this->getCommonMockChain();
344  $mockChain->add(Service::class, 'getCatalog', new \Exception("bad"));
345 
346  $controller = WebServiceApi::create($mockChain->getMock());
347  $response = $controller->getCatalog();
348  $this->assertEquals('{"message":"bad"}', $response->getContent());
349  }
350 
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)
359  ->index(0)
360  );
361  $mockChain->add(SchemaRetriever::class, 'retrieve', "{}");
362  return $mockChain;
363  }
364 
365 }
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testPatchObjectNotFound
testPatchObjectNotFound()
Definition: WebServiceApiTest.php:278
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testPut
testPut()
Definition: WebServiceApiTest.php:152
Drupal\dkan_metastore\Exception\ExistingObjectException
Definition: ExistingObjectException.php:10
Drupal\dkan_data\Storage\Data
Definition: Data.php:16
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testGet
testGet()
Definition: WebServiceApiTest.php:42
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testGetAll
testGetAll()
Definition: WebServiceApiTest.php:27
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testPost
testPost()
Definition: WebServiceApiTest.php:92
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest
Definition: WebServiceApiTest.php:22
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testPostExistingObjectException
testPostExistingObjectException()
Definition: WebServiceApiTest.php:137
Drupal\dkan_metastore\Exception\UnmodifiedObjectException
Definition: UnmodifiedObjectException.php:10
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testGetCatalog
testGetCatalog()
Definition: WebServiceApiTest.php:328
Drupal\dkan_metastore\WebServiceApi\create
static create(ContainerInterface $container)
Definition: WebServiceApi.php:42
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testPatchExceptionOtherThanMetastore
testPatchExceptionOtherThanMetastore()
Definition: WebServiceApiTest.php:292
Drupal\dkan_schema\SchemaRetriever
Definition: SchemaRetriever.php:11
Drupal\Tests\dkan_metastore\Unit
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testPutWithEquivalentData
testPutWithEquivalentData()
Definition: WebServiceApiTest.php:193
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testGetCatalogException
testGetCatalogException()
Definition: WebServiceApiTest.php:342
Drupal\dkan_metastore\WebServiceApi
Definition: WebServiceApi.php:20
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testPostNoIdentifier
testPostNoIdentifier()
Definition: WebServiceApiTest.php:107
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testPatchBadPayload
testPatchBadPayload()
Definition: WebServiceApiTest.php:262
Drupal\dkan_metastore\Service
Definition: Service.php:20
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testPatchModifyId
testPatchModifyId()
Definition: WebServiceApiTest.php:245
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testPatch
testPatch()
Definition: WebServiceApiTest.php:228
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testPutInvalidJsonException
testPutInvalidJsonException()
Definition: WebServiceApiTest.php:167
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testGetResourcesException
testGetResourcesException()
Definition: WebServiceApiTest.php:68
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testDelete
testDelete()
Definition: WebServiceApiTest.php:304
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testPutMissingPayloadException
testPutMissingPayloadException()
Definition: WebServiceApiTest.php:180
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testGetResources
testGetResources()
Definition: WebServiceApiTest.php:55
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testPutExceptionOtherThanMetastore
testPutExceptionOtherThanMetastore()
Definition: WebServiceApiTest.php:216
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testPostNoIdentifierException
testPostNoIdentifierException()
Definition: WebServiceApiTest.php:122
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testGetException
testGetException()
Definition: WebServiceApiTest.php:80
Drupal\Tests\dkan_metastore\Unit\WebServiceApiTest\testDeleteExceptionOtherThanMetastore
testDeleteExceptionOtherThanMetastore()
Definition: WebServiceApiTest.php:316
Drupal\dkan_metastore\Exception\MissingObjectException
Definition: MissingObjectException.php:10