1<?php
2
3use MediaWiki\MediaWikiServices;
4
5/**
6 * @covers JobQueueMemory
7 *
8 * @group JobQueue
9 *
10 * @license GPL-2.0-or-later
11 * @author Thiemo Kreuz
12 */
13class JobQueueMemoryTest extends PHPUnit\Framework\TestCase {
14
15	use MediaWikiCoversValidator;
16
17	/**
18	 * @return JobQueueMemory
19	 */
20	private function newJobQueue() {
21		$services = MediaWikiServices::getInstance();
22
23		return JobQueue::factory( [
24			'class' => JobQueueMemory::class,
25			'domain' => WikiMap::getCurrentWikiDbDomain()->getId(),
26			'type' => 'null',
27			'idGenerator' => $services->getGlobalIdGenerator(),
28		] );
29	}
30
31	private function newJobSpecification() {
32		return new JobSpecification(
33			'null',
34			[ 'customParameter' => null ],
35			[],
36			Title::newFromText( 'Custom title' )
37		);
38	}
39
40	public function testGetAllQueuedJobs() {
41		$queue = $this->newJobQueue();
42		$this->assertCount( 0, $queue->getAllQueuedJobs() );
43
44		$queue->push( $this->newJobSpecification() );
45		$this->assertCount( 1, $queue->getAllQueuedJobs() );
46	}
47
48	public function testGetAllAcquiredJobs() {
49		$queue = $this->newJobQueue();
50		$this->assertCount( 0, $queue->getAllAcquiredJobs() );
51
52		$queue->push( $this->newJobSpecification() );
53		$this->assertCount( 0, $queue->getAllAcquiredJobs() );
54
55		$queue->pop();
56		$this->assertCount( 1, $queue->getAllAcquiredJobs() );
57	}
58
59	public function testJobFromSpecInternal() {
60		$queue = $this->newJobQueue();
61		$job = $queue->jobFromSpecInternal( $this->newJobSpecification() );
62		$this->assertInstanceOf( Job::class, $job );
63		$this->assertSame( 'null', $job->getType() );
64		$this->assertArrayHasKey( 'customParameter', $job->getParams() );
65		$this->assertSame( 'Custom title', $job->getTitle()->getText() );
66	}
67
68}
69