|
2 | 2 |
|
3 | 3 | namespace Markup\Bundle\JobQueueBundle\Tests\Service;
|
4 | 4 |
|
| 5 | +use Doctrine\Common\Persistence\ManagerRegistry; |
5 | 6 | use Markup\JobQueueBundle\Job\SleepJob;
|
| 7 | +use Markup\JobQueueBundle\Model\Job; |
6 | 8 | use Markup\JobQueueBundle\Publisher\JobPublisher;
|
| 9 | +use Markup\JobQueueBundle\Repository\JobLogRepository; |
7 | 10 | use Markup\JobQueueBundle\Service\JobManager;
|
8 | 11 | use Markup\JobQueueBundle\Service\ScheduledJobService;
|
9 | 12 | use Mockery as m;
|
|
12 | 15 |
|
13 | 16 | class JobManagerTest extends MockeryTestCase
|
14 | 17 | {
|
| 18 | + /** |
| 19 | + * @var JobPublisher |
| 20 | + */ |
| 21 | + private $jobPublisher; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var ScheduledJobService |
| 25 | + */ |
| 26 | + private $scheduledJobService; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var JobManager |
| 30 | + */ |
| 31 | + private $jobManager; |
| 32 | + |
15 | 33 | protected function setUp()
|
16 | 34 | {
|
17 |
| - $jobPublisher = m::mock(JobPublisher::class); |
18 |
| - $scheduledJob = m::mock(ScheduledJobService::class); |
19 |
| - $jobPublisher->shouldReceive('publish')->andReturn(null); |
20 |
| - $scheduledJob->shouldReceive('addScheduledJob')->andReturn(null); |
21 |
| - $this->jobManager = new JobManager($jobPublisher, $scheduledJob); |
| 35 | + $this->jobPublisher = $this->createStoringJobPublisher(); |
| 36 | + $this->scheduledJobService = $this->createStoringJobScheduler(); |
| 37 | + $this->jobManager = new JobManager( |
| 38 | + $this->jobPublisher, |
| 39 | + $this->scheduledJobService |
| 40 | + ); |
22 | 41 | }
|
23 | 42 |
|
24 | 43 | public function testCanAddJob()
|
25 | 44 | {
|
26 | 45 | $job = new SleepJob();
|
27 | 46 | $scheduledTime = new \DateTime();
|
28 |
| - $this->assertNull($this->jobManager->addJob($job)); |
29 |
| - $this->assertNull($this->jobManager->addJob($job, $scheduledTime)); |
| 47 | + $this->jobManager->addJob($job); |
| 48 | + $this->jobManager->addJob($job, $scheduledTime); |
| 49 | + $this->assertSame([$job], $this->jobPublisher->getJobs()); |
| 50 | + $this->assertSame([$job], $this->scheduledJobService->getJobs()); |
30 | 51 | }
|
31 | 52 |
|
32 |
| - public function testDoesNotAcceptBadJobs() |
| 53 | + public function testCanAddCommandJob() |
33 | 54 | {
|
34 |
| - $badjob = 'console:herp:derp'; |
35 |
| - $exceptionThrown = false; |
36 |
| - try { |
37 |
| - $this->jobManager->addJob($badjob); |
38 |
| - } catch (Error $e) { |
39 |
| - $exceptionThrown = true; |
40 |
| - } catch (\TypeError $e) { |
41 |
| - $exceptionThrown = true; |
42 |
| - } |
43 |
| - if (!$exceptionThrown) { |
44 |
| - $this->fail(); |
45 |
| - } |
| 55 | + $this->jobManager->addCommandJob('console:herp:derp', 'system', 60, 60); |
| 56 | + $this->assertCount(1, $this->jobPublisher->getJobs()); |
46 | 57 | }
|
47 | 58 |
|
48 |
| - public function testCanAddCommandJob() |
| 59 | + public function testIdleTimeoutDefaultsToTimeout() |
| 60 | + { |
| 61 | + $timeout = 720; |
| 62 | + $this->jobManager->addCommandJob('command', 'topic', $timeout); |
| 63 | + /** @var Job $job */ |
| 64 | + $job = $this->jobPublisher->getJobs()[0]; |
| 65 | + $this->assertEquals($timeout, $job->getArgs()['idleTimeout']); |
| 66 | + } |
| 67 | + |
| 68 | + private function createStoringJobPublisher(): JobPublisher |
| 69 | + { |
| 70 | + return new class () extends JobPublisher { |
| 71 | + use JobStore; |
| 72 | + |
| 73 | + public function __construct() |
| 74 | + { |
| 75 | + parent::__construct(m::mock(JobLogRepository::class)); |
| 76 | + $this->initializeJobs(); |
| 77 | + } |
| 78 | + |
| 79 | + public function publish(Job $job) |
| 80 | + { |
| 81 | + $this->addJob($job); |
| 82 | + } |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + private function createStoringJobScheduler() |
| 87 | + { |
| 88 | + return new class () extends ScheduledJobService { |
| 89 | + use JobStore; |
| 90 | + |
| 91 | + public function __construct() |
| 92 | + { |
| 93 | + parent::__construct(m::mock(ManagerRegistry::class)); |
| 94 | + $this->initializeJobs(); |
| 95 | + } |
| 96 | + |
| 97 | + public function addScheduledJob(Job $job, $scheduledTime) |
| 98 | + { |
| 99 | + $this->addJob($job); |
| 100 | + } |
| 101 | + }; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +trait JobStore { |
| 106 | + /** |
| 107 | + * @var array<Job> |
| 108 | + */ |
| 109 | + private $jobs; |
| 110 | + |
| 111 | + private function initializeJobs(): void |
| 112 | + { |
| 113 | + $this->jobs = []; |
| 114 | + } |
| 115 | + |
| 116 | + private function addJob(Job $job): void |
| 117 | + { |
| 118 | + $this->jobs[] = $job; |
| 119 | + } |
| 120 | + |
| 121 | + public function getJobs(): array |
49 | 122 | {
|
50 |
| - $this->assertNull($this->jobManager->addCommandJob('console:herp:derp', 'system', 60, 60)); |
| 123 | + return $this->jobs; |
51 | 124 | }
|
52 | 125 | }
|
0 commit comments