Open Data Catalog v2.0.0
Container.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Database\Query\SelectInterface;
7 use Drupal\Core\Database\Schema;
8 use Drupal\Core\Database\StatementInterface;
9 use Drupal\Core\Entity\EntityRepository;
10 use Drupal\Core\Entity\EntityStorageException;
11 use Drupal\Core\Field\FieldItemListInterface;
12 use Drupal\Core\File\FileSystem;
13 use Drupal\Core\Queue\QueueFactory;
14 use Drupal\Core\TypedData\TypedDataInterface;
15 use Drupal\dkan_datastore\Service\Service;
16 use Drupal\node\NodeInterface;
17 use FileFetcher\Processor\Local;
18 use PHPUnit\Framework\TestCase;
19 use Symfony\Component\DependencyInjection\ContainerInterface;
20 
24 class Container {
25 
26  private $testCase;
27 
28  private $jobStoreData;
29  private $tableName;
30  private $noNode = FALSE;
31 
35  public function __construct(TestCase $testCase) {
36  $this->testCase = $testCase;
37 
38  $fileFetcherContent = file_get_contents(__DIR__ . '/../../../data/filefetcher.json');
39  $fileFetcherObject = json_decode($fileFetcherContent);
40  $data = json_decode($fileFetcherObject->result->data);
41  $data->processor = Local::class;
42  $fileFetcherObject->result->data = json_encode($data);
43 
44  $this->jobStoreData = (object) [
45  'jid' => 1,
46  'job_data' => json_encode($fileFetcherObject),
47  ];
48  }
49 
53  public function setNoNode() {
54  $this->noNode = TRUE;
55  }
56 
60  public function get() {
61  $container = $this->testCase->getMockBuilder(ContainerInterface::class)
62  ->setMethods(['get'])
63  ->disableOriginalConstructor()
64  ->getMockForAbstractClass();
65  $container->method('get')
66  ->with($this->testCase->logicalOr($this->testCase->equalTo('dkan_datastore.service')))
67  ->will($this->testCase->returnCallback(
68  function ($serviceName) {
69  switch ($serviceName) {
70  case 'dkan_datastore.service':
71  $mockEntityRepository = $this->getEntityRepositoryMock();
72  $mockConnection = $this->getConnectionMock();
73  $mockQueueFactory = $this->getQueueFactoryMock();
74  $mockFileSystem = $this->getFileSystemMock();
75 
76  return new Service(
77  $mockEntityRepository,
78  $mockConnection,
79  $mockQueueFactory,
80  $mockFileSystem
81  );
82  }
83  }
84  ));
85  return $container;
86  }
87 
91  private function getQueueFactoryMock() {
92  $mock = $this->testCase->getMockBuilder(QueueFactory::class)
93  ->disableOriginalConstructor()
94  ->setMethods(['get'])
95  ->getMockForAbstractClass();
96 
97  $mock->method('get')->willReturn($this->getQueueMock());
98 
99  return $mock;
100 
101  }
102 
106  private function getQueueMock() {
107  $mock = $this->testCase->getMockBuilder("\Drupal\Core\Queue\QueueInterface")
108  ->disableOriginalConstructor()
109  ->setMethods(['createItem'])
110  ->getMockForAbstractClass();
111 
112  $mock->method('createItem')->willReturn("1");
113 
114  return $mock;
115  }
116 
120  private function getFileSystemMock() {
121  $mock = $this->testCase->getMockBuilder(FileSystem::class)
122  ->disableOriginalConstructor()
123  ->setMethods(['prepareDir'])
124  ->getMockForAbstractClass();
125 
126  $mock->method('prepareDir')->willReturn(TRUE);
127 
128  return $mock;
129  }
130 
134  private function getConnectionMock() {
135  $mock = $this->testCase->getMockBuilder(Connection::class)
136  ->disableOriginalConstructor()
137  ->setMethods(['schema', 'query', 'select', 'insert', 'delete', 'update'])
138  ->getMockForAbstractClass();
139 
140  $mock->method('schema')->willReturn($this->getSchemaMock());
141  $mock->method('query')->willReturn($this->getStatementMock());
142  $mock->method('select')->willReturnCallback(function ($tableName) {
143  $this->tableName = $tableName;
144  return $this->getQueryMock();
145  });
146  $mock->method('insert')->willReturn($this->getQueryMock());
147  $mock->method('delete')->willReturn($this->getQueryMock());
148  $mock->method('update')->willReturn($this->getQueryMock());
149 
150  return $mock;
151  }
152 
156  private function getQueryMock() {
157  $mock = $this->testCase->getMockBuilder(SelectInterface::class)
158  ->disableOriginalConstructor()
159  ->setMethods(['fields', 'countQuery', 'condition', 'values', 'execute'])
160  ->getMockForAbstractClass();
161 
162  $mock->method('fields')->willReturn($mock);
163  $mock->method('countQuery')->willReturn($mock);
164  $mock->method('condition')->willReturn($mock);
165  $mock->method('values')->willReturn($mock);
166  $mock->method('execute')->willReturn($this->getStatementMock());
167 
168  return $mock;
169  }
170 
174  private function getStatementMock() {
175  $mock = $this->testCase->getMockBuilder(StatementInterface::class)
176  ->disableOriginalConstructor()
177  ->setMethods(['fetchAll', 'fetchField'])
178  ->getMockForAbstractClass();
179  $mock->method('fetch')->willReturnCallback(function () {
180  if ($this->tableName == 'jobstore_filefetcher_filefetcher') {
181  return $this->jobStoreData;
182  }
183  return [];
184  });
185  $mock->method('fetchAll')
186  ->willReturn([
187  (object) ['Field' => 'field_1'],
188  (object) ['Field' => 'field_2'],
189  ]
190  );
191  $mock->method('fetchField')->willReturn(2);
192 
193  return $mock;
194  }
195 
199  private function getSchemaMock() {
200  $mock = $this->testCase->getMockBuilder(Schema::class)
201  ->disableOriginalConstructor()
202  ->setMethods(['tableExists'])
203  ->getMockForAbstractClass();
204 
205  $mock->method('tableExists')->willReturn(TRUE);
206 
207  return $mock;
208  }
209 
213  private function getEntityRepositoryMock() {
214  $mock = $this->testCase->getMockBuilder(EntityRepository::class)
215  ->setMethods(['loadEntityByUuid'])
216  ->disableOriginalConstructor()
217  ->getMock();
218 
219  $node = $this->mockNodeInterface();
220 
221  if ($this->noNode) {
222  $mock->method('loadEntityByUuid')->willThrowException(new EntityStorageException("You Failed"));
223  }
224  else {
225  $mock->method('loadEntityByUuid')
226  ->willReturn($node);
227  }
228 
229  return $mock;
230  }
231 
235  private function mockNodeInterface() {
236  $mock = $this->testCase->getMockBuilder(NodeInterface::class)
237  ->setMethods(['get'])
238  ->disableOriginalConstructor()
239  ->getMockForAbstractClass();
240 
241  $node = $this->mockFieldItemListInterface();
242 
243  $mock->method('get')
244  ->willReturn($node);
245 
246  return $mock;
247  }
248 
252  private function mockFieldItemListInterface() {
253  $mock = $this->testCase->getMockBuilder(FieldItemListInterface::class)
254  ->setMethods(['get'])
255  ->disableOriginalConstructor()
256  ->getMockForAbstractClass();
257 
258  $list = $this->mockTypedDataInterface();
259 
260  $mock->method('get')
261  ->willReturn($list);
262 
263  return $mock;
264  }
265 
269  private function mockTypedDataInterface() {
270  $mock = $this->testCase->getMockBuilder(TypedDataInterface::class)
271  ->setMethods(['getValue'])
272  ->disableOriginalConstructor()
273  ->getMockForAbstractClass();
274 
275  $data = [
276  'value' => json_encode([
277  'data' => [
278  'downloadURL' => __DIR__ . '/../../../data/countries.csv',
279  'mediaType' => 'text/csv',
280  ],
281  ]),
282  ];
283 
284  $mock->method('getValue')
285  ->willReturn($data);
286 
287  return $mock;
288  }
289 
290 }
Drupal\Tests\dkan_datastore\Unit\Mock\Container
Definition: Container.php:24
Drupal\Tests\dkan_datastore\Unit\Mock\Container\__construct
__construct(TestCase $testCase)
Definition: Container.php:35
Drupal\Tests\dkan_datastore\Unit\Mock
Definition: Container.php:3
Drupal\Tests\dkan_datastore\Unit\Mock\Container\setNoNode
setNoNode()
Definition: Container.php:53