/** * @param array $jobs * @return void */ public function addJobs(array $jobs) { foreach ($jobs as $job) { $this->validateJobDeclaration($job); $queue = json_decode($this->reader->read(), true); $queue[self::KEY_JOBS][] = $job; $this->writer->write(json_encode($queue, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); } }
/** * Pop all updater application queued jobs. * * Note, that this method is not idempotent, queue will be cleared after its execution * * @return AbstractJob[] * @throws \RuntimeException */ public function popQueuedJobs() { $jobs = []; $queue = json_decode($this->reader->read(), true); if (!is_array($queue)) { return $jobs; } if (isset($queue[self::KEY_JOBS]) && is_array($queue[self::KEY_JOBS])) { /** @var object $job */ foreach ($queue[self::KEY_JOBS] as $job) { $this->validateJobDeclaration($job); $jobs[] = $this->jobFactory->create($job[self::KEY_JOB_NAME], $job[self::KEY_JOB_PARAMS]); } } else { throw new \RuntimeException(sprintf('"%s" field is missing or is not an array.', self::KEY_JOBS)); } $this->reader->clearQueue(); return $jobs; }
public function testAddJobs() { $queue = ['jobs' => []]; $this->reader->expects($this->at(0))->method('read')->willReturn(''); $queue['jobs'][] = ['name' => 'job A', 'params' => []]; $this->writer->expects($this->at(0))->method('write')->with(json_encode($queue, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); $this->reader->expects($this->at(1))->method('read')->willReturn(json_encode($queue, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); $queue['jobs'][] = ['name' => 'job B', 'params' => []]; $this->writer->expects($this->at(1))->method('write')->with(json_encode($queue, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); $this->queue->addJobs([['name' => 'job A', 'params' => []], ['name' => 'job B', 'params' => []]]); }
/** * @dataProvider popQueueJobInvalidQueueFormatProvider * @param string $queueJson * @param string $expectedExceptionMessage */ public function testPopQueueJobInvalidQueueFormat($queueJson, $expectedExceptionMessage) { $this->setExpectedException('\\RuntimeException', $expectedExceptionMessage); $this->queueReaderMock->expects($this->once())->method('read')->willReturn($queueJson); $this->queue->popQueuedJobs(); }