JobTest.php 3.5 KB

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