Merge "mw.rcfilters.ui.SaveFiltersPopupButtonWidget: Remove pointless option"
[lhc/web/wiklou.git] / tests / phpunit / includes / jobqueue / JobTest.php
1 <?php
2
3 /**
4 * @author Addshore
5 */
6 class JobTest extends MediaWikiTestCase {
7
8 /**
9 * @dataProvider provideTestToString
10 *
11 * @param Job $job
12 * @param string $expected
13 *
14 * @covers Job::toString
15 */
16 public function testToString( $job, $expected ) {
17 $this->assertEquals( $expected, $job->toString() );
18 }
19
20 public function provideTestToString() {
21 $mockToStringObj = $this->getMockBuilder( 'stdClass' )
22 ->setMethods( [ '__toString' ] )->getMock();
23 $mockToStringObj->expects( $this->any() )
24 ->method( '__toString' )
25 ->will( $this->returnValue( '{STRING_OBJ_VAL}' ) );
26
27 $requestId = 'requestId=' . WebRequest::getRequestId();
28
29 return [
30 [
31 $this->getMockJob( false ),
32 'someCommand ' . $requestId
33 ],
34 [
35 $this->getMockJob( [ 'key' => 'val' ] ),
36 'someCommand key=val ' . $requestId
37 ],
38 [
39 $this->getMockJob( [ 'key' => [ 'inkey' => 'inval' ] ] ),
40 'someCommand key={"inkey":"inval"} ' . $requestId
41 ],
42 [
43 $this->getMockJob( [ 'val1' ] ),
44 'someCommand 0=val1 ' . $requestId
45 ],
46 [
47 $this->getMockJob( [ 'val1', 'val2' ] ),
48 'someCommand 0=val1 1=val2 ' . $requestId
49 ],
50 [
51 $this->getMockJob( [ new stdClass() ] ),
52 'someCommand 0=object(stdClass) ' . $requestId
53 ],
54 [
55 $this->getMockJob( [ $mockToStringObj ] ),
56 'someCommand 0={STRING_OBJ_VAL} ' . $requestId
57 ],
58 [
59 $this->getMockJob( [
60 "pages" => [
61 "932737" => [
62 0,
63 "Robert_James_Waller"
64 ]
65 ],
66 "rootJobSignature" => "45868e99bba89064e4483743ebb9b682ef95c1a7",
67 "rootJobTimestamp" => "20160309110158",
68 "masterPos" => [
69 "file" => "db1023-bin.001288",
70 "pos" => "308257743",
71 "asOfTime" => 1457521464.3814
72 ],
73 "triggeredRecursive" => true
74 ] ),
75 'someCommand pages={"932737":[0,"Robert_James_Waller"]} ' .
76 'rootJobSignature=45868e99bba89064e4483743ebb9b682ef95c1a7 ' .
77 'rootJobTimestamp=20160309110158 masterPos=' .
78 '{"file":"db1023-bin.001288","pos":"308257743","asOfTime":' .
79 // Embed dynamically because TestSetup sets serialize_precision=17
80 // which, in PHP 7.1 and 7.2, produces 1457521464.3814001 instead
81 json_encode( 1457521464.3814 ) . '} ' . 'triggeredRecursive=1 ' .
82 $requestId
83 ],
84 ];
85 }
86
87 public function getMockJob( $params ) {
88 $mock = $this->getMockForAbstractClass(
89 'Job',
90 [ 'someCommand', new Title(), $params ],
91 'SomeJob'
92 );
93 return $mock;
94 }
95
96 /**
97 * @dataProvider provideTestJobFactory
98 *
99 * @param mixed $handler
100 *
101 * @covers Job::factory
102 */
103 public function testJobFactory( $handler ) {
104 $this->mergeMwGlobalArrayValue( 'wgJobClasses', [ 'testdummy' => $handler ] );
105
106 $job = Job::factory( 'testdummy', Title::newMainPage(), [] );
107 $this->assertInstanceOf( NullJob::class, $job );
108
109 $job2 = Job::factory( 'testdummy', Title::newMainPage(), [] );
110 $this->assertInstanceOf( NullJob::class, $job2 );
111 $this->assertNotSame( $job, $job2, 'should not reuse instance' );
112 }
113
114 public function provideTestJobFactory() {
115 return [
116 'class name' => [ 'NullJob' ],
117 'closure' => [ function ( Title $title, array $params ) {
118 return new NullJob( $title, $params );
119 } ],
120 'function' => [ [ $this, 'newNullJob' ] ],
121 'static function' => [ self::class . '::staticNullJob' ]
122 ];
123 }
124
125 public function newNullJob( Title $title, array $params ) {
126 return new NullJob( $title, $params );
127 }
128
129 public static function staticNullJob( Title $title, array $params ) {
130 return new NullJob( $title, $params );
131 }
132
133 }