3 use PHPUnit\Framework\TestCase;
4 use FileFetcher\FileFetcher;
5 use Procrastinator\Result;
6 use Dkan\Datastore\Resource;
7 use Drupal\Core\Entity\EntityRepository;
8 use Drupal\Core\File\FileSystem;
9 use Drupal\Core\Field\Plugin\DataType\FieldItem;
10 use Drupal\node\Entity\Node;
26 $object = (object) [];
27 $object->data = (object) [];
28 $object->data->downloadURL =
"http://google.com";
29 $object->data->mediaType =
"text/csv";
31 $meta = json_encode($object);
33 $entityRepository = (
new Chain($this))
34 ->add(EntityRepository::class,
"loadEntityByUuid", Node::class)
35 ->add(Node::class,
"id",
"1")
36 ->add(Node::class,
"get", FieldItem::class)
37 ->add(FieldItem::class,
"getString", $meta)
40 $fileSystem = (
new Chain($this))
41 ->add(FileSystem::class,
"chmod", NULL)
44 $jobStore = (
new Chain($this))
45 ->add(JobStore::class,
"store", NULL)
48 $jobStoreFactory = (
new Chain($this))
49 ->add(JobStoreFactory::class,
"getInstance", $jobStore)
52 $service =
new Service(
"123", $entityRepository, $fileSystem, $jobStoreFactory);
53 $resource = $service->get();
55 $this->assertTrue($resource instanceof Resource);
63 $object = (object) [];
64 $object->data = (object) [];
65 $object->data->downloadURL =
"http://google.com";
66 $object->data->mediaType =
"text/csv";
68 $meta = json_encode($object);
70 $entityRepository = (
new Chain($this))
71 ->add(EntityRepository::class,
"loadEntityByUuid", Node::class)
72 ->add(Node::class,
"id",
"1")
73 ->add(Node::class,
"get", FieldItem::class)
74 ->add(FieldItem::class,
"getString", $meta)
77 $fileSystem = (
new Chain($this))
78 ->add(FileSystem::class,
"chmod", NULL)
79 ->add(FileSystem::class,
"realpath",
"/tmp")
80 ->add(FileSystem::class,
"prepareDirectory", NULL)
83 $data = json_encode((
object) [
84 "destination" =>
"hello",
87 $jobStore = (
new Chain($this))
88 ->add(JobStore::class,
"retrieve",
"")
89 ->add(JobStore::class,
"store", NULL)
90 ->add(Result::class,
"getData", $data)
93 $jobStoreFactory = (
new Chain($this))
94 ->add(JobStoreFactory::class,
"getInstance", $jobStore)
97 $fileFetcher = (
new Chain($this))
98 ->add(FileFetcher::class,
"getResult", Result::class)
99 ->add(FileFetcher::class,
"run", Result::class)
102 $service = $this->getMockBuilder(Service::class)
103 ->setConstructorArgs([
"123", $entityRepository, $fileSystem, $jobStoreFactory])
104 ->setMethods([
"getFileFetcherInstance"])
107 $service->method(
"getFileFetcherInstance")->willReturn($fileFetcher);
109 $resource = $service->get(TRUE);
111 $this->assertTrue(!isset($resource));
119 $object = (object) [];
120 $object->data = (object) [];
121 $object->data->downloadURL =
"http://google.com";
122 $object->data->mediaType =
"text/csv";
124 $meta = json_encode($object);
126 $entityRepository = (
new Chain($this))
127 ->add(EntityRepository::class,
"loadEntityByUuid", Node::class)
128 ->add(Node::class,
"id",
"1")
129 ->add(Node::class,
"get", FieldItem::class)
130 ->add(FieldItem::class,
"getString", $meta)
133 $fileSystem = $this->getFileSystemMock();
135 $data = json_encode((
object) [
136 "destination" =>
"hello",
139 $fileFetcher = (
new Chain($this))
140 ->add(FileFetcher::class,
"getResult", Result::class)
141 ->add(FileFetcher::class,
"run", Result::class)
142 ->add(Result::class,
"getData", $data)
143 ->add(Result::class,
"getStatus", Result::DONE)
146 $jobStore = (
new Chain($this))
147 ->add(JobStore::class,
"retrieve", FileFetcher::class)
148 ->add(JobStore::class,
"store", NULL)
149 ->add(FileFetcher::class,
"getResult", $fileFetcher)
152 $jobStoreFactory = (
new Chain($this))
153 ->add(JobStoreFactory::class,
"getInstance", $jobStore)
156 $service = $this->getMockBuilder(Service::class)
157 ->setConstructorArgs([
"123", $entityRepository, $fileSystem, $jobStoreFactory])
158 ->setMethods([
"getFileFetcherInstance"])
161 $service->method(
"getFileFetcherInstance")->willReturn($fileFetcher);
162 $resource = $service->get(TRUE);
164 $this->assertTrue($resource instanceof Resource);
165 $this->assertTrue($service->getResult() instanceof Result);
173 $object = (object) [];
174 $object->data = (object) [];
175 $object->data->downloadURL =
"http://google.com";
176 $object->data->mediaType =
"text/csv";
178 $meta = json_encode($object);
180 $entityRepository = (
new Chain($this))
181 ->add(EntityRepository::class,
"loadEntityByUuid", Node::class)
182 ->add(Node::class,
"id",
"1")
183 ->add(Node::class,
"get", FieldItem::class)
184 ->add(FieldItem::class,
"getString", $meta)
187 $fileSystem = $this->getFileSystemMock();
189 $jobStore = (
new Chain($this))
190 ->add(JobStore::class,
"retrieve", NULL)
191 ->add(JobStore::class,
"store", NULL)
194 $jobStoreFactory = (
new Chain($this))
195 ->add(JobStoreFactory::class,
"getInstance", $jobStore)
198 $data = json_encode((
object) [
199 "destination" =>
"hello",
201 $fileFetcher = (
new Chain($this))
202 ->add(FileFetcher::class,
"getResult", Result::class)
203 ->add(FileFetcher::class,
"run", Result::class)
204 ->add(Result::class,
"getData", $data)
205 ->add(Result::class,
"getStatus", Result::DONE)
208 $builder = $this->getMockBuilder(Service::class);
209 $builder->setConstructorArgs([
"123", $entityRepository, $fileSystem, $jobStoreFactory])
210 ->setMethods([
"getFileFetcherInstance"]);
212 $service = $builder->getMock();
213 $service->method(
"getFileFetcherInstance")->willReturn($fileFetcher);
215 $resource = $service->get(TRUE);
217 $this->assertTrue($resource instanceof Resource);
223 private function getFileSystemMock() {
224 return (
new Chain($this))
225 ->add(FileSystem::class,
"realpath",
"/")
226 ->add(FileSystem::class,
"prepareDirectory", NULL)
227 ->add(FileSystem::class,
"chmod", NULL)