Completed
Push — master ( 5fe2ea...bba9f0 )
by Matthew
13:23
created

BaseJobManagerTest::testCountLiveJobs()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 23
nc 4
nop 0
1
<?php
2
3
namespace Dtc\QueueBundle\Tests\Doctrine;
4
5
use Doctrine\ODM\MongoDB\DocumentManager;
6
use Doctrine\ORM\EntityManager;
7
use Dtc\QueueBundle\Doctrine\BaseJobManager;
8
use Dtc\QueueBundle\Doctrine\DtcQueueListener;
9
use Dtc\QueueBundle\Model\BaseJob;
10
use Dtc\QueueBundle\Model\Job;
11
use Dtc\QueueBundle\Tests\Model\PriorityTestTrait;
12
use Dtc\QueueBundle\Model\RetryableJob;
13
use Dtc\QueueBundle\Tests\FibonacciWorker;
14
use Dtc\QueueBundle\Tests\Model\BaseJobManagerTest as BaseBaseJobManagerTest;
15
use Dtc\QueueBundle\ODM\JobManager;
16
use Dtc\QueueBundle\Tests\ORM\JobManagerTest;
17
use Symfony\Component\DependencyInjection\Container;
18
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
19
20
/**
21
 * @author David
22
 *
23
 * This test requires local mongodb running
24
 */
25
abstract class BaseJobManagerTest extends BaseBaseJobManagerTest
26
{
27
    use PriorityTestTrait;
28
29
    protected static $dtcQueueListener;
30
31
    /** @var DocumentManager|EntityManager */
32
    protected static $objectManager;
33
    protected static $objectName;
34
    protected static $archiveObjectName;
35
    protected static $runClass;
36
    protected static $runArchiveClass;
37
    protected static $jobTimingClass;
38
    protected static $jobManagerClass;
39
    protected static $runManagerClass;
40
    protected static $jobTimingManagerClass;
41
    public static $runManager;
42
43
    public static function setUpBeforeClass()
44
    {
45
        self::$jobTimingManager = new self::$jobTimingManagerClass(self::$objectManager, self::$jobTimingClass, true);
46
        self::$runManager = new self::$runManagerClass(self::$objectManager, self::$runClass, self::$runArchiveClass);
47
        self::$jobManager = new self::$jobManagerClass(self::$runManager, self::$jobTimingManager, self::$objectManager, self::$objectName, self::$archiveObjectName);
48
        self::$jobManager->setMaxPriority(255);
49
50
        self::assertEquals(255, self::$jobManager->getMaxPriority());
51
        self::assertEquals(JobManager::PRIORITY_DESC, self::$jobManager->getPriorityDirection());
52
        self::$jobManager->setPriorityDirection(JobManager::PRIORITY_ASC);
53
        self::assertEquals(JobManager::PRIORITY_ASC, self::$jobManager->getPriorityDirection());
54
        self::$jobManager->setPriorityDirection(JobManager::PRIORITY_DESC);
55
56
        /** @var BaseJobManager $jobManager */
57
        $jobManager = self::$jobManager;
58
59
        $parameters = new ParameterBag();
60
61
        $container = new Container($parameters);
62
        $container->set('dtc_queue.job_manager', $jobManager);
63
        $container->set('dtc_queue.run_manager', self::$runManager);
64
65
        self::$dtcQueueListener = new DtcQueueListener(self::$jobManager->getJobArchiveClass(), self::$runManager->getRunArchiveClass());
66
        self::$objectManager->getEventManager()->addEventListener('preUpdate', self::$dtcQueueListener);
67
        self::$objectManager->getEventManager()->addEventListener('prePersist', self::$dtcQueueListener);
68
        self::$objectManager->getEventManager()->addEventListener('preRemove', self::$dtcQueueListener);
69
70
        self::$worker = new FibonacciWorker();
71
72
        self::$worker->setJobClass($jobManager->getRepository()->getClassName());
73
        parent::setUpBeforeClass();
74
    }
75
76
    public static function tearDownAfterClass()
77
    {
78
        self::$objectManager->getEventManager()->removeEventListener('preUpdate', self::$dtcQueueListener);
79
        self::$objectManager->getEventManager()->removeEventListener('prePersist', self::$dtcQueueListener);
80
        self::$objectManager->getEventManager()->removeEventListener('preRemove', self::$dtcQueueListener);
81
        parent::tearDownAfterClass();
82
    }
83
84
    public function testOrdering()
85
    {
86
        // priority when at
87
        /** @var BaseJobManager $jobManager */
88
        $jobManager = self::$jobManager;
89
90
        $time1 = time() - 2;
91
        $dateTime1 = new \DateTime("@$time1");
92
93
        $time2 = time();
94
        $dateTime2 = new \DateTime("@$time2");
95
96
        /** @var Job $job */
97
        $job = new static::$jobClass(static::$worker, false, null);
98
        $job->fibonacci(1);
0 ignored issues
show
Documentation Bug introduced by
The method fibonacci does not exist on object<Dtc\QueueBundle\Model\Job>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
99
        $job->setWhenAt($dateTime1);
100
        $job->setPriority(3);
101
        $id = $job->getId();
102
103
        $job2 = new static::$jobClass(static::$worker, false, null);
104
        $job2->setPriority(1);
105
        $job2->setWhenAt($dateTime2);
106
        $job2->fibonacci(1);
107
        $id2 = $job2->getId();
108
109
        $job3 = new static::$jobClass(static::$worker, false, null);
110
        $job3->setPriority(1);
111
        $job3->setWhenAt($dateTime1);
112
        $job3->fibonacci(1);
113
        $id3 = $job3->getId();
114
115
        $job4 = new static::$jobClass(static::$worker, false, null);
116
        $job4->setPriority(1);
117
        $job4->setWhenAt($dateTime2);
118
        $job4->fibonacci(1);
119
        $id4 = $job4->getId();
120
121
        $nextJob = $jobManager->getJob();
122
        static::assertEquals($id3, $nextJob->getId());
123
        $nextNextJob = $jobManager->getJob();
124
        $nextNextId = $nextNextJob->getId();
125
        static::assertTrue($id4 == $nextNextId || $id2 == $nextNextId, "$nextNextId not equals $id4 or $id2, could be $id or $id3");
126
127
        static::assertNotNull($jobManager->getJob());
128
        static::assertNotNull($jobManager->getJob());
129
130
        // non-priority when at
131
        $time1 = time() - 2;
132
        $dateTime1 = new \DateTime("@$time1");
133
134
        $time2 = time();
135
        $dateTime2 = new \DateTime("@$time2");
136
137
        /** @var Job $job */
138
        $job = new static::$jobClass(static::$worker, false, null);
139
        $job->fibonacci(1);
0 ignored issues
show
Documentation Bug introduced by
The method fibonacci does not exist on object<Dtc\QueueBundle\Model\Job>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
140
        $job->setWhenAt($dateTime1);
141
        $job->setPriority(3);
142
        $id = $job->getId();
143
144
        $job2 = new static::$jobClass(static::$worker, false, null);
145
        $job2->setPriority(1);
146
        $job2->setWhenAt($dateTime2);
147
        $job2->fibonacci(1);
148
149
        $job3 = new static::$jobClass(static::$worker, false, null);
150
        $job3->setPriority(1);
151
        $job3->setWhenAt($dateTime2);
152
        $job3->fibonacci(1);
153
154
        $job4 = new static::$jobClass(static::$worker, false, null);
155
        $job4->setPriority(1);
156
        $job4->setWhenAt($dateTime2);
157
        $job4->fibonacci(1);
158
159
        $nextJob = $jobManager->getJob(null, null, false);
160
        static::assertEquals($id, $nextJob->getId());
161
        static::assertNotNull($jobManager->getJob());
162
        static::assertNotNull($jobManager->getJob());
163
        static::assertNotNull($jobManager->getJob());
164
    }
165
166
    public function getJobBy()
167
    {
168
        /** @var BaseJobManager $jobManager */
169
        $jobManager = self::$jobManager;
170
171
        /** @var Job $job */
172
        $job = new static::$jobClass(static::$worker, false, null);
173
        $job->fibonacci(1);
0 ignored issues
show
Documentation Bug introduced by
The method fibonacci does not exist on object<Dtc\QueueBundle\Model\Job>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
174
        $id = $job->getId();
175
        $nextJob = $jobManager->getJob('fibonacci', null);
176
        static::assertNotNull($nextJob);
177
        static::assertEquals($id, $nextJob->getId());
178
179
        /** @var Job $job */
180
        $job = new static::$jobClass(static::$worker, false, null);
181
        $job->fibonacci(1);
0 ignored issues
show
Documentation Bug introduced by
The method fibonacci does not exist on object<Dtc\QueueBundle\Model\Job>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
182
        $id = $job->getId();
183
        $nextJob = $jobManager->getJob('fibonacci', 'fibonacci');
184
        static::assertNotNull($nextJob);
185
        static::assertEquals($id, $nextJob->getId());
186
187
        /** @var Job $job */
188
        $job = new static::$jobClass(static::$worker, false, null);
189
        $job->fibonacci(1);
0 ignored issues
show
Documentation Bug introduced by
The method fibonacci does not exist on object<Dtc\QueueBundle\Model\Job>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
190
        $id = $job->getId();
191
        $nextJob = $jobManager->getJob(null, 'fibonacci');
192
        static::assertNotNull($nextJob);
193
        static::assertEquals($id, $nextJob->getId());
194
195
        /** @var Job $job */
196
        $job = new static::$jobClass(static::$worker, false, null);
197
        $job->fibonacci(1);
0 ignored issues
show
Documentation Bug introduced by
The method fibonacci does not exist on object<Dtc\QueueBundle\Model\Job>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
198
        $id = $job->getId();
199
        $nextJob = $jobManager->getJob(null, 'fibonaccia');
200
        static::assertNull($nextJob);
201
        $nextJob = $jobManager->getJob('fibonacci', 'fibonaccia');
202
        static::assertNull($nextJob);
203
        $nextJob = $jobManager->getJob('fibonaccii', 'fibonacci');
204
        static::assertNull($nextJob);
205
        $nextJob = $jobManager->getJob();
206
        static::assertNotNull($nextJob);
207
        static::assertEquals($id, $nextJob->getId());
208
    }
209
210
    public function testDeleteJob()
211
    {
212
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
213
        $jobManager = self::$jobManager;
214
215
        /** @var Job $job */
216
        $job = $this->getJob();
217
        $id = $job->getId();
218
        $jobManager->deleteJob($job);
219
220
        $nextJob = $jobManager->getJob(null, null, true, 123);
221
        self::assertNull($nextJob, "Shouldn't be any jobs left in queue");
222
223
        $archiveObjectName = $jobManager->getJobArchiveClass();
224
225
        self::assertNotNull($id);
226
        $archiveRepository = $jobManager->getObjectManager()->getRepository($archiveObjectName);
227
        $result = $archiveRepository->find($id);
228
        self::assertNotNull($result);
229
        self::assertEquals($id, $result->getId());
230
    }
231
232
    public function testCountLiveJobs()
233
    {
234
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
235
        $jobManager = self::$jobManager;
236
237
        while ($job = $jobManager->getJob()) {
238
            $jobManager->deleteJob($job);
239
        }
240
241
        $this->getJob();
242
243
        $count = $jobManager->countLiveJobs();
244
        self::assertEquals(1, $count);
245
246
        $this->getJob();
247
248
        $count = $jobManager->countLiveJobs();
249
        self::assertEquals(2, $count);
250
251
        $this->getJob();
252
253
        $count = $jobManager->countLiveJobs();
254
        self::assertEquals(3, $count);
255
256
        $count = $jobManager->countLiveJobs('asdf');
257
        self::assertEquals(0, $count);
258
259
        $count = $jobManager->countLiveJobs('fibonacci');
260
        self::assertEquals(3, $count);
261
262
        $count = $jobManager->countLiveJobs('fibonacci', 'test');
263
        self::assertEquals(0, $count);
264
265
        $count = $jobManager->countLiveJobs('fibonacci', 'fibonacci');
266
        self::assertEquals(3, $count);
267
268
        while ($job = $jobManager->getJob()) {
269
            $jobManager->deleteJob($job);
270
        }
271
    }
272
273
    public function testArchiveAllJobs()
274
    {
275
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
276
        $jobManager = self::$jobManager;
277
278
        while ($job = $jobManager->getJob()) {
279
            $jobManager->deleteJob($job);
280
        }
281
282
        $this->getJob();
283
284
        $count = $jobManager->countLiveJobs();
285
        $archiveCount = $this->runCountQuery($jobManager->getJobArchiveClass());
286
        self::assertEquals(1, $count);
287
        $allCount = $this->runCountQuery($jobManager->getJobClass());
288
        $counter = 0;
289
        $countJobs = function ($count) use (&$counter) {
290
            $counter += $count;
291
        };
292
        $jobManager->archiveAllJobs(null, null, $countJobs);
293
        self::assertEquals(0, $jobManager->countLiveJobs());
294
        self::assertEquals($allCount - 1, $this->runCountQuery($jobManager->getJobClass()));
295
        self::assertEquals($archiveCount + 1, $this->runCountQuery($jobManager->getJobArchiveClass()));
296
        self::assertEquals(1, $counter);
297
298
        $this->getJob();
299
        $this->getJob();
300
301
        $count = $jobManager->countLiveJobs();
302
        self::assertEquals(2, $count);
303
        $archiveCount = $this->runCountQuery($jobManager->getJobArchiveClass());
304
        $counter = 0;
305
        $jobManager->archiveAllJobs('fibonacci', null, $countJobs);
306
        self::assertEquals(0, $jobManager->countLiveJobs());
307
        self::assertEquals(2, $counter);
308
        self::assertEquals($archiveCount + 2, $this->runCountQuery($jobManager->getJobArchiveClass()));
309
310
        $this->getJob();
311
        $this->getJob();
312
313
        $count = $jobManager->countLiveJobs();
314
        self::assertEquals(2, $count);
315
316
        $jobManager->archiveAllJobs('fibonacc', null, $countJobs);
317
        self::assertEquals(2, $jobManager->countLiveJobs());
318
319
        $jobManager->archiveAllJobs('fibonacci', 'fibo', $countJobs);
320
        self::assertEquals(2, $jobManager->countLiveJobs());
321
322
        $jobManager->archiveAllJobs('fibonacci', 'fibonacci', $countJobs);
323
        self::assertEquals(0, $jobManager->countLiveJobs());
324
325
        while ($job = $jobManager->getJob()) {
326
            $jobManager->deleteJob($job);
327
        }
328
    }
329
330
    abstract protected function runCountQuery($class);
331
332
    public function testResetErroneousJobs()
333
    {
334
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
335
        $jobManager = self::$jobManager;
336
337
        $id = $this->createErroredJob();
338
        $archiveObjectName = $jobManager->getJobArchiveClass();
339
        $objectManager = $jobManager->getObjectManager();
340
        $archiveRepository = $objectManager->getRepository($archiveObjectName);
341
        $result = $archiveRepository->find($id);
342
        self::assertNotNull($result);
343
        self::assertEquals(BaseJob::STATUS_ERROR, $result->getStatus());
344
        if ($objectManager instanceof EntityManager) {
345
            JobManagerTest::createObjectManager();
346
            $jobManager = new self::$jobManagerClass(self::$runManager, self::$jobTimingManager, self::$objectManager, self::$objectName, self::$archiveObjectName);
347
            $jobManager->getObjectManager()->clear();
348
            $objectManager = $jobManager->getObjectManager();
349
        }
350
351
        $count = $jobManager->resetErroneousJobs();
352
353
        self::assertEquals(1, $count);
354
        $repository = $jobManager->getRepository();
355
        $job = $repository->find($id);
356
357
        self::assertNotNull($job);
358
        self::assertEquals(BaseJob::STATUS_NEW, $job->getStatus());
359
        self::assertNull($job->getLockedAt());
360
        self::assertNull($job->getFinishedAt());
361
        self::assertNull($job->getElapsed());
362
        self::assertNull($job->getMessage());
363
        self::assertNull($job->getLocked());
364
365
        $objectManager->remove($job);
366
        $objectManager->flush();
367
368
        $id = $this->createErroredJob();
369
        $archiveObjectName = $jobManager->getJobArchiveClass();
370
        $objectManager = $jobManager->getObjectManager();
371
        $archiveRepository = $objectManager->getRepository($archiveObjectName);
372
        $result = $archiveRepository->find($id);
373
        $result->setMaxRetries(10);
374
        $result->setRetries(10);
375
        $objectManager->persist($result);
376
        $objectManager->flush();
377
        $count = $jobManager->resetErroneousJobs();
378
        self::assertEquals(0, $count);
379
        $job = $repository->find($id);
380
        self::assertNull($job);
381
        $job = $archiveRepository->find($id);
382
        self::assertNotNull($job);
383
        $objectManager->remove($job);
384
        $objectManager->flush();
385
    }
386
387
    protected function createErroredJob()
388
    {
389
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
390
        $jobManager = self::$jobManager;
391
392
        /** @var Job $job */
393
        $job = $this->getJob();
394
        $id = $job->getId();
395
        $jobManager->deleteJob($job);
396
397
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
398
        $archiveObjectName = $jobManager->getJobArchiveClass();
399
400
        $objectManager = $jobManager->getObjectManager();
401
402
        $archiveRepository = $objectManager->getRepository($archiveObjectName);
403
        $result = $archiveRepository->find($id);
404
        self::assertNotNull($result);
405
        self::assertEquals($id, $result->getId());
406
407
        $result->setStatus(BaseJob::STATUS_ERROR);
408
        $result->setLocked(true);
409
        $result->setLockedAt(new \DateTime());
410
        $result->setFinishedAt(new \DateTime());
411
        $result->setElapsed(12345);
412
        $result->setMessage('soomething');
413
        $objectManager->persist($result);
414
        $objectManager->flush();
415
416
        return $id;
417
    }
418
419
    /**
420
     * @param bool $endRun
421
     * @param bool $setId
422
     *
423
     * @return mixed
424
     */
425
    public function createStalledJob($endRun, $setId)
426
    {
427
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
428
        $jobManager = self::$jobManager;
429
430
        $job = new self::$jobClass(self::$worker, false, null);
431
        $job->fibonacci(1);
432
        self::assertNotNull($job->getId(), 'Job id should be generated');
433
        $job->setStatus(BaseJob::STATUS_RUNNING);
434
        $job->setLocked(true);
435
        $time = time();
436
        $date = new \DateTime("@$time");
437
        $job->setLockedAt($date);
438
        $id = $job->getId();
439
        $job = $jobManager->getRepository()->find($id);
440
441
        self::assertNotNull($job);
442
443
        $runClass = self::$runManager->getRunClass();
444
445
        $objectManager = $jobManager->getObjectManager();
446
        $run = new $runClass();
447
        $run->setLastHeartbeatAt(new \DateTime());
448
        if ($setId) {
449
            $run->setCurrentJobId($job->getId());
450
        }
451
        $objectManager->persist($run);
452
        $objectManager->flush();
453
        $runId = $run->getId();
454
        self::assertNotNull($runId);
455
        $job->setRunId($runId);
456
        $objectManager->persist($job);
457
        $objectManager->flush();
458
        if ($endRun) {
459
            $objectManager->remove($run);
460
            $objectManager->flush();
461
        }
462
        $id = $job->getId();
463
        $job = $jobManager->getRepository()->find($id);
464
465
        self::assertNotNull($job);
466
467
        if ($endRun) {
468
            $archivedRun = $objectManager->getRepository(self::$runManager->getRunArchiveClass())->find($runId);
469
470
            $minusTime = $time - (BaseJobManager::STALLED_SECONDS + 1);
471
            $archivedRun->setEndedAt(new \DateTime("@$minusTime"));
472
473
            $objectManager->persist($archivedRun);
474
            $objectManager->flush();
475
        }
476
        $id = $job->getId();
477
478
        return $id;
479
    }
480
481
    public function testResetStalledJobs()
482
    {
483
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
484
        $jobManager = self::$jobManager;
485
        $id = $this->createStalledJob(true, false);
486
487
        $objectManager = $jobManager->getObjectManager();
488
        $count = $jobManager->resetStalledJobs();
489
        self::assertEquals(1, $count);
490
491
        $job = $jobManager->getRepository()->find($id);
492
493
        self::assertNotNull($job);
494
        self::assertEquals(BaseJob::STATUS_NEW, $job->getStatus());
495
        self::assertNull($job->getLockedAt());
496
        self::assertNull($job->getFinishedAt());
497
        self::assertNull($job->getElapsed());
498
        self::assertNull($job->getMessage());
499
        self::assertNull($job->getLocked());
500
        self::assertEquals(1, $job->getStalledCount());
501
502
        $objectManager->remove($job);
503
        $objectManager->flush();
504
505
        $jobManager = self::$jobManager;
506
        $id = $this->createStalledJob(true, true);
507
508
        $objectManager = $jobManager->getObjectManager();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
509
        $count = $jobManager->resetStalledJobs();
510
        self::assertEquals(1, $count);
511
512
        $job = $jobManager->getRepository()->find($id);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getRepository() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
513
514
        self::assertNotNull($job);
515
        $objectManager->remove($job);
516
        $objectManager->flush();
517
518
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
519
        $id = $this->createStalledJob(false, false);
520
521
        $objectManager = $jobManager->getObjectManager();
522
        $count = $jobManager->resetStalledJobs();
523
        self::assertEquals(1, $count);
524
525
        $job = $jobManager->getRepository()->find($id);
526
527
        self::assertNotNull($job);
528
        self::assertEquals(BaseJob::STATUS_NEW, $job->getStatus());
529
        self::assertNull($job->getLockedAt());
530
        self::assertNull($job->getFinishedAt());
531
        self::assertNull($job->getElapsed());
532
        self::assertNull($job->getMessage());
533
        self::assertNull($job->getLocked());
534
        self::assertEquals(1, $job->getStalledCount());
535
536
        $objectManager->remove($job);
537
        $objectManager->flush();
538
539
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
540
        $jobManager = self::$jobManager;
541
        $id = $this->createStalledJob(false, true);
542
543
        $job = $jobManager->getRepository()->find($id);
544
        $objectManager = $jobManager->getObjectManager();
545
        $count = $jobManager->resetStalledJobs();
546
        self::assertEquals(0, $count);
547
548
        $objectManager->remove($job);
549
        $objectManager->flush();
550
551
        $id = $this->createStalledJob(true, false);
552
        $job = $jobManager->getRepository()->find($id);
553
        $job->setMaxRetries(10);
554
        $job->setRetries(10);
555
        $objectManager->persist($job);
556
        $objectManager->flush();
557
558
        $count = $jobManager->resetStalledJobs();
559
        self::assertEquals(0, $count);
560
        $job = $jobManager->getRepository()->find($id);
561
        self::assertNull($job);
562
        $job = $objectManager->getRepository($jobManager->getJobArchiveClass())->find($id);
563
        self::assertNotNull($job);
564
        $objectManager->remove($job);
565
        $objectManager->flush();
566
567
        $id = $this->createStalledJob(true, false);
568
        $job = $jobManager->getRepository()->find($id);
569
        $job->setMaxStalled(10);
570
        $job->setStalledCount(10);
571
        $objectManager->persist($job);
572
        $objectManager->flush();
573
574
        $count = $jobManager->resetStalledJobs();
575
        self::assertEquals(0, $count);
576
        $job = $jobManager->getRepository()->find($id);
577
        self::assertNull($job);
578
        $job = $objectManager->getRepository($jobManager->getJobArchiveClass())->find($id);
579
        self::assertNotNull($job);
580
        $objectManager->remove($job);
581
        $objectManager->flush();
582
    }
583
584
    public function testPruneErroneousJobs()
585
    {
586
        $job = $this->getJob();
587
        $id = $job->getId();
588
589
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
590
        $jobManager = self::$jobManager;
591
        $jobManager->deleteJob($job);
592
        $archiveObjectName = $jobManager->getJobArchiveClass();
593
594
        $objectManager = $jobManager->getObjectManager();
595
596
        $archiveRepository = $objectManager->getRepository($archiveObjectName);
597
        $result = $archiveRepository->find($id);
598
        self::assertNotNull($result);
599
        self::assertEquals($id, $result->getId());
600
601
        $result->setStatus(BaseJob::STATUS_ERROR);
602
        $result->setLocked(true);
603
        $result->setLockedAt(new \DateTime());
604
        $result->setFinishedAt(new \DateTime());
605
        $result->setElapsed(12345);
606
        $result->setMessage('soomething');
607
        $objectManager->persist($result);
608
        $objectManager->flush();
609
610
        $count = $jobManager->pruneErroneousJobs('asdf');
611
        self::assertEquals(0, $count);
612
        $count = $jobManager->pruneErroneousJobs(null, 'asdf');
613
        self::assertEquals(0, $count);
614
        $count = $jobManager->pruneErroneousJobs('fibonacci', 'asdf');
615
        self::assertEquals(0, $count);
616
        $count = $jobManager->pruneErroneousJobs('fibonacci', 'asdf');
617
        self::assertEquals(0, $count);
618
        $count = $jobManager->pruneErroneousJobs('fibonacci', 'fibonacci');
619
        self::assertEquals(1, $count);
620
        $repository = $jobManager->getRepository();
621
        $job = $repository->find($id);
622
        $objectManager->clear();
623
        self::assertNull($job);
624
        $archiveJob = $archiveRepository->find($id);
625
        self::assertNull($archiveJob);
626
627
        $job = $this->getJob();
628
        $id = $job->getId();
629
        $objectManager->remove($job);
630
        $objectManager->flush();
631
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
632
        $jobManager = self::$jobManager;
633
        $archiveObjectName = $jobManager->getJobArchiveClass();
634
635
        $objectManager = $jobManager->getObjectManager();
636
637
        $archiveRepository = $objectManager->getRepository($archiveObjectName);
638
        $result = $archiveRepository->find($id);
639
        self::assertNotNull($result);
640
        self::assertEquals($id, $result->getId());
641
642
        $result->setStatus(BaseJob::STATUS_ERROR);
643
        $result->setLocked(true);
644
        $result->setLockedAt(new \DateTime());
645
        $result->setFinishedAt(new \DateTime());
646
        $result->setElapsed(12345);
647
        $result->setMessage('soomething');
648
        $objectManager->persist($result);
649
        $objectManager->flush();
650
651
        $job = $this->getJob();
652
        $id = $job->getId();
653
        $objectManager->remove($job);
654
        $objectManager->flush();
655
656
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
657
        $jobManager = self::$jobManager;
658
        $archiveObjectName = $jobManager->getJobArchiveClass();
659
        $objectManager = $jobManager->getObjectManager();
660
661
        $archiveRepository = $objectManager->getRepository($archiveObjectName);
662
        $result = $archiveRepository->find($id);
663
        self::assertNotNull($result);
664
        self::assertEquals($id, $result->getId());
665
666
        $result->setStatus(BaseJob::STATUS_ERROR);
667
        $result->setLocked(true);
668
        $result->setLockedAt(new \DateTime());
669
        $result->setFinishedAt(new \DateTime());
670
        $result->setElapsed(12345);
671
        $result->setMessage('soomething');
672
        $objectManager->persist($result);
673
        $objectManager->flush();
674
        $count = $jobManager->pruneErroneousJobs();
675
        self::assertEquals(2, $count);
676
    }
677
678
    public function testPruneStalledJobs()
679
    {
680
        static::setUpBeforeClass();
681
682
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
683
        $jobManager = self::$jobManager;
684
685
        $job = new self::$jobClass(self::$worker, false, null);
686
        $job->fibonacci(1);
687
        self::assertNotNull($job->getId(), 'Job id should be generated');
688
        $job->setStatus(BaseJob::STATUS_RUNNING);
689
        $job->setLocked(true);
690
        $time = time();
691
        $date = new \DateTime("@$time");
692
        $job->setLockedAt($date);
693
        $id = $job->getId();
694
        $job = $jobManager->getRepository()->find($id);
695
696
        self::assertNotNull($job);
697
698
        $runClass = self::$runManager->getRunClass();
699
700
        $objectManager = $jobManager->getObjectManager();
701
        $run = new $runClass();
702
        $run->setLastHeartbeatAt(new \DateTime());
703
        $objectManager->persist($run);
704
        $objectManager->flush();
705
        $runId = $run->getId();
706
        self::assertNotNull($runId);
707
        $job->setRunId($runId);
708
        $objectManager->persist($job);
709
        $objectManager->flush();
710
        $objectManager->remove($run);
711
        $objectManager->flush();
712
        $id = $job->getId();
713
        $job = $jobManager->getRepository()->find($id);
714
715
        self::assertNotNull($job);
716
717
        $archivedRun = $objectManager->getRepository(self::$runManager->getRunArchiveClass())->find($runId);
718
719
        $minusTime = $time - (BaseJobManager::STALLED_SECONDS + 1);
720
        $archivedRun->setEndedAt(new \DateTime("@$minusTime"));
721
722
        $objectManager->persist($archivedRun);
723
        $objectManager->flush();
724
725
        $count = $jobManager->pruneStalledJobs('asdf');
726
        self::assertEquals(0, $count);
727
        $count = $jobManager->pruneStalledJobs(null, 'asdf');
728
        self::assertEquals(0, $count);
729
        $count = $jobManager->pruneStalledJobs('fibonacci', 'asdf');
730
        self::assertEquals(0, $count);
731
        $count = $jobManager->pruneStalledJobs('fibonacci', 'fibonacci');
732
        self::assertEquals(1, $count);
733
734
        $job = $jobManager->getRepository()->find($id);
735
736
        self::assertNull($job);
737
738
        $archivedJob = $jobManager->getObjectManager()->getRepository($jobManager->getJobArchiveClass())->find($id);
739
740
        self::assertNotNull($archivedJob);
741
        self::assertEquals(BaseJob::STATUS_ERROR, $archivedJob->getStatus());
742
        self::assertEquals(1, $archivedJob->getStalledCount());
743
        $objectManager->remove($archivedJob);
744
        $objectManager->flush();
745
746
        // multiple
747
748
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
749
        $jobManager = self::$jobManager;
750
751
        $job = new self::$jobClass(self::$worker, false, null);
752
        $job->fibonacci(1);
753
        self::assertNotNull($job->getId(), 'Job id should be generated');
754
        $job->setStatus(BaseJob::STATUS_RUNNING);
755
        $job->setLocked(true);
756
        $time = time();
757
        $date = new \DateTime("@$time");
758
        $job->setLockedAt($date);
759
        $id = $job->getId();
760
        $job = $jobManager->getRepository()->find($id);
761
762
        self::assertNotNull($job);
763
764
        $runClass = self::$runManager->getRunClass();
765
766
        $objectManager = $jobManager->getObjectManager();
767
        $run = new $runClass();
768
        $run->setLastHeartbeatAt(new \DateTime());
769
        $objectManager->persist($run);
770
        $objectManager->flush();
771
        $runId = $run->getId();
772
        self::assertNotNull($runId);
773
        $job->setRunId($runId);
774
        $objectManager->persist($job);
775
        $objectManager->flush();
776
        $objectManager->remove($run);
777
        $objectManager->flush();
778
        $id = $job->getId();
779
        $job = $jobManager->getRepository()->find($id);
780
781
        self::assertNotNull($job);
782
783
        $archivedRun = $objectManager->getRepository(self::$runManager->getRunArchiveClass())->find($runId);
784
785
        $minusTime = $time - (BaseJobManager::STALLED_SECONDS + 1);
786
        $archivedRun->setEndedAt(new \DateTime("@$minusTime"));
787
788
        $objectManager->persist($archivedRun);
789
        $objectManager->flush();
790
791
        $job = new self::$jobClass(self::$worker, false, null);
792
        $job->fibonacci(1);
793
        self::assertNotNull($job->getId(), 'Job id should be generated');
794
        $job->setStatus(BaseJob::STATUS_RUNNING);
795
        $job->setLocked(true);
796
        $time = time();
797
        $date = new \DateTime("@$time");
798
        $job->setLockedAt($date);
799
        $id = $job->getId();
800
        $job = $jobManager->getRepository()->find($id);
801
802
        self::assertNotNull($job);
803
804
        $runClass = self::$runManager->getRunClass();
805
806
        $objectManager = $jobManager->getObjectManager();
807
        $run = new $runClass();
808
        $run->setLastHeartbeatAt(new \DateTime());
809
        $objectManager->persist($run);
810
        $objectManager->flush();
811
        $runId = $run->getId();
812
        self::assertNotNull($runId);
813
        $job->setRunId($runId);
814
        $objectManager->persist($job);
815
        $objectManager->flush();
816
        $objectManager->remove($run);
817
        $objectManager->flush();
818
        $id = $job->getId();
819
        $job = $jobManager->getRepository()->find($id);
820
821
        self::assertNotNull($job);
822
823
        $archivedRun = $objectManager->getRepository(self::$runManager->getRunArchiveClass())->find($runId);
824
825
        $minusTime = $time - (BaseJobManager::STALLED_SECONDS + 1);
826
        $archivedRun->setEndedAt(new \DateTime("@$minusTime"));
827
828
        $objectManager->persist($archivedRun);
829
        $objectManager->flush();
830
        $count = $jobManager->pruneStalledJobs();
831
        self::assertEquals(2, $count);
832
    }
833
834
    public function testBatchJobs()
835
    {
836
        $jobs = self::$jobManager->getRepository()->findAll();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getRepository() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
837
        foreach ($jobs as $job) {
838
            self::$jobManager->getObjectManager()->remove($job);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
839
        }
840
        self::$jobManager->getObjectManager()->flush();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
841
        self::$jobManager->getObjectManager()->clear();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
842
843
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
844
        $worker = self::$worker;
845
        $job1 = $worker->later()->fibonacci(1);
846
        $job2 = $worker->batchLater()->fibonacci(1);
847
        self::assertEquals($job1, $job2);
848
849
        $jobs = self::$jobManager->getRepository()->findAll();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getRepository() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
850
        self::assertCount(1, $jobs);
851
        self::assertEquals($job1, $jobs[0]);
852
        self::assertNull($jobs[0]->getPriority());
853
        self::$jobManager->getObjectManager()->remove($jobs[0]);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
854
        self::$jobManager->getObjectManager()->flush();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
855
        self::$jobManager->getObjectManager()->clear();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
856
857
        $job1 = $worker->later()->fibonacci(1);
858
        self::assertNull($job1->getPriority());
859
        $job2 = $worker->batchLater()->setPriority(3)->fibonacci(1);
860
        self::assertEquals($job1, $job2);
861
        self::assertNotNull($job2->getPriority());
862
863
        $jobs = self::$jobManager->getRepository()->findAll();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getRepository() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
864
        self::assertCount(1, $jobs);
865
        self::assertEquals($job1, $jobs[0]);
866
        self::assertNotNull($jobs[0]->getPriority());
867
868
        // Not
869
        $jobs = self::$jobManager->getRepository()->findAll();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getRepository() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
870
        foreach ($jobs as $job) {
871
            self::$jobManager->getObjectManager()->remove($job);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
872
        }
873
        self::$jobManager->getObjectManager()->remove($jobs[0]);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
874
        self::$jobManager->getObjectManager()->flush();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
875
        self::$jobManager->getObjectManager()->clear();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
876
877
        $job1 = $worker->later(100)->fibonacci(1);
878
879
        $time1 = new \DateTime('@'.time());
880
        $job2 = $worker->batchLater(0)->fibonacci(1);
881
        $time2 = new \DateTime();
882
883
        self::assertEquals($job1, $job2);
884
        self::assertGreaterThanOrEqual($time1, $job2->getWhenAt());
885
        self::assertLessThanOrEqual($time2, $job2->getWhenAt());
886
887
        $jobs = self::$jobManager->getRepository()->findAll();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getRepository() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
888
        self::assertCount(1, $jobs);
889
        self::assertEquals($job1, $jobs[0]);
890
        self::assertGreaterThanOrEqual($time1, $jobs[0]->getWhenAt());
891
        self::assertLessThanOrEqual($time2, $jobs[0]->getWhenAt());
892
        self::$jobManager->getObjectManager()->remove($jobs[0]);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
893
        self::$jobManager->getObjectManager()->flush();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
894
        self::$jobManager->getObjectManager()->clear();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
895
896
        $job1 = $worker->later(100)->setPriority(3)->fibonacci(1);
897
        $priority1 = $job1->getPriority();
898
        $time1 = new \DateTime('@'.time());
899
        $job2 = $worker->batchLater(0)->setPriority(1)->fibonacci(1);
900
        $time2 = new \DateTime();
901
        self::assertEquals($job1, $job2);
902
        self::assertNotEquals($priority1, $job2->getPriority());
903
904
        self::assertEquals($job1, $job2);
905
        self::assertGreaterThanOrEqual($time1, $job2->getWhenAt());
906
        self::assertLessThanOrEqual($time2, $job2->getWhenAt());
907
    }
908
909
    public function testPruneExpiredJobs()
910
    {
911
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
912
        $jobManager = self::$jobManager;
913
        $objectManager = $jobManager->getObjectManager();
914
915
        $job = new self::$jobClass(self::$worker, false, null);
916
        $job->fibonacci(1);
917
        self::assertNotNull($job->getId(), 'Job id should be generated');
918
        $time = time() - 1;
919
        $date = new \DateTime("@$time");
920
        $job->setExpiresAt($date);
921
        $objectManager->persist($job);
922
        $objectManager->flush();
923
924
        $count = $jobManager->pruneExpiredJobs('asdf');
925
        self::assertEquals(0, $count);
926
        $count = $jobManager->pruneExpiredJobs(null, 'asdf');
927
        self::assertEquals(0, $count);
928
        $count = $jobManager->pruneExpiredJobs(null, 'fibonacci');
929
        self::assertEquals(1, $count);
930
931
        $job = new self::$jobClass(self::$worker, false, null);
932
        $job->fibonacci(1);
933
        self::assertNotNull($job->getId(), 'Job id should be generated');
934
        $time = time() - 1;
935
        $date = new \DateTime("@$time");
936
        $job->setExpiresAt($date);
937
        $objectManager->persist($job);
938
        $objectManager->flush();
939
940
        $job = new self::$jobClass(self::$worker, false, null);
941
        $job->fibonacci(1);
942
        self::assertNotNull($job->getId(), 'Job id should be generated');
943
        $time = time() - 1;
944
        $date = new \DateTime("@$time");
945
        $job->setExpiresAt($date);
946
        $objectManager->persist($job);
947
        $objectManager->flush();
948
949
        $count = $jobManager->pruneExpiredJobs(null, 'fibonacci');
950
        self::assertEquals(2, $count);
951
952
        $job = new self::$jobClass(self::$worker, false, null);
953
        $job->fibonacci(1);
954
        self::assertNotNull($job->getId(), 'Job id should be generated');
955
        $time = time() - 1;
956
        $date = new \DateTime("@$time");
957
        $job->setExpiresAt($date);
958
        $objectManager->persist($job);
959
        $objectManager->flush();
960
961
        $job = new self::$jobClass(self::$worker, false, null);
962
        $job->fibonacci(1);
963
        self::assertNotNull($job->getId(), 'Job id should be generated');
964
        $time = time() - 1;
965
        $date = new \DateTime("@$time");
966
        $job->setExpiresAt($date);
967
        $objectManager->persist($job);
968
        $objectManager->flush();
969
970
        $count = $jobManager->pruneExpiredJobs('fibonacci', 'fibonacci');
971
        self::assertEquals(2, $count);
972
973
        $job = new self::$jobClass(self::$worker, false, null);
974
        $job->fibonacci(1);
975
        self::assertNotNull($job->getId(), 'Job id should be generated');
976
        $time = time() - 1;
977
        $date = new \DateTime("@$time");
978
        $job->setExpiresAt($date);
979
        $objectManager->persist($job);
980
        $objectManager->flush();
981
982
        $job = new self::$jobClass(self::$worker, false, null);
983
        $job->fibonacci(1);
984
        self::assertNotNull($job->getId(), 'Job id should be generated');
985
        $time = time() - 1;
986
        $date = new \DateTime("@$time");
987
        $job->setExpiresAt($date);
988
        $objectManager->persist($job);
989
        $objectManager->flush();
990
991
        $count = $jobManager->pruneExpiredJobs('fibonacci');
992
        self::assertEquals(2, $count);
993
994
        $job = new self::$jobClass(self::$worker, false, null);
995
        $job->fibonacci(1);
996
        self::assertNotNull($job->getId(), 'Job id should be generated');
997
        $time = time() - 1;
998
        $date = new \DateTime("@$time");
999
        $job->setExpiresAt($date);
1000
        $objectManager->persist($job);
1001
        $objectManager->flush();
1002
1003
        $jobId1 = $job->getId();
1004
1005
        $job = new self::$jobClass(self::$worker, false, null);
1006
        $job->fibonacci(1);
1007
        self::assertNotNull($job->getId(), 'Job id should be generated');
1008
        $time = time() - 1;
1009
        $date = new \DateTime("@$time");
1010
        $job->setExpiresAt($date);
1011
        $objectManager->persist($job);
1012
        $objectManager->flush();
1013
1014
        $jobId2 = $job->getId();
1015
1016
        $count = $jobManager->pruneExpiredJobs();
1017
        self::assertEquals(2, $count);
1018
1019
        $archiveRepository = $jobManager->getObjectManager()->getRepository($jobManager->getJobArchiveClass());
1020
1021
        $job = $archiveRepository->find($jobId1);
1022
        self::assertNotNull($job);
1023
        self::assertEquals(Job::STATUS_EXPIRED, $job->getStatus());
1024
1025
        $job = $archiveRepository->find($jobId2);
1026
        self::assertNotNull($job);
1027
        self::assertEquals(Job::STATUS_EXPIRED, $job->getStatus());
1028
    }
1029
1030
    public function testPruneArchivedJobs()
1031
    {
1032
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
1033
        $jobManager = self::$jobManager;
1034
        $objectManager = $jobManager->getObjectManager();
1035
        $jobArchiveClass = $jobManager->getJobArchiveClass();
1036
        $jobArchiveRepository = $objectManager->getRepository($jobArchiveClass);
1037
1038
        self::$objectManager->getEventManager()->removeEventListener('preUpdate', self::$dtcQueueListener);
1039
1040
        $job = new self::$jobClass(self::$worker, false, null);
1041
        $job->fibonacci(1);
1042
        $id = $job->getId();
1043
        $objectManager->remove($job);
1044
        $objectManager->flush();
1045
1046
        $jobArchive = $jobArchiveRepository->find($id);
1047
        self::assertNotNull($jobArchive);
1048
        $time = time() - 86401;
1049
        $jobArchive->setUpdatedAt(new \DateTime("@$time"));
1050
        $objectManager->persist($jobArchive);
1051
        $objectManager->flush();
1052
1053
        $older = $time + 1;
1054
        $count = $jobManager->pruneArchivedJobs(new \DateTime("@$time"));
1055
        self::assertEquals(0, $count);
1056
        $count = $jobManager->pruneArchivedJobs(new \DateTime("@$older"));
1057
        self::assertEquals(1, $count);
1058
1059
        $job = new self::$jobClass(self::$worker, false, null);
1060
        $job->fibonacci(1);
1061
        $id = $job->getId();
1062
        $objectManager->remove($job);
1063
        $objectManager->flush();
1064
1065
        $jobArchive = $jobArchiveRepository->find($id);
1066
        self::assertNotNull($jobArchive);
1067
        $time = time() - 86401;
1068
        $jobArchive->setUpdatedAt(new \DateTime("@$time"));
1069
        $objectManager->persist($jobArchive);
1070
        $objectManager->flush();
1071
1072
        $job = new self::$jobClass(self::$worker, false, null);
1073
        $job->fibonacci(1);
1074
        $id = $job->getId();
1075
        $objectManager->remove($job);
1076
        $objectManager->flush();
1077
1078
        $jobArchive = $jobArchiveRepository->find($id);
1079
        self::assertNotNull($jobArchive);
1080
        $jobArchive->setUpdatedAt(new \DateTime("@$time"));
1081
        $objectManager->persist($jobArchive);
1082
        $objectManager->flush();
1083
        $older = $time + 1;
1084
        $count = $jobManager->pruneArchivedJobs(new \DateTime("@$time"));
1085
        self::assertEquals(0, $count);
1086
        $count = $jobManager->pruneArchivedJobs(new \DateTime("@$older"));
1087
        self::assertEquals(2, $count);
1088
1089
        self::$objectManager->getEventManager()->addEventListener('preUpdate', self::$dtcQueueListener);
1090
    }
1091
1092
    public function testPerformance()
1093
    {
1094
        $jobs = self::$jobManager->getRepository()->findAll();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getRepository() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
1095
        foreach ($jobs as $job) {
1096
            self::$jobManager->getObjectManager()->remove($job);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
1097
        }
1098
        self::$jobManager->getObjectManager()->flush();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
1099
1100
        self::$jobManager->getObjectManager()->clear();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
1101
        parent::testPerformance();
1102
    }
1103
1104
    protected function getBaseStatus()
1105
    {
1106
        /** @var BaseJobManager $jobManager */
1107
        $jobManager = self::$jobManager;
1108
        $job = new self::$jobClass(self::$worker, false, null);
1109
        $job->fibonacci(1);
1110
        $status = $jobManager->getStatus();
1111
        self::assertArrayHasKey('fibonacci->fibonacci()', $status);
1112
        $fibonacciStatus = $status['fibonacci->fibonacci()'];
1113
1114
        self::assertArrayHasKey(BaseJob::STATUS_NEW, $fibonacciStatus);
1115
        self::assertArrayHasKey(BaseJob::STATUS_ERROR, $fibonacciStatus);
1116
        self::assertArrayHasKey(BaseJob::STATUS_RUNNING, $fibonacciStatus);
1117
        self::assertArrayHasKey(BaseJob::STATUS_SUCCESS, $fibonacciStatus);
1118
        self::assertArrayHasKey(RetryableJob::STATUS_MAX_STALLED, $fibonacciStatus);
1119
        self::assertArrayHasKey(RetryableJob::STATUS_MAX_ERROR, $fibonacciStatus);
1120
        self::assertArrayHasKey(RetryableJob::STATUS_MAX_RETRIES, $fibonacciStatus);
1121
        self::assertArrayHasKey(RetryableJob::STATUS_EXPIRED, $fibonacciStatus);
1122
1123
        return [$job, $status];
1124
    }
1125
1126
    public function testGetStatus()
1127
    {
1128
        list($job1, $status1) = $this->getBaseStatus();
1129
        list($job2, $status2) = $this->getBaseStatus();
1130
        $fibonacciStatus1 = $status1['fibonacci->fibonacci()'];
1131
        $fibonacciStatus2 = $status2['fibonacci->fibonacci()'];
1132
1133
        self::assertEquals($fibonacciStatus1[BaseJob::STATUS_NEW] + 1, $fibonacciStatus2[BaseJob::STATUS_NEW]);
1134
        $jobManager = self::$jobManager;
1135
        $objectManager = $jobManager->getObjectManager();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Model\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\BaseJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
1136
        $objectManager->remove($job1);
1137
        $objectManager->remove($job2);
1138
    }
1139
}
1140