Open Data Catalog v2.0.0
DataTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Entity\Query\QueryInterface;
7 use Drupal\Core\Field\FieldItemInterface;
8 use Drupal\Core\Field\FieldItemListInterface;
10 use Drupal\node\NodeInterface;
11 use Drupal\node\NodeStorageInterface;
12 use PHPUnit\Framework\TestCase;
13 
20 class DataTest extends TestCase {
21  private $node;
22 
26  public function testRetrieveAll() {
27  $store = new Data($this->getEntityTypeManagerMock());
28  $store->setSchema('dataset');
29  $all = $store->retrieveAll();
30 
31  $object = '{"name":"blah"}';
32 
33  $this->assertEquals([$object, $object], $all);
34  }
35 
39  public function testRetrieve() {
40  $this->node = $this->getNodeMock();
41  $store = new Data($this->getEntityTypeManagerMock());
42  $store->setSchema('dataset');
43  $obj = $store->retrieve(1);
44 
45  $object = '{"name":"blah"}';
46 
47  $this->assertEquals($object, $obj);
48  }
49 
53  public function testStoreExisting() {
54  $this->node = $this->getNodeMock();
55  $object = '{"name":"blah"}';
56 
57  $store = new Data($this->getEntityTypeManagerMock());
58  $store->setSchema('dataset');
59  $id = $store->store($object, 1);
60 
61  $this->assertEquals(1, $id);
62  }
63 
67  public function testStoreNew() {
68  $this->node = NULL;
69  $object = '{"name":"blah"}';
70 
71  $store = new Data($this->getEntityTypeManagerMock());
72  $store->setSchema('dataset');
73  $id = $store->store($object, 1);
74 
75  $this->assertEquals(1, $id);
76  }
77 
81  public function testRemove() {
82  $this->node = $this->getNodeMock();
83  $store = new Data($this->getEntityTypeManagerMock());
84  $store->setSchema('dataset');
85  $removed = $store->remove(1);
86 
87  $this->assertEquals(NULL, $removed);
88  }
89 
93  public function testRetrieveAllException() {
94  $this->expectExceptionMessage("Data schemaId not set in retrieveAll()");
95  $store = new Data($this->getEntityTypeManagerMock());
96  $store->retrieveAll();
97  }
98 
102  public function testRetrieveException() {
103  $this->expectExceptionMessage("Data schemaId not set in retrieve()");
104  $store = new Data($this->getEntityTypeManagerMock());
105  $store->retrieve(1);
106  }
107 
111  public function testStoreException() {
112  $this->expectExceptionMessage("Data schemaId not set in store()");
113  $object = '{"name":"blah"}';
114  $store = new Data($this->getEntityTypeManagerMock());
115  $store->store($object, 1);
116  }
117 
121  private function getEntityTypeManagerMock() {
122  $entityTypeManager = $this->getMockBuilder(EntityTypeManagerInterface::class)
123  ->disableOriginalConstructor()
124  ->setMethods(['getStorage'])
125  ->getMockForAbstractClass();
126 
127  $entityTypeManager->method('getStorage')
128  ->willReturn($this->getNodeStorageMock());
129 
130  return $entityTypeManager;
131  }
132 
136  private function getNodeStorageMock() {
137  $nodeStorage = $this->getMockBuilder(NodeStorageInterface::class)
138  ->disableOriginalConstructor()
139  ->setMethods(['getQuery', 'load', 'loadByProperties', 'create'])
140  ->getMockForAbstractClass();
141 
142  $nodeStorage->method('getQuery')
143  ->willReturn($this->getQueryMock());
144 
145  $nodeStorage->method('load')
146  ->willReturn($this->getNodeMock());
147 
148  $nodeStorage->method('loadByProperties')
149  ->willReturn([$this->node]);
150 
151  $nodeStorage->method('create')
152  ->willReturn($this->getNodeMock());
153 
154  return $nodeStorage;
155  }
156 
160  private function getNodeMock() {
161  $node = $this->getMockBuilder(NodeInterface::class)
162  ->disableOriginalConstructor()
163  ->setMethods(['get', 'uuid', 'save'])
164  ->getMockForAbstractClass();
165 
166  $node->method('get')
167  ->willReturn($this->getFieldItemListMock());
168 
169  $node->method('uuid')
170  ->willReturn(1);
171 
172  $node->method('save')
173  ->willReturn(1);
174 
175  return $node;
176  }
177 
181  private function getFieldItemListMock() {
182  $list = $this->getMockBuilder(FieldItemListInterface::class)
183  ->disableOriginalConstructor()
184  ->setMethods(['get'])
185  ->getMockForAbstractClass();
186 
187  $list->method('get')->willReturn($this->getFieldItemMock());
188 
189  return $list;
190  }
191 
195  private function getFieldItemMock() {
196  $item = $this->getMockBuilder(FieldItemInterface::class)
197  ->disableOriginalConstructor()
198  ->setMethods(['getValue'])
199  ->getMockForAbstractClass();
200 
201  $item->method('getValue')->willReturn(['value' => json_encode(["name" => "blah"])]);
202 
203  return $item;
204  }
205 
209  private function getQueryMock() {
210  $query = $this->getMockBuilder(QueryInterface::class)
211  ->disableOriginalConstructor()
212  ->setMethods(['condition', 'execute'])
213  ->getMockForAbstractClass();
214 
215  $query->method('condition')
216  ->willReturn($query);
217 
218  $query->method('execute')->willReturn([1, 2]);
219 
220  return $query;
221  }
222 
223 }
Drupal\dkan_data\Storage\Data
Definition: Data.php:16
Drupal\Tests\dkan_api\Unit\Storage\DataTest\testRemove
testRemove()
Definition: DataTest.php:81
Drupal\Tests\dkan_api\Unit\Storage\DataTest\testStoreException
testStoreException()
Definition: DataTest.php:111
Drupal\Tests\dkan_api\Unit\Storage\DataTest\testStoreExisting
testStoreExisting()
Definition: DataTest.php:53
Drupal\Tests\dkan_api\Unit\Storage\DataTest\testRetrieveAllException
testRetrieveAllException()
Definition: DataTest.php:93
Drupal\Tests\dkan_api\Unit\Storage\DataTest\testStoreNew
testStoreNew()
Definition: DataTest.php:67
Drupal\Tests\dkan_api\Unit\Storage
Definition: DataTest.php:3
Drupal\Tests\dkan_api\Unit\Storage\DataTest\testRetrieve
testRetrieve()
Definition: DataTest.php:39
Drupal\Tests\dkan_api\Unit\Storage\DataTest
Definition: DataTest.php:20
Drupal\Tests\dkan_api\Unit\Storage\DataTest\testRetrieveAll
testRetrieveAll()
Definition: DataTest.php:26
Drupal\Tests\dkan_api\Unit\Storage\DataTest\testRetrieveException
testRetrieveException()
Definition: DataTest.php:102