public function testPopQueuedJobEmptyAfter() { $this->reader->expects($this->once())->method('read')->willReturn('{"jobs": [{"name": "job A", "params" : []}]}'); $job = $this->getMockForAbstractClass('Magento\\Update\\Queue\\AbstractJob', [], '', false); $this->jobFactory->expects($this->once())->method('create')->with('job A', [])->willReturn($job); $this->writer->expects($this->once())->method('write')->with(''); $this->assertEquals($job, $this->queue->popQueuedJob()); }
public function testPopQueueJob() { $queueJson = file_get_contents(__DIR__ . '/_files/update_queue_valid.json'); $this->queueReaderMock->expects($this->once())->method('read')->willReturn($queueJson); $jobMock = $this->getMockBuilder('Magento\\Update\\Queue\\AbstractJob')->disableOriginalConstructor()->getMockForAbstractClass(); $this->jobFactoryMock->expects($this->exactly(4))->method('create')->willReturn($jobMock); /** Ensure that arguments are passed correctly to the job factory */ $this->jobFactoryMock->expects($this->at(0))->method('create')->with('backup', []); $this->queueReaderMock->expects($this->once())->method('clearQueue'); $jobs = $this->queue->popQueuedJobs(); $this->assertCount(4, $jobs); $this->assertInternalType('array', $jobs); $this->assertSame($jobMock, $jobs[3]); }
/** * 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; }
/** * Pop job queue. * * @return AbstractJob * @throws \RuntimeException */ public function popQueuedJob() { $job = null; $queue = json_decode($this->reader->read(), true); if (!is_array($queue)) { return $job; } if (isset($queue[self::KEY_JOBS]) && is_array($queue[self::KEY_JOBS])) { $this->validateJobDeclaration($queue[self::KEY_JOBS][0]); $job = $this->jobFactory->create($queue[self::KEY_JOBS][0][self::KEY_JOB_NAME], $queue[self::KEY_JOBS][0][self::KEY_JOB_PARAMS]); array_shift($queue[self::KEY_JOBS]); if (empty($queue[self::KEY_JOBS])) { $this->writer->write(''); } else { $this->writer->write(json_encode($queue, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); } } else { throw new \RuntimeException(sprintf('"%s" field is missing or is not an array.', self::KEY_JOBS)); } return $job; }
public function testCreateInvalidJob() { $this->setExpectedException('\\RuntimeException', '"invalid" job is not supported.'); $this->jobFactory->create('invalid', []); }