FOSSology  4.5.1
Open Source License Compliance by Open Source Software
JobTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  SPDX-FileCopyrightText: © 2020 Siemens AG
5  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
6 
7  SPDX-License-Identifier: GPL-2.0-only
8 */
9 
16 
21 use Mockery as M;
22 use PHPUnit\Framework\TestCase;
23 
28 class JobTest extends TestCase
29 {
31 
37  public function testConstructor()
38  {
39  $job = new Job(
40  1,
41  "Data Processing Job",
42  "2025-02-18 10:00:00",
43  123,
44  456,
45  789,
46  3600,
47  "Queued",
48  []
49  );
50  $this->assertInstanceOf(Job::class, $job);
51  }
52 
57  public function testDataFormatV1()
58  {
59  $this->testDataFormat(ApiVersion::V1);
60  }
61 
66  public function testDataFormatV2()
67  {
68  $this->testDataFormat(ApiVersion::V2);
69  }
70 
74  private function testDataFormat($version)
75  {
76  $jobQueue = new JobQueue(44, 'readmeoss', '2024-07-03 20:41:49', '2024-07-03 20:41:50',
77  'Completed', 0, null, [], 0, true, false, true,
78  ['text' => 'ReadMeOss', 'link' => 'http://localhost/repo/api/v1/report/16']
79  );
80 
81  if ($version == ApiVersion::V2){
82  $expectedStatus = [
83  'id' => 22,
84  'name' => 'ojo',
85  'queueDate' => '01-01-2020',
86  'uploadId' => 4,
87  'userName' => "fossy",
88  'groupName' => "fossy",
89  'eta' => 3,
90  'status' => 'Processing',
91  'jobQueue' => $jobQueue->getArray()
92  ];
93 
94  global $container;
95  $userDao = M::mock(UserDao::class);
96  $container = M::mock('ContainerBuilder');
97  $container->shouldReceive('get')->with('dao.user')->andReturn($userDao);
98  $userDao->shouldReceive('getUserName')->with(2)->andReturn('fossy');
99  $userDao->shouldReceive('getGroupNameById')->with(2)->andReturn('fossy');
100  } else {
101  $expectedStatus = [
102  'id' => 22,
103  'name' => 'ojo',
104  'queueDate' => '01-01-2020',
105  'uploadId' => 4,
106  'userId' => 2,
107  'groupId' => 2,
108  'eta' => 3,
109  'status' => 'Processing'
110  ];
111  }
112 
113  $actualJob = new Job(22, 'ojo', '01-01-2020', 4, 2, 2, 3, 'Processing', $jobQueue->getArray());
114 
115  $this->assertEquals($expectedStatus, $actualJob->getArray($version));
116  }
117 
118  // Getter tests
119  public function testGetId()
120  {
121  $job = new Job(22, 'ojo', '01-01-2020', 4, 2, 2, 3, 'Processing');
122  $this->assertEquals(22, $job->getId());
123  }
124 
125  public function testGetName()
126  {
127  $job = new Job(22, 'ojo', '01-01-2020', 4, 2, 2, 3, 'Processing');
128  $this->assertEquals('ojo', $job->getName());
129  }
130 
131  public function testGetQueueDate()
132  {
133  $job = new Job(22, 'ojo', '01-01-2020', 4, 2, 2, 3, 'Processing');
134  $this->assertEquals('01-01-2020', $job->getQueueDate());
135  }
136 
137  public function testGetUploadId()
138  {
139  $job = new Job(22, 'ojo', '01-01-2020', 4, 2, 2, 3, 'Processing');
140  $this->assertEquals(4, $job->getUploadId());
141  }
142 
143  public function testGetUserId()
144  {
145  $job = new Job(22, 'ojo', '01-01-2020', 4, 2, 2, 3, 'Processing');
146  $this->assertEquals(2, $job->getUserId());
147  }
148 
149  public function testGetGroupId()
150  {
151  $job = new Job(22, 'ojo', '01-01-2020', 4, 2, 2, 3, 'Processing');
152  $this->assertEquals(2, $job->getGroupId());
153  }
154 
155  public function testGetEta()
156  {
157  $job = new Job(22, 'ojo', '01-01-2020', 4, 2, 2, 3, 'Processing');
158  $this->assertEquals(3, $job->getEta());
159  }
160 
161  public function testGetStatus()
162  {
163  $job = new Job(22, 'ojo', '01-01-2020', 4, 2, 2, 3, 'Processing');
164  $this->assertEquals('Processing', $job->getStatus());
165  }
166 
167  public function testGetJobQueue()
168  {
169  $jobQueue = new JobQueue(44, 'readmeoss', '2024-07-03 20:41:49', '2024-07-03 20:41:50',
170  'Completed', 0, null, [], 0, true, false, true,
171  ['text' => 'ReadMeOss', 'link' => 'http://localhost/repo/api/v1/report/16']
172  );
173  $job = new Job(22, 'ojo', '01-01-2020', 4, 2, 2, 3, 'Processing', $jobQueue->getArray());
174  $this->assertEquals($jobQueue->getArray(), $job->getJobQueue());
175  }
176 
177  // Setter tests
178  public function testSetName()
179  {
180  $job = new Job(22, 'ojo', '01-01-2020', 4, 2, 2, 3, 'Processing');
181  $job->setName('newName');
182  $this->assertEquals('newName', $job->getName());
183  }
184 
185  public function testSetQueueDate()
186  {
187  $job = new Job(22, 'ojo', '01-01-2020', 4, 2, 2, 3, 'Processing');
188  $job->setQueueDate('02-02-2020');
189  $this->assertEquals('02-02-2020', $job->getQueueDate());
190  }
191 
192  public function testSetUploadId()
193  {
194  $job = new Job(22, 'ojo', '01-01-2020', 4, 2, 2, 3, 'Processing');
195  $job->setUploadId(5);
196  $this->assertEquals(5, $job->getUploadId());
197  }
198 
199  public function testSetUserId()
200  {
201  $job = new Job(22, 'ojo', '01-01-2020', 4, 2, 2, 3, 'Processing');
202  $job->setUserId(3);
203  $this->assertEquals(3, $job->getUserId());
204  }
205 
206  public function testSetGroupId()
207  {
208  $job = new Job(22, 'ojo', '01-01-2020', 4, 2, 2, 3, 'Processing');
209  $job->setGroupId(4);
210  $this->assertEquals(4, $job->getGroupId());
211  }
212  public function testSetEta()
213  {
214  $job = new Job(22, 'ojo', '01-01-2020', 4, 2, 2, 3, 'Processing');
215  $job->setEta(10);
216  $this->assertEquals(10, $job->getEta());
217  }
218 
219  public function testSetStatus()
220  {
221  $job = new Job(22, 'ojo', '01-01-2020', 4, 2, 2, 3, 'Processing');
222  $job->setStatus('Completed');
223  $this->assertEquals('Completed', $job->getStatus());
224  }
225 }
Model class to hold JobQueue info.
Definition: JobQueue.php:18