23 use Slim\Psr7\Factory\StreamFactory;
24 use Slim\Psr7\Headers;
25 use Slim\Psr7\Request;
28 require_once dirname(__DIR__, 4) .
"/lib/php/Plugin/FO_Plugin.php";
82 protected function setUp() : void
85 $container = M::mock(
'ContainerBuilder');
86 $this->dbHelper = M::mock(DbHelper::class);
87 $this->restHelper = M::mock(RestHelper::class);
88 $this->jobDao = M::mock(JobDao::class);
89 $this->showJobsDao = M::mock(ShowJobsDao::class);
91 $this->restHelper->shouldReceive(
'getDbHelper')->andReturn($this->dbHelper);
92 $this->restHelper->shouldReceive(
'getJobDao')->andReturn($this->jobDao);
93 $this->restHelper->shouldReceive(
'getShowJobDao')->andReturn($this->showJobsDao);
95 $container->shouldReceive(
'get')->withArgs(array(
96 'helper.restHelper'))->andReturn($this->restHelper);
98 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
99 $this->streamFactory =
new StreamFactory();
108 $this->addToAssertionCount(
109 \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
122 $response->getBody()->seek(0);
123 return json_decode($response->getBody()->getContents(),
true);
133 $userArray = array();
134 foreach ($userIds as $userId) {
137 } elseif ($userId > 2 && $userId <= 4) {
139 } elseif ($userId == 5) {
144 $user =
new User($userId,
"user$userId",
"User $userId",
145 "user$userId@example.com", $accessLevel, 2, 4,
"");
146 $userArray[] = $user->getArray();
158 $job =
new Job(11,
"job_name",
"01-01-2020", 4, 2, 2, 0,
"Completed");
159 $this->jobDao->shouldReceive(
'getAllJobStatus')->withArgs(array(4, 2, 2))
160 ->andReturn([
'11' => 0]);
161 $this->showJobsDao->shouldReceive(
'getEstimatedTime')
162 ->withArgs(array(11,
'', 0, 4))->andReturn(
"0");
163 $this->showJobsDao->shouldReceive(
'getDataForASingleJob')
164 ->withArgs(array(11))->andReturn([
"jq_endtext"=>
'Completed']);
166 $requestHeaders =
new Headers();
167 $body = $this->streamFactory->createStream();
168 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
169 $requestHeaders, [], [], $body);
173 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
174 $this->dbHelper->shouldReceive(
'getUserJobs')->withArgs(array(
null, 2, 0, 1))
175 ->andReturn([[$job], 1]);
176 $actualResponse = $this->jobController->getJobs($request, $response, []);
177 $expectedResponse = $job->getArray();
178 $this->assertEquals(200, $actualResponse->getStatusCode());
179 $this->assertEquals($expectedResponse,
181 $this->assertEquals(
'1',
182 $actualResponse->getHeaderLine(
'X-Total-Pages'));
192 $jobTwo =
new Job(12,
"job_two",
"01-01-2020", 5, 2, 2, 0,
"Completed");
193 $this->jobDao->shouldReceive(
'getAllJobStatus')->withArgs(array(4, 2, 2))
194 ->andReturn([
'11' => 0]);
195 $this->jobDao->shouldReceive(
'getAllJobStatus')->withArgs(array(5, 2, 2))
196 ->andReturn([
'12' => 0]);
197 $this->showJobsDao->shouldReceive(
'getEstimatedTime')
198 ->withArgs(array(M::anyOf(11, 12),
'', 0, M::anyOf(4, 5)))->andReturn(
"0");
199 $this->showJobsDao->shouldReceive(
'getDataForASingleJob')
200 ->withArgs([M::anyOf(11, 12)])->andReturn([
"jq_endtext"=>
'Completed']);
202 $requestHeaders =
new Headers();
203 $requestHeaders->setHeader(
'limit',
'1');
204 $requestHeaders->setHeader(
'page',
'2');
205 $body = $this->streamFactory->createStream();
206 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
207 $requestHeaders, [], [], $body);
211 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
212 $this->dbHelper->shouldReceive(
'getUserJobs')->withArgs(array(
null, 2, 1, 2))
213 ->andReturn([[$jobTwo], 2]);
214 $actualResponse = $this->jobController->getJobs($request, $response, []);
215 $expectedResponse = $jobTwo->getArray();
216 $this->assertEquals(200, $actualResponse->getStatusCode());
217 $this->assertEquals($expectedResponse,
219 $this->assertEquals(
'2',
220 $actualResponse->getHeaderLine(
'X-Total-Pages'));
230 $this->dbHelper->shouldReceive(
'doesIdExist')
231 ->withArgs([
"job",
"job_pk", 2])->andReturn(
false);
233 $requestHeaders =
new Headers();
234 $requestHeaders->setHeader(
'limit',
'1');
235 $requestHeaders->setHeader(
'page',
'2');
236 $body = $this->streamFactory->createStream();
237 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
238 $requestHeaders, [], [], $body);
241 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
242 $this->expectException(HttpNotFoundException::class);
244 $this->jobController->getJobs($request, $response, [
"id" => 2]);
254 $job =
new Job(12,
"job_two",
"01-01-2020", 5, 2, 2, 0,
"Completed");
255 $this->dbHelper->shouldReceive(
'doesIdExist')
256 ->withArgs([
"job",
"job_pk", 12])->andReturn(
true);
257 $this->dbHelper->shouldReceive(
'getJobs')->withArgs(array(12, 0, 1))
258 ->andReturn([[$job], 1]);
259 $this->jobDao->shouldReceive(
'getAllJobStatus')->withArgs(array(5, 2, 2))
260 ->andReturn([
'12' => 0]);
261 $this->showJobsDao->shouldReceive(
'getEstimatedTime')
262 ->withArgs(array(12,
'', 0, 5))->andReturn(
"0");
263 $this->showJobsDao->shouldReceive(
'getDataForASingleJob')
264 ->withArgs([12])->andReturn([
"jq_endtext"=>
'Completed']);
266 $requestHeaders =
new Headers();
267 $body = $this->streamFactory->createStream();
268 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
269 $requestHeaders, [], [], $body);
273 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
274 $actualResponse = $this->jobController->getJobs($request, $response, [
276 $expectedResponse = $job->getArray();
277 $this->assertEquals(200, $actualResponse->getStatusCode());
278 $this->assertEquals($expectedResponse,
280 $this->assertEquals(
'1',
281 $actualResponse->getHeaderLine(
'X-Total-Pages'));
291 $job =
new Job(12,
"job_two",
"01-01-2020", 5, 2, 2, 0,
"Completed");
292 $this->dbHelper->shouldReceive(
'doesIdExist')
293 ->withArgs([
"upload",
"upload_pk", 5])->andReturn(
true);
294 $this->dbHelper->shouldReceive(
'doesIdExist')
295 ->withArgs([
'job',
'job_pk', 12])->andReturn(
true);
296 $this->dbHelper->shouldReceive(
'getJobs')->withArgs(array(
null, 0, 1, 5))
297 ->andReturn([[$job], 1]);
298 $this->jobDao->shouldReceive(
'getAllJobStatus')->withArgs(array(5, 2, 2))
299 ->andReturn([
'12' => 0]);
300 $this->showJobsDao->shouldReceive(
'getEstimatedTime')
301 ->withArgs(array(12,
'', 0, 5))->andReturn(
"0");
302 $this->showJobsDao->shouldReceive(
'getDataForASingleJob')
303 ->withArgs([12])->andReturn([
"jq_endtext"=>
'Completed']);
305 $requestHeaders =
new Headers();
306 $body = $this->streamFactory->createStream();
307 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
308 $requestHeaders, [], [], $body);
313 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
314 $actualResponse = $this->jobController->getJobs($request, $response, []);
315 $expectedResponse = $job->getArray();
316 $this->assertEquals(200, $actualResponse->getStatusCode());
317 $this->assertEquals($expectedResponse,
319 $this->assertEquals(
'1',
320 $actualResponse->getHeaderLine(
'X-Total-Pages'));
334 $completedUpload = 3;
335 $this->showJobsDao->shouldReceive(
'getEstimatedTime')
336 ->withArgs([$jobId,
'', 0, $uploadId])
337 ->andReturn(
"3:10:23");
338 $this->showJobsDao->shouldReceive(
'getEstimatedTime')
339 ->withArgs([$completedJob,
'', 0, $completedUpload])
341 $reflection = new \ReflectionClass(get_class($this->jobController));
342 $method = $reflection->getMethod(
'getUploadEtaInSeconds');
343 $method->setAccessible(
true);
345 $result = $method->invokeArgs($this->jobController, [$jobId, $uploadId]);
346 $this->assertEquals((3 * 3600) + (10 * 60) + 23, $result);
348 $result = $method->invokeArgs($this->jobController,
349 [$completedJob, $completedUpload]);
350 $this->assertEquals(0, $result);
363 $jobCompleted = [1, 2];
366 $this->showJobsDao->shouldReceive(
'getDataForASingleJob')
367 ->withArgs([M::anyof(1, 2, 5)])
368 ->andReturn([
"jq_endtext" =>
"Completed"]);
369 $this->showJobsDao->shouldReceive(
'getDataForASingleJob')
370 ->withArgs([3])->andReturn([
"jq_endtext" =>
"Started"]);
371 $this->showJobsDao->shouldReceive(
'getDataForASingleJob')
372 ->withArgs([4])->andReturn([
"jq_endtext" =>
"Processing",
373 "jq_endtime" =>
""]);
374 $this->showJobsDao->shouldReceive(
'getDataForASingleJob')
375 ->withArgs([6])->andReturn([
"jq_endtext" =>
"Failed",
376 "jq_endtime" =>
"01-01-2020 00:00:00"]);
378 $reflection = new \ReflectionClass(get_class($this->jobController));
379 $method = $reflection->getMethod(
'getJobStatus');
380 $method->setAccessible(
true);
382 $result = $method->invokeArgs($this->jobController, [$jobCompleted]);
383 $this->assertEquals(
"Completed", $result);
385 $result = $method->invokeArgs($this->jobController, [$jobQueued]);
386 $this->assertEquals(
"Processing", $result);
388 $result = $method->invokeArgs($this->jobController, [$jobFailed]);
389 $this->assertEquals(
"Failed", $result);
Controller for Job model.
Override Slim response for withJson function.
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.