25 use Slim\Psr7\Factory\StreamFactory;
26 use Slim\Psr7\Headers;
27 use Slim\Psr7\Request;
30 require_once dirname(__DIR__, 4) .
"/lib/php/Plugin/FO_Plugin.php";
84 protected function setUp() : void
87 $container = M::mock(
'ContainerBuilder');
88 $this->dbHelper = M::mock(DbHelper::class);
89 $this->restHelper = M::mock(RestHelper::class);
90 $this->jobDao = M::mock(JobDao::class);
91 $this->showJobsDao = M::mock(ShowJobsDao::class);
93 $this->restHelper->shouldReceive(
'getDbHelper')->andReturn($this->dbHelper);
94 $this->restHelper->shouldReceive(
'getJobDao')->andReturn($this->jobDao);
95 $this->restHelper->shouldReceive(
'getShowJobDao')->andReturn($this->showJobsDao);
97 $container->shouldReceive(
'get')->withArgs(array(
98 'helper.restHelper'))->andReturn($this->restHelper);
100 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
101 $this->streamFactory =
new StreamFactory();
110 $this->addToAssertionCount(
111 \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
124 $response->getBody()->seek(0);
125 return json_decode($response->getBody()->getContents(),
true);
135 $userArray = array();
136 foreach ($userIds as $userId) {
139 } elseif ($userId > 2 && $userId <= 4) {
141 } elseif ($userId == 5) {
146 $user =
new User($userId,
"user$userId",
"User $userId",
147 "user$userId@example.com", $accessLevel, 2, 4,
"");
148 $userArray[] = $user->getArray();
160 $job =
new Job(11,
"job_name",
"01-01-2020", 4, 2, 2, 0,
"Completed");
161 $jobQueue =
new JobQueue(44,
'readmeoss',
'2020-01-01 20:41:49',
'2020-01-01 20:41:50',
162 'Completed', 0,
null, [], 0,
true,
false,
true,
163 [
'text' =>
'ReadMeOss',
'link' =>
'http://localhost/repo/api/v1/report/16']);
164 $this->jobDao->shouldReceive(
'getChlidJobStatus')->withArgs(array(11))
165 ->andReturn([
'44' => 0]);
166 $this->showJobsDao->shouldReceive(
'getEstimatedTime')
167 ->withArgs(array(11,
'', 0, 4))->andReturn(
"0");
168 $this->showJobsDao->shouldReceive(
'getDataForASingleJob')
169 ->withArgs(array(44))->andReturn([
"jq_endtext"=>
'Completed']);
171 $requestHeaders =
new Headers();
172 $body = $this->streamFactory->createStream();
173 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
174 $requestHeaders, [], [], $body);
178 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
179 $this->dbHelper->shouldReceive(
'getUserJobs')->withArgs(array(
null, 2, 0, 1))
180 ->andReturn([[$job], 1]);
181 $actualResponse = $this->jobController->getJobs($request, $response, []);
182 $expectedResponse = $job->getArray(ApiVersion::V1);
183 $this->assertEquals(200, $actualResponse->getStatusCode());
184 $this->assertEquals($expectedResponse,
186 $this->assertEquals(
'1',
187 $actualResponse->getHeaderLine(
'X-Total-Pages'));
197 $jobTwo =
new Job(12,
"job_two",
"01-01-2020", 5, 2, 2, 0,
"Completed");
198 $jobTwoQueue =
new JobQueue(45,
'readmeoss',
'2020-01-01 20:41:49',
'2020-01-01 20:41:50',
199 'Completed', 0,
null, [], 0,
true,
false,
true,
200 [
'text' =>
'ReadMeOss',
'link' =>
'http://localhost/repo/api/v1/report/16']);
201 $this->jobDao->shouldReceive(
'getChlidJobStatus')->withArgs(array(11))
202 ->andReturn([
'44' => 0]);
203 $this->jobDao->shouldReceive(
'getChlidJobStatus')->withArgs(array(12))
204 ->andReturn([
'45' => 0]);
205 $this->showJobsDao->shouldReceive(
'getEstimatedTime')
206 ->withArgs(array(M::anyOf(11, 12),
'', 0, M::anyOf(4, 5)))->andReturn(
"0");
207 $this->showJobsDao->shouldReceive(
'getDataForASingleJob')
208 ->withArgs([M::anyOf(44, 45)])->andReturn([
"jq_endtext"=>
'Completed']);
210 $requestHeaders =
new Headers();
211 $requestHeaders->setHeader(
'limit',
'1');
212 $requestHeaders->setHeader(
'page',
'2');
213 $body = $this->streamFactory->createStream();
214 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
215 $requestHeaders, [], [], $body);
219 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
220 $this->dbHelper->shouldReceive(
'getUserJobs')->withArgs(array(
null, 2, 1, 2))
221 ->andReturn([[$jobTwo], 2]);
222 $actualResponse = $this->jobController->getJobs($request, $response, []);
223 $expectedResponse = $jobTwo->getArray(ApiVersion::V1);
224 $this->assertEquals(200, $actualResponse->getStatusCode());
225 $this->assertEquals($expectedResponse,
227 $this->assertEquals(
'2',
228 $actualResponse->getHeaderLine(
'X-Total-Pages'));
238 $this->dbHelper->shouldReceive(
'doesIdExist')
239 ->withArgs([
"job",
"job_pk", 2])->andReturn(
false);
241 $requestHeaders =
new Headers();
242 $requestHeaders->setHeader(
'limit',
'1');
243 $requestHeaders->setHeader(
'page',
'2');
244 $body = $this->streamFactory->createStream();
245 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
246 $requestHeaders, [], [], $body);
249 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
250 $this->expectException(HttpNotFoundException::class);
252 $this->jobController->getJobs($request, $response, [
"id" => 2]);
262 $job =
new Job(12,
"job_two",
"01-01-2020", 5, 2, 2, 0,
"Completed");
263 $jobTwoQueue =
new JobQueue(45,
'readmeoss',
'2020-01-01 20:41:49',
'2020-01-01 20:41:50',
264 'Completed', 0,
null, [], 0,
true,
false,
true,
265 [
'text' =>
'ReadMeOss',
'link' =>
'http://localhost/repo/api/v1/report/16']);
266 $this->dbHelper->shouldReceive(
'doesIdExist')
267 ->withArgs([
"job",
"job_pk", 12])->andReturn(
true);
268 $this->dbHelper->shouldReceive(
'getJobs')->withArgs(array(12, 0, 1))
269 ->andReturn([[$job], 1]);
270 $this->jobDao->shouldReceive(
'getChlidJobStatus')->withArgs(array(12))
271 ->andReturn([
'45' => 0]);
272 $this->showJobsDao->shouldReceive(
'getEstimatedTime')
273 ->withArgs(array(12,
'', 0, 5))->andReturn(
"0");
274 $this->showJobsDao->shouldReceive(
'getDataForASingleJob')
275 ->withArgs([45])->andReturn([
"jq_endtext"=>
'Completed']);
277 $requestHeaders =
new Headers();
278 $body = $this->streamFactory->createStream();
279 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
280 $requestHeaders, [], [], $body);
284 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
285 $actualResponse = $this->jobController->getJobs($request, $response, [
287 $expectedResponse = $job->getArray(ApiVersion::V1);
288 $this->assertEquals(200, $actualResponse->getStatusCode());
289 $this->assertEquals($expectedResponse,
291 $this->assertEquals(
'1',
292 $actualResponse->getHeaderLine(
'X-Total-Pages'));
302 $job =
new Job(12,
"job_two",
"01-01-2020", 5, 2, 2, 0,
"Completed");
303 $jobTwoQueue =
new JobQueue(45,
'readmeoss',
'2020-01-01 20:41:49',
'2020-01-01 20:41:50',
304 'Completed', 0,
null, [], 0,
true,
false,
true,
305 [
'text' =>
'ReadMeOss',
'link' =>
'http://localhost/repo/api/v1/report/16']);
306 $this->dbHelper->shouldReceive(
'doesIdExist')
307 ->withArgs([
"upload",
"upload_pk", 5])->andReturn(
true);
308 $this->dbHelper->shouldReceive(
'doesIdExist')
309 ->withArgs([
'job',
'job_pk', 12])->andReturn(
true);
310 $this->dbHelper->shouldReceive(
'getJobs')->withArgs(array(
null, 0, 1, 5))
311 ->andReturn([[$job], 1]);
312 $this->jobDao->shouldReceive(
'getChlidJobStatus')->withArgs(array(12))
313 ->andReturn([
'45' => 0]);
314 $this->showJobsDao->shouldReceive(
'getEstimatedTime')
315 ->withArgs(array(12,
'', 0, 5))->andReturn(
"0");
316 $this->showJobsDao->shouldReceive(
'getDataForASingleJob')
317 ->withArgs([45])->andReturn([
"jq_endtext"=>
'Completed']);
319 $requestHeaders =
new Headers();
320 $body = $this->streamFactory->createStream();
321 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
322 $requestHeaders, [], [], $body);
327 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
328 $actualResponse = $this->jobController->getJobs($request, $response, []);
329 $expectedResponse = $job->getArray(ApiVersion::V1);
330 $this->assertEquals(200, $actualResponse->getStatusCode());
331 $this->assertEquals($expectedResponse,
333 $this->assertEquals(
'1',
334 $actualResponse->getHeaderLine(
'X-Total-Pages'));
348 $completedUpload = 3;
349 $this->showJobsDao->shouldReceive(
'getEstimatedTime')
350 ->withArgs([$jobId,
'', 0, $uploadId])
351 ->andReturn(
"3:10:23");
352 $this->showJobsDao->shouldReceive(
'getEstimatedTime')
353 ->withArgs([$completedJob,
'', 0, $completedUpload])
355 $reflection = new \ReflectionClass(get_class($this->jobController));
356 $method = $reflection->getMethod(
'getUploadEtaInSeconds');
357 $method->setAccessible(
true);
359 $result = $method->invokeArgs($this->jobController, [$jobId, $uploadId]);
360 $this->assertEquals((3 * 3600) + (10 * 60) + 23, $result);
362 $result = $method->invokeArgs($this->jobController,
363 [$completedJob, $completedUpload]);
364 $this->assertEquals(0, $result);
377 $jobCompleted = [1, 2];
380 $this->showJobsDao->shouldReceive(
'getDataForASingleJob')
381 ->withArgs([M::anyof(1, 2, 5)])
382 ->andReturn([
"jq_endtext" =>
"Completed"]);
383 $this->showJobsDao->shouldReceive(
'getDataForASingleJob')
384 ->withArgs([3])->andReturn([
"jq_endtext" =>
"Started"]);
385 $this->showJobsDao->shouldReceive(
'getDataForASingleJob')
386 ->withArgs([4])->andReturn([
"jq_endtext" =>
"Processing",
387 "jq_endtime" =>
""]);
388 $this->showJobsDao->shouldReceive(
'getDataForASingleJob')
389 ->withArgs([6])->andReturn([
"jq_endtext" =>
"Failed",
390 "jq_endtime" =>
"01-01-2020 00:00:00"]);
392 $reflection = new \ReflectionClass(get_class($this->jobController));
393 $method = $reflection->getMethod(
'getJobStatus');
394 $method->setAccessible(
true);
396 $result = $method->invokeArgs($this->jobController, [$jobCompleted]);
397 $this->assertEquals(
"Completed", $result);
399 $result = $method->invokeArgs($this->jobController, [$jobQueued]);
400 $this->assertEquals(
"Processing", $result);
402 $result = $method->invokeArgs($this->jobController, [$jobFailed]);
403 $this->assertEquals(
"Failed", $result);
Controller for Job model.
Override Slim response for withJson function.
Model class to hold JobQueue info.
Model to hold user information.
getResponseJson($response)
testGetUploadEtaInSeconds()
tearDown()
Remove test objects.
setUp()
Setup test objects.
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.