Test Setup Failed
Push — master ( 298fd3...56daff )
by Matthew
17:14
created

BaseJobManagerTest::testPruneExpriedJobs()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 106
Code Lines 89

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 106
rs 8.2857
c 0
b 0
f 0
cc 1
eloc 89
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Dtc\QueueBundle\Tests\Doctrine;
4
5
use Dtc\QueueBundle\Doctrine\BaseJobManager;
6
use Dtc\QueueBundle\Model\Job;
7
use Dtc\QueueBundle\Tests\Model\BaseJobManagerTest as BaseBaseJobManagerTest;
8
use Dtc\QueueBundle\ODM\JobManager;
9
10
/**
11
 * @author David
12
 *
13
 * This test requires local mongodb running
14
 */
15
abstract class BaseJobManagerTest extends BaseBaseJobManagerTest
16
{
17
    public function testDeleteJob()
18
    {
19
        $job = parent::testDeleteJob();
20
        $jobManager = self::$jobManager;
21
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
22
        $archiveObjectName = $jobManager->getArchiveObjectName();
23
24
        $id = $job->getId();
25
        $archiveRepository = $jobManager->getObjectManager()->getRepository($archiveObjectName);
26
        $result = $archiveRepository->find($id);
27
        self::assertNotNull($result);
28
        self::assertEquals($id, $result->getId());
29
    }
30
31
    public function testResetErroneousJobs()
32
    {
33
        $job = parent::testDeleteJob();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (testDeleteJob() instead of testResetErroneousJobs()). Are you sure this is correct? If so, you might want to change this to $this->testDeleteJob().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
34
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
35
        $jobManager = self::$jobManager;
36
        $archiveObjectName = $jobManager->getArchiveObjectName();
37
38
        $id = $job->getId();
39
        $objectManager = $jobManager->getObjectManager();
40
41
        $archiveRepository = $objectManager->getRepository($archiveObjectName);
42
        $result = $archiveRepository->find($id);
43
        self::assertNotNull($result);
44
        self::assertEquals($id, $result->getId());
45
46
        $result->setStatus(Job::STATUS_ERROR);
47
        $result->setLocked(true);
48
        $result->setLockedAt(new \DateTime());
49
        $result->setFinishedAt(new \DateTime());
50
        $result->setElapsed(12345);
51
        $result->setMessage('soomething');
52
        $objectManager->persist($result);
53
        $objectManager->flush();
54
55
        $count = $jobManager->resetErroneousJobs();
56
57
        self::assertEquals(1, $count);
58
        $repository = $jobManager->getRepository();
59
        $job = $repository->find($id);
60
61
        self::assertNotNull($job);
62
        self::assertEquals(Job::STATUS_NEW, $job->getStatus());
63
        self::assertNull($job->getLockedAt());
64
        self::assertNull($job->getFinishedAt());
65
        self::assertNull($job->getElapsed());
66
        self::assertNull($job->getMessage());
67
        self::assertNull($job->getLocked());
68
69
        $objectManager->remove($job);
70
        $objectManager->flush();
71
    }
72
73
    public function testResetStalledJobs()
74
    {
75
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
76
        $jobManager = self::$jobManager;
77
78
        $job = new self::$jobClass(self::$worker, false, null);
79
        $job->fibonacci(1);
80
        $this->assertNotNull($job->getId(), 'Job id should be generated');
81
        $job->setStatus(Job::STATUS_RUNNING);
82
        $job->setLocked(true);
83
        $time = time();
84
        $date = new \DateTime("@$time");
85
        $job->setLockedAt($date);
86
        $id = $job->getId();
87
        $job = $jobManager->getRepository()->find($id);
88
89
        self::assertNotNull($job);
90
91
        $runClass = self::$jobManager->getRunClass();
0 ignored issues
show
Bug introduced by
The method getRunClass does only exist in Dtc\QueueBundle\ODM\JobM...ueBundle\ORM\JobManager, but not in Dtc\QueueBundle\Beanstal...\Tests\StaticJobManager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
92
93
        $objectManager = self::$jobManager->getObjectManager();
0 ignored issues
show
Bug introduced by
The method getObjectManager does only exist in Dtc\QueueBundle\ODM\JobM...ueBundle\ORM\JobManager, but not in Dtc\QueueBundle\Beanstal...\Tests\StaticJobManager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
94
        $run = new $runClass();
95
        $objectManager->persist($run);
96
        $objectManager->flush();
97
        $runId = $run->getId();
98
        self::assertNotNull($runId);
99
        $job->setRunId($runId);
100
        $objectManager->persist($job);
101
        $objectManager->flush();
102
        $objectManager->remove($run);
103
        $objectManager->flush();
104
        $id = $job->getId();
105
        $job = $jobManager->getRepository()->find($id);
106
107
        self::assertNotNull($job);
108
109
        $archivedRun = $objectManager->getRepository(self::$jobManager->getRunArchiveClass())->find($runId);
0 ignored issues
show
Bug introduced by
The method getRunArchiveClass does only exist in Dtc\QueueBundle\ODM\JobM...ueBundle\ORM\JobManager, but not in Dtc\QueueBundle\Beanstal...\Tests\StaticJobManager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
110
111
        $minusTime = $time - (BaseJobManager::STALLED_SECONDS + 1);
112
        $archivedRun->setEndedAt(new \DateTime("@$minusTime"));
113
114
        $objectManager->persist($archivedRun);
115
        $objectManager->flush();
116
117
        $count = self::$jobManager->resetStalledJobs();
118
119
        self::assertEquals(1, $count);
120
121
        $id = $job->getId();
122
        $job = $jobManager->getRepository()->find($id);
123
124
        self::assertNotNull($job);
125
        self::assertEquals(Job::STATUS_NEW, $job->getStatus());
126
        self::assertNull($job->getLockedAt());
127
        self::assertNull($job->getFinishedAt());
128
        self::assertNull($job->getElapsed());
129
        self::assertNull($job->getMessage());
130
        self::assertNull($job->getLocked());
131
132
        $objectManager->remove($job);
133
        $objectManager->flush();
134
    }
135
136
    public function testPruneErroneousJobs()
137
    {
138
        $job = parent::testDeleteJob();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (testDeleteJob() instead of testPruneErroneousJobs()). Are you sure this is correct? If so, you might want to change this to $this->testDeleteJob().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
139
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
140
        $jobManager = self::$jobManager;
141
        $archiveObjectName = $jobManager->getArchiveObjectName();
142
143
        $id = $job->getId();
144
        $objectManager = $jobManager->getObjectManager();
145
146
        $archiveRepository = $objectManager->getRepository($archiveObjectName);
147
        $result = $archiveRepository->find($id);
148
        self::assertNotNull($result);
149
        self::assertEquals($id, $result->getId());
150
151
        $result->setStatus(Job::STATUS_ERROR);
152
        $result->setLocked(true);
153
        $result->setLockedAt(new \DateTime());
154
        $result->setFinishedAt(new \DateTime());
155
        $result->setElapsed(12345);
156
        $result->setMessage('soomething');
157
        $objectManager->persist($result);
158
        $objectManager->flush();
159
160
        $count = $jobManager->pruneErroneousJobs('asdf');
161
        self::assertEquals(0, $count);
162
        $count = $jobManager->pruneErroneousJobs(null, 'asdf');
163
        self::assertEquals(0, $count);
164
        $count = $jobManager->pruneErroneousJobs('fibonacci', 'asdf');
165
        self::assertEquals(0, $count);
166
        $count = $jobManager->pruneErroneousJobs('fibonacci', 'asdf');
167
        self::assertEquals(0, $count);
168
        $count = $jobManager->pruneErroneousJobs('fibonacci', 'fibonacci');
169
        self::assertEquals(1, $count);
170
        $repository = $jobManager->getRepository();
171
        $job = $repository->find($id);
172
        $objectManager->clear();
173
        self::assertNull($job);
174
        $archiveJob = $archiveRepository->find($id);
175
        self::assertNull($archiveJob);
176
177
        $job = parent::testDeleteJob();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (testDeleteJob() instead of testPruneErroneousJobs()). Are you sure this is correct? If so, you might want to change this to $this->testDeleteJob().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
178
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
179
        $jobManager = self::$jobManager;
180
        $archiveObjectName = $jobManager->getArchiveObjectName();
181
182
        $id = $job->getId();
183
        $objectManager = $jobManager->getObjectManager();
184
185
        $archiveRepository = $objectManager->getRepository($archiveObjectName);
186
        $result = $archiveRepository->find($id);
187
        self::assertNotNull($result);
188
        self::assertEquals($id, $result->getId());
189
190
        $result->setStatus(Job::STATUS_ERROR);
191
        $result->setLocked(true);
192
        $result->setLockedAt(new \DateTime());
193
        $result->setFinishedAt(new \DateTime());
194
        $result->setElapsed(12345);
195
        $result->setMessage('soomething');
196
        $objectManager->persist($result);
197
        $objectManager->flush();
198
199
        $job = parent::testDeleteJob();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (testDeleteJob() instead of testPruneErroneousJobs()). Are you sure this is correct? If so, you might want to change this to $this->testDeleteJob().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
200
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
201
        $jobManager = self::$jobManager;
202
        $archiveObjectName = $jobManager->getArchiveObjectName();
203
204
        $id = $job->getId();
205
        $objectManager = $jobManager->getObjectManager();
206
207
        $archiveRepository = $objectManager->getRepository($archiveObjectName);
208
        $result = $archiveRepository->find($id);
209
        self::assertNotNull($result);
210
        self::assertEquals($id, $result->getId());
211
212
        $result->setStatus(Job::STATUS_ERROR);
213
        $result->setLocked(true);
214
        $result->setLockedAt(new \DateTime());
215
        $result->setFinishedAt(new \DateTime());
216
        $result->setElapsed(12345);
217
        $result->setMessage('soomething');
218
        $objectManager->persist($result);
219
        $objectManager->flush();
220
        $count = $jobManager->pruneErroneousJobs();
221
        self::assertEquals(2, $count);
222
    }
223
224
    public function testPruneStalledJobs()
225
    {
226
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
227
        $jobManager = self::$jobManager;
228
229
        $job = new self::$jobClass(self::$worker, false, null);
230
        $job->fibonacci(1);
231
        $this->assertNotNull($job->getId(), 'Job id should be generated');
232
        $job->setStatus(Job::STATUS_RUNNING);
233
        $job->setLocked(true);
234
        $time = time();
235
        $date = new \DateTime("@$time");
236
        $job->setLockedAt($date);
237
        $id = $job->getId();
238
        $job = $jobManager->getRepository()->find($id);
239
240
        self::assertNotNull($job);
241
242
        $runClass = self::$jobManager->getRunClass();
0 ignored issues
show
Bug introduced by
The method getRunClass does only exist in Dtc\QueueBundle\ODM\JobM...ueBundle\ORM\JobManager, but not in Dtc\QueueBundle\Beanstal...\Tests\StaticJobManager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
243
244
        $objectManager = self::$jobManager->getObjectManager();
0 ignored issues
show
Bug introduced by
The method getObjectManager does only exist in Dtc\QueueBundle\ODM\JobM...ueBundle\ORM\JobManager, but not in Dtc\QueueBundle\Beanstal...\Tests\StaticJobManager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
245
        $run = new $runClass();
246
        $objectManager->persist($run);
247
        $objectManager->flush();
248
        $runId = $run->getId();
249
        self::assertNotNull($runId);
250
        $job->setRunId($runId);
251
        $objectManager->persist($job);
252
        $objectManager->flush();
253
        $objectManager->remove($run);
254
        $objectManager->flush();
255
        $id = $job->getId();
256
        $job = $jobManager->getRepository()->find($id);
257
258
        self::assertNotNull($job);
259
260
        $archivedRun = $objectManager->getRepository(self::$jobManager->getRunArchiveClass())->find($runId);
0 ignored issues
show
Bug introduced by
The method getRunArchiveClass does only exist in Dtc\QueueBundle\ODM\JobM...ueBundle\ORM\JobManager, but not in Dtc\QueueBundle\Beanstal...\Tests\StaticJobManager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
261
262
        $minusTime = $time - (BaseJobManager::STALLED_SECONDS + 1);
263
        $archivedRun->setEndedAt(new \DateTime("@$minusTime"));
264
265
        $objectManager->persist($archivedRun);
266
        $objectManager->flush();
267
268
        $count = $jobManager->pruneStalledJobs('asdf');
269
        self::assertEquals(0, $count);
270
        $count = $jobManager->pruneStalledJobs(null, 'asdf');
271
        self::assertEquals(0, $count);
272
        $count = $jobManager->pruneStalledJobs('fibonacci', 'asdf');
273
        self::assertEquals(0, $count);
274
        $count = $jobManager->pruneStalledJobs('fibonacci', 'fibonacci');
275
        self::assertEquals(1, $count);
276
277
        $id = $job->getId();
278
        $job = $jobManager->getRepository()->find($id);
279
280
        self::assertNull($job);
281
282
        $archivedJob = $jobManager->getObjectManager()->getRepository($jobManager->getArchiveObjectName())->find($id);
283
284
        self::assertNotNull($archivedJob);
285
        self::assertEquals(Job::STATUS_ERROR, $archivedJob->getStatus());
286
        $objectManager->remove($archivedJob);
287
        $objectManager->flush();
288
289
        // multiple
290
291
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
292
        $jobManager = self::$jobManager;
293
294
        $job = new self::$jobClass(self::$worker, false, null);
295
        $job->fibonacci(1);
296
        $this->assertNotNull($job->getId(), 'Job id should be generated');
297
        $job->setStatus(Job::STATUS_RUNNING);
298
        $job->setLocked(true);
299
        $time = time();
300
        $date = new \DateTime("@$time");
301
        $job->setLockedAt($date);
302
        $id = $job->getId();
303
        $job = $jobManager->getRepository()->find($id);
304
305
        self::assertNotNull($job);
306
307
        $runClass = self::$jobManager->getRunClass();
308
309
        $objectManager = self::$jobManager->getObjectManager();
310
        $run = new $runClass();
311
        $objectManager->persist($run);
312
        $objectManager->flush();
313
        $runId = $run->getId();
314
        self::assertNotNull($runId);
315
        $job->setRunId($runId);
316
        $objectManager->persist($job);
317
        $objectManager->flush();
318
        $objectManager->remove($run);
319
        $objectManager->flush();
320
        $id = $job->getId();
321
        $job = $jobManager->getRepository()->find($id);
322
323
        self::assertNotNull($job);
324
325
        $archivedRun = $objectManager->getRepository(self::$jobManager->getRunArchiveClass())->find($runId);
326
327
        $minusTime = $time - (BaseJobManager::STALLED_SECONDS + 1);
328
        $archivedRun->setEndedAt(new \DateTime("@$minusTime"));
329
330
        $objectManager->persist($archivedRun);
331
        $objectManager->flush();
332
333
        $jobManager = self::$jobManager;
334
335
        $job = new self::$jobClass(self::$worker, false, null);
336
        $job->fibonacci(1);
337
        $this->assertNotNull($job->getId(), 'Job id should be generated');
338
        $job->setStatus(Job::STATUS_RUNNING);
339
        $job->setLocked(true);
340
        $time = time();
341
        $date = new \DateTime("@$time");
342
        $job->setLockedAt($date);
343
        $id = $job->getId();
344
        $job = $jobManager->getRepository()->find($id);
0 ignored issues
show
Bug introduced by
The method getRepository does only exist in Dtc\QueueBundle\ODM\JobM...ueBundle\ORM\JobManager, but not in Dtc\QueueBundle\Beanstal...\Tests\StaticJobManager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
345
346
        self::assertNotNull($job);
347
348
        $runClass = self::$jobManager->getRunClass();
349
350
        $objectManager = self::$jobManager->getObjectManager();
351
        $run = new $runClass();
352
        $objectManager->persist($run);
353
        $objectManager->flush();
354
        $runId = $run->getId();
355
        self::assertNotNull($runId);
356
        $job->setRunId($runId);
357
        $objectManager->persist($job);
358
        $objectManager->flush();
359
        $objectManager->remove($run);
360
        $objectManager->flush();
361
        $id = $job->getId();
362
        $job = $jobManager->getRepository()->find($id);
363
364
        self::assertNotNull($job);
365
366
        $archivedRun = $objectManager->getRepository(self::$jobManager->getRunArchiveClass())->find($runId);
367
368
        $minusTime = $time - (BaseJobManager::STALLED_SECONDS + 1);
369
        $archivedRun->setEndedAt(new \DateTime("@$minusTime"));
370
371
        $objectManager->persist($archivedRun);
372
        $objectManager->flush();
373
        $count = $jobManager->pruneStalledJobs();
374
        self::assertEquals(2, $count);
375
    }
376
377
    public function testPruneExpriedJobs()
378
    {
379
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
380
        $jobManager = self::$jobManager;
381
        $objectManager = $jobManager->getObjectManager();
382
383
        $job = new self::$jobClass(self::$worker, false, null);
384
        $job->fibonacci(1);
385
        $this->assertNotNull($job->getId(), 'Job id should be generated');
386
        $time = time() - 1;
387
        $date = new \DateTime("@$time");
388
        $job->setExpiresAt($date);
389
        $objectManager->persist($job);
390
        $objectManager->flush();
391
392
        $count = $jobManager->pruneExpiredJobs('asdf');
393
        self::assertEquals(0, $count);
394
        $count = $jobManager->pruneExpiredJobs(null, 'asdf');
395
        self::assertEquals(0, $count);
396
        $count = $jobManager->pruneExpiredJobs(null, 'fibonacci');
397
        self::assertEquals(1, $count);
398
399
        $job = new self::$jobClass(self::$worker, false, null);
400
        $job->fibonacci(1);
401
        $this->assertNotNull($job->getId(), 'Job id should be generated');
402
        $time = time() - 1;
403
        $date = new \DateTime("@$time");
404
        $job->setExpiresAt($date);
405
        $objectManager->persist($job);
406
        $objectManager->flush();
407
408
        $job = new self::$jobClass(self::$worker, false, null);
409
        $job->fibonacci(1);
410
        $this->assertNotNull($job->getId(), 'Job id should be generated');
411
        $time = time() - 1;
412
        $date = new \DateTime("@$time");
413
        $job->setExpiresAt($date);
414
        $objectManager->persist($job);
415
        $objectManager->flush();
416
417
        $count = $jobManager->pruneExpiredJobs(null, 'fibonacci');
418
        self::assertEquals(2, $count);
419
420
        $job = new self::$jobClass(self::$worker, false, null);
421
        $job->fibonacci(1);
422
        $this->assertNotNull($job->getId(), 'Job id should be generated');
423
        $time = time() - 1;
424
        $date = new \DateTime("@$time");
425
        $job->setExpiresAt($date);
426
        $objectManager->persist($job);
427
        $objectManager->flush();
428
429
        $job = new self::$jobClass(self::$worker, false, null);
430
        $job->fibonacci(1);
431
        $this->assertNotNull($job->getId(), 'Job id should be generated');
432
        $time = time() - 1;
433
        $date = new \DateTime("@$time");
434
        $job->setExpiresAt($date);
435
        $objectManager->persist($job);
436
        $objectManager->flush();
437
438
        $count = $jobManager->pruneExpiredJobs('fibonacci', 'fibonacci');
439
        self::assertEquals(2, $count);
440
441
        $job = new self::$jobClass(self::$worker, false, null);
442
        $job->fibonacci(1);
443
        $this->assertNotNull($job->getId(), 'Job id should be generated');
444
        $time = time() - 1;
445
        $date = new \DateTime("@$time");
446
        $job->setExpiresAt($date);
447
        $objectManager->persist($job);
448
        $objectManager->flush();
449
450
        $job = new self::$jobClass(self::$worker, false, null);
451
        $job->fibonacci(1);
452
        $this->assertNotNull($job->getId(), 'Job id should be generated');
453
        $time = time() - 1;
454
        $date = new \DateTime("@$time");
455
        $job->setExpiresAt($date);
456
        $objectManager->persist($job);
457
        $objectManager->flush();
458
459
        $count = $jobManager->pruneExpiredJobs('fibonacci');
460
        self::assertEquals(2, $count);
461
462
        $job = new self::$jobClass(self::$worker, false, null);
463
        $job->fibonacci(1);
464
        $this->assertNotNull($job->getId(), 'Job id should be generated');
465
        $time = time() - 1;
466
        $date = new \DateTime("@$time");
467
        $job->setExpiresAt($date);
468
        $objectManager->persist($job);
469
        $objectManager->flush();
470
471
        $job = new self::$jobClass(self::$worker, false, null);
472
        $job->fibonacci(1);
473
        $this->assertNotNull($job->getId(), 'Job id should be generated');
474
        $time = time() - 1;
475
        $date = new \DateTime("@$time");
476
        $job->setExpiresAt($date);
477
        $objectManager->persist($job);
478
        $objectManager->flush();
479
480
        $count = $jobManager->pruneExpiredJobs();
481
        self::assertEquals(2, $count);
482
    }
483
484
    public function testPruneArchivedJobs()
485
    {
486
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
487
        $jobManager = self::$jobManager;
488
        $objectManager = $jobManager->getObjectManager();
489
        $jobArchiveClass = $jobManager->getArchiveObjectName();
490
        $jobArchiveRepository = $objectManager->getRepository($jobArchiveClass);
491
492
        $job = new self::$jobClass(self::$worker, false, null);
493
        $job->fibonacci(1);
494
        $objectManager->remove($job);
495
        $objectManager->flush();
496
497
        $jobArchive = $jobArchiveRepository->find($job->getId());
498
        self::assertNotNull($jobArchive);
499
        $time = time() - 86401;
500
        $jobArchive->setUpdatedAt(new \DateTime("@$time"));
501
        $objectManager->persist($jobArchive);
502
        $objectManager->flush();
503
504
        $older = $time + 1;
505
        $count = $jobManager->pruneArchivedJobs(new \DateTime("@$time"));
506
        self::assertEquals(0, $count);
507
        $count = $jobManager->pruneArchivedJobs(new \DateTime("@$older"));
508
        self::assertEquals(1, $count);
509
510
        $job = new self::$jobClass(self::$worker, false, null);
511
        $job->fibonacci(1);
512
        $objectManager->remove($job);
513
        $objectManager->flush();
514
515
        $jobArchive = $jobArchiveRepository->find($job->getId());
516
        self::assertNotNull($jobArchive);
517
        $time = time() - 86401;
518
        $jobArchive->setUpdatedAt(new \DateTime("@$time"));
519
        $objectManager->persist($jobArchive);
520
        $objectManager->flush();
521
522
        $job = new self::$jobClass(self::$worker, false, null);
523
        $job->fibonacci(1);
524
        $objectManager->remove($job);
525
        $objectManager->flush();
526
527
        $jobArchive = $jobArchiveRepository->find($job->getId());
528
        self::assertNotNull($jobArchive);
529
        $time = time() - 86401;
530
        $jobArchive->setUpdatedAt(new \DateTime("@$time"));
531
        $objectManager->persist($jobArchive);
532
        $objectManager->flush();
533
        $older = $time + 1;
534
        $count = $jobManager->pruneArchivedJobs(new \DateTime("@$time"));
535
        self::assertEquals(0, $count);
536
        $count = $jobManager->pruneArchivedJobs(new \DateTime("@$older"));
537
        self::assertEquals(2, $count);
538
    }
539
540
    public function testPerformance()
541
    {
542
        $jobs = self::$jobManager->getRepository()->findAll();
0 ignored issues
show
Bug introduced by
The method getRepository does only exist in Dtc\QueueBundle\ODM\JobM...ueBundle\ORM\JobManager, but not in Dtc\QueueBundle\Beanstal...\Tests\StaticJobManager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
543
        foreach ($jobs as $job) {
544
            self::$jobManager->getObjectManager()->remove($job);
0 ignored issues
show
Bug introduced by
The method getObjectManager does only exist in Dtc\QueueBundle\ODM\JobM...ueBundle\ORM\JobManager, but not in Dtc\QueueBundle\Beanstal...\Tests\StaticJobManager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
545
        }
546
        self::$jobManager->getObjectManager()->flush();
547
548
        self::$jobManager->getObjectManager()->clear();
549
        parent::testPerformance();
550
    }
551
}
552