Open Data Catalog v2.0.0
DatasetTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Drupal\Core\TypedData\TypedDataManagerInterface;
5 use MockChain\Chain;
7 use PHPUnit\Framework\TestCase;
8 use MockChain\Options;
9 use Drupal\Core\DependencyInjection\Container;
10 
14 class DatasetTest extends TestCase {
15 
19  public function test() {
20  $schema = '
21  {
22  "$id": "https://example.com/person.schema.json",
23  "$schema": "http://json-schema.org/draft-07/schema#",
24  "title": "Person",
25  "type": "object",
26  "properties": {
27  "firstName": {
28  "type": "string",
29  "description": "First name."
30  },
31  "lastName": {
32  "type": "string",
33  "description": "Last name."
34  },
35  "occupations" : {
36  "type": "array"
37  },
38  "age": {
39  "description": "Age in years which must be equal to or greater than zero.",
40  "type": "integer",
41  "minimum": 0
42  },
43  "address": {
44  "type": "object",
45  "properties": {
46  "city": {
47  "coords": "array",
48  "items": {
49  "type": "string"
50  }
51  },
52  "state": {
53  "type": "string"
54  }
55  }
56  }
57  }
58  }
59  ';
60 
61  $options = (new Options())
62  ->add('dkan_schema.schema_retriever', SchemaRetriever::class)
63  ->add('typed_data_manager', TypedDataManagerInterface::class)
64  ->index(0);
65 
66  $container = (new Chain($this))
67  ->add(Container::class, "get", $options)
68  ->add(SchemaRetriever::class, 'retrieve', $schema)
69  ->getMock();
70 
71  \Drupal::setContainer($container);
72 
73  $thing = (object) ['firstName' => 'hello', 'lastName' => 'goodbye', 'age' => 5000, 'occupations' => ['teacher']];
74  $json = json_encode($thing);
75  $dataset = new Dataset($json);
76  $this->assertEquals($json, json_encode($dataset->getValue()));
77 
78  $properties = $dataset->getProperties();
79  $this->assertEquals(6, count($properties));
80  }
81 
82 }
DatasetTest
Definition: DatasetTest.php:14
DatasetTest\test
test()
Definition: DatasetTest.php:19
Drupal\dkan_search\ComplexData\Dataset
Definition: Dataset.php:14
Drupal\dkan_schema\SchemaRetriever
Definition: SchemaRetriever.php:11