Open Data Catalog v2.0.0
ServiceTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Drupal\Core\DependencyInjection\Container;
12 use MockChain\Chain;
13 use MockChain\Options;
14 use PHPUnit\Framework\TestCase;
15 use Sae\Sae as Engine;
16 
20 class ServiceTest extends TestCase {
21 
25  public function testGetSchemas() {
26  $container = $this->getCommonMockChain()
27  ->add(SchemaRetriever::class, "getAllIds", ["1"]);
28 
29  $service = Service::create($container->getMock());
30  $this->assertEquals(json_encode(["1" => "blah"]), json_encode($service->getSchemas()));
31  }
32 
36  public function testGetSchema() {
37  $container = $this->getCommonMockChain();
38 
39  $service = Service::create($container->getMock());
40  $this->assertEquals(json_encode("blah"), json_encode($service->getSchema("1")));
41  }
42 
46  public function testGetAll() {
47  $container = $this->getCommonMockChain()
48  ->add(Sae::class, "getInstance", Engine::class)
49  ->add(Engine::class, "get", [json_encode("blah")]);
50 
51  $service = Service::create($container->getMock());
52 
53  $this->assertEquals(json_encode(["blah"]), json_encode($service->getAll("dataset")));
54  }
55 
59  public function testGet() {
60  $container = $this->getCommonMockChain()
61  ->add(Sae::class, "getInstance", Engine::class)
62  ->add(Engine::class, "get", json_encode("blah"));
63 
64  $service = Service::create($container->getMock());
65 
66  $this->assertEquals(json_encode("blah"), $service->get("dataset", "1"));
67  }
68 
72  public function testGetResources() {
73  $dataset = (object) [
74  "identifier" => "1",
75  "distribution" => [
76  (object) ["title" => "hello"],
77  ],
78  ];
79 
80  $container = $this->getCommonMockChain()
81  ->add(Sae::class, "getInstance", Engine::class)
82  ->add(Engine::class, "get", json_encode($dataset));
83 
84  $service = Service::create($container->getMock());
85 
86  $this->assertEquals(json_encode([(object) ["title" => "hello"]]),
87  json_encode($service->getResources("dataset", "1")));
88  }
89 
93  public function testPost() {
94  $container = $this->getCommonMockChain()
95  ->add(Sae::class, "getInstance", Engine::class)
96  ->add(Engine::class, "post", "1");
97 
98  $service = Service::create($container->getMock());
99 
100  $this->assertEquals("1", $service->post("dataset", json_encode("blah")));
101  }
102 
106  public function testPostAlreadyExisting() {
107  $container = $this->getCommonMockChain()
108  ->add(Sae::class, "getInstance", Engine::class)
109  ->add(Engine::class, "get", "1");
110 
111  $service = Service::create($container->getMock());
112 
113  $this->expectException(ExistingObjectException::class);
114  $service->post("dataset", '{"identifier":1,"title":"FooBar"}');
115  }
116 
120  public function testPut() {
121  $container = $this->getCommonMockChain()
122  ->add(Sae::class, "getInstance", Engine::class)
123  ->add(Engine::class, "put", "1")
124  ->add(Engine::class, "get", "1");
125 
126  $service = Service::create($container->getMock());
127 
128  $info = $service->put("dataset", "1", json_encode("blah"));
129  $this->assertEquals("1", $info['identifier']);
130  }
131 
136  $existing = '{"identifier":"1","title":"Foo"}';
137  $updating = '{"identifier":"2","title":"Bar"}';
138 
139  $container = $this->getCommonMockChain()
140  ->add(Sae::class, "getInstance", Engine::class)
141  ->add(Engine::class, "get", $existing);
142 
143  $service = Service::create($container->getMock());
144 
145  $this->expectExceptionMessage("Identifier cannot be modified");
146  $service->put("dataset", "1", $updating);
147  }
148 
152  public function testPutResultingInNewData() {
153  $data = '{"identifier":"3","title":"FooBar"}';
154  $container = $this->getCommonMockChain()
155  ->add(Sae::class, "getInstance", Engine::class)
156  ->add(Engine::class, "get", new \Exception())
157  ->add(Engine::class, "put", "3")
158  ->add(Engine::class, "post", "3");
159 
160  $service = Service::create($container->getMock());
161  $info = $service->put("dataset", "3", $data);
162  $this->assertEquals("3", $info['identifier']);
163  }
164 
169  $existing = '{"identifier":"1","title":"Foo"}';
170 
171  $container = $this->getCommonMockChain()
172  ->add(Sae::class, "getInstance", Engine::class)
173  ->add(Engine::class, "get", $existing);
174 
175  $service = Service::create($container->getMock());
176  $this->expectException(UnmodifiedObjectException::class);
177  $service->put("dataset", "1", $existing);
178  }
179 
184  $existing = '{"identifier":"1","title":"Foo"}';
185  $updating = <<<EOF
186  {
187  "title":"Foo",
188  "identifier":"1"
189  }
190 EOF;
191 
192  $container = $this->getCommonMockChain()
193  ->add(Sae::class, "getInstance", Engine::class)
194  ->add(Engine::class, "get", $existing);
195 
196  $service = Service::create($container->getMock());
197  $this->expectException(UnmodifiedObjectException::class);
198  $service->put("dataset", "1", $updating);
199  }
200 
204  public function testPatch() {
205  $container = $this->getCommonMockChain()
206  ->add(Sae::class, "getInstance", Engine::class)
207  ->add(Engine::class, "patch", "1")
208  ->add(Engine::class, "get", "1");
209 
210  $service = Service::create($container->getMock());
211 
212  $this->assertEquals("1", $service->patch("dataset", "1", json_encode("blah")));
213  }
214 
219  $data = '{"identifier":"1","title":"FooBar"}';
220 
221  $container = $this->getCommonMockChain()
222  ->add(Sae::class, "getInstance", Engine::class)
223  ->add(Engine::class, "get", new \Exception());
224 
225  $service = Service::create($container->getMock());
226  $this->expectException(MissingObjectException::class);
227  $service->patch("dataset", "1", $data);
228  }
229 
233  public function testDelete() {
234  $container = $this->getCommonMockChain()
235  ->add(Sae::class, "getInstance", Engine::class)
236  ->add(Engine::class, "delete", "1")
237  ->add(Engine::class, "get", "1");
238 
239  $service = Service::create($container->getMock());
240 
241  $this->assertEquals("1", $service->delete("dataset", "1"));
242  }
243 
247  public function testGetCatalog() {
248  $catalog = (object) [
249  "@id" => "http://catalog",
250  "dataset" => [],
251  ];
252  $dataset = (object) ["foo" => "bar"];
253 
254  $container = $this->getCommonMockChain()
255  ->add(SchemaRetriever::class, "retrieve", json_encode($catalog))
256  ->add(Sae::class, "getInstance", Engine::class)
257  ->add(Engine::class, "get", [json_encode($dataset), json_encode($dataset)]);
258 
259  $service = Service::create($container->getMock());
260  $catalog->dataset = [
261  $dataset,
262  $dataset,
263  ];
264  $this->assertEquals($catalog, $service->getCatalog());
265  }
266 
270  public function getCommonMockChain() {
271  $options = (new Options())
272  ->add('dkan_schema.schema_retriever', SchemaRetriever::class)
273  ->add('dkan_metastore.sae_factory', Sae::class)
274  ->index(0);
275 
276  return (new Chain($this))
277  ->add(Container::class, "get", $options)
278  ->add(SchemaRetriever::class, "retrieve", json_encode("blah"));
279  }
280 
281 }
Drupal\dkan_metastore\Exception\ExistingObjectException
Definition: ExistingObjectException.php:10
Drupal\Tests\dkan_metastore\Unit\ServiceTest\testPutModifyIdentifierException
testPutModifyIdentifierException()
Definition: ServiceTest.php:135
Drupal\dkan_metastore\Factory\Sae
Definition: Sae.php:13
Drupal\Tests\dkan_metastore\Unit\ServiceTest\testGetAll
testGetAll()
Definition: ServiceTest.php:46
Drupal\dkan_metastore\Service\create
static create(ContainerInterface $container)
Definition: Service.php:42
Drupal\dkan_metastore\Exception\UnmodifiedObjectException
Definition: UnmodifiedObjectException.php:10
Drupal\Tests\dkan_metastore\Unit\ServiceTest\testGetResources
testGetResources()
Definition: ServiceTest.php:72
Drupal\Tests\dkan_metastore\Unit\ServiceTest\testGetSchema
testGetSchema()
Definition: ServiceTest.php:36
Drupal\Tests\dkan_metastore\Unit\ServiceTest\testDelete
testDelete()
Definition: ServiceTest.php:233
Drupal\Tests\dkan_metastore\Unit\ServiceTest\testPost
testPost()
Definition: ServiceTest.php:93
Drupal\Tests\dkan_metastore\Unit\ServiceTest\testPutEquivalentDataObjectUnchangedException
testPutEquivalentDataObjectUnchangedException()
Definition: ServiceTest.php:183
Drupal\dkan_schema\SchemaRetriever
Definition: SchemaRetriever.php:11
Drupal\Tests\dkan_metastore\Unit
Drupal\Tests\dkan_metastore\Unit\ServiceTest\testPutResultingInNewData
testPutResultingInNewData()
Definition: ServiceTest.php:152
Drupal\Tests\dkan_metastore\Unit\ServiceTest\testPutObjectUnchangedException
testPutObjectUnchangedException()
Definition: ServiceTest.php:168
Drupal\Tests\dkan_metastore\Unit\ServiceTest\testGetSchemas
testGetSchemas()
Definition: ServiceTest.php:25
Drupal\Tests\dkan_metastore\Unit\ServiceTest\testPut
testPut()
Definition: ServiceTest.php:120
Drupal\dkan_metastore\Service
Definition: Service.php:20
Drupal\Tests\dkan_metastore\Unit\ServiceTest\getCommonMockChain
getCommonMockChain()
Definition: ServiceTest.php:270
Drupal\Tests\dkan_metastore\Unit\ServiceTest\testPatch
testPatch()
Definition: ServiceTest.php:204
Drupal\Tests\dkan_metastore\Unit\ServiceTest
Definition: ServiceTest.php:20
Drupal\Tests\dkan_metastore\Unit\ServiceTest\testGet
testGet()
Definition: ServiceTest.php:59
Drupal\Tests\dkan_metastore\Unit\ServiceTest\testPostAlreadyExisting
testPostAlreadyExisting()
Definition: ServiceTest.php:106
Drupal\Tests\dkan_metastore\Unit\ServiceTest\testGetCatalog
testGetCatalog()
Definition: ServiceTest.php:247
Drupal\dkan_metastore\Exception\MissingObjectException
Definition: MissingObjectException.php:10
Drupal\Tests\dkan_metastore\Unit\ServiceTest\testPatchObjectNotFoundException
testPatchObjectNotFoundException()
Definition: ServiceTest.php:218