ArrayDiffFormatterTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * @author Addshore
  4. *
  5. * @group Diff
  6. */
  7. class ArrayDiffFormatterTest extends MediaWikiTestCase {
  8. /**
  9. * @param Diff $input
  10. * @param array $expectedOutput
  11. * @dataProvider provideTestFormat
  12. * @covers ArrayDiffFormatter::format
  13. */
  14. public function testFormat( $input, $expectedOutput ) {
  15. $instance = new ArrayDiffFormatter();
  16. $output = $instance->format( $input );
  17. $this->assertEquals( $expectedOutput, $output );
  18. }
  19. private function getMockDiff( $edits ) {
  20. $diff = $this->getMockBuilder( Diff::class )
  21. ->disableOriginalConstructor()
  22. ->getMock();
  23. $diff->expects( $this->any() )
  24. ->method( 'getEdits' )
  25. ->will( $this->returnValue( $edits ) );
  26. return $diff;
  27. }
  28. private function getMockDiffOp( $type = null, $orig = [], $closing = [] ) {
  29. $diffOp = $this->getMockBuilder( DiffOp::class )
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. $diffOp->expects( $this->any() )
  33. ->method( 'getType' )
  34. ->will( $this->returnValue( $type ) );
  35. $diffOp->expects( $this->any() )
  36. ->method( 'getOrig' )
  37. ->will( $this->returnValue( $orig ) );
  38. if ( $type === 'change' ) {
  39. $diffOp->expects( $this->any() )
  40. ->method( 'getClosing' )
  41. ->with( $this->isType( 'integer' ) )
  42. ->will( $this->returnCallback( function () {
  43. return 'mockLine';
  44. } ) );
  45. } else {
  46. $diffOp->expects( $this->any() )
  47. ->method( 'getClosing' )
  48. ->will( $this->returnValue( $closing ) );
  49. }
  50. return $diffOp;
  51. }
  52. public function provideTestFormat() {
  53. $emptyArrayTestCases = [
  54. $this->getMockDiff( [] ),
  55. $this->getMockDiff( [ $this->getMockDiffOp( 'add' ) ] ),
  56. $this->getMockDiff( [ $this->getMockDiffOp( 'delete' ) ] ),
  57. $this->getMockDiff( [ $this->getMockDiffOp( 'change' ) ] ),
  58. $this->getMockDiff( [ $this->getMockDiffOp( 'copy' ) ] ),
  59. $this->getMockDiff( [ $this->getMockDiffOp( 'FOOBARBAZ' ) ] ),
  60. $this->getMockDiff( [ $this->getMockDiffOp( 'add', 'line' ) ] ),
  61. $this->getMockDiff( [ $this->getMockDiffOp( 'delete', [], [ 'line' ] ) ] ),
  62. $this->getMockDiff( [ $this->getMockDiffOp( 'copy', [], [ 'line' ] ) ] ),
  63. ];
  64. $otherTestCases = [];
  65. $otherTestCases[] = [
  66. $this->getMockDiff( [ $this->getMockDiffOp( 'add', [], [ 'a1' ] ) ] ),
  67. [ [ 'action' => 'add', 'new' => 'a1', 'newline' => 1 ] ],
  68. ];
  69. $otherTestCases[] = [
  70. $this->getMockDiff( [ $this->getMockDiffOp( 'add', [], [ 'a1', 'a2' ] ) ] ),
  71. [
  72. [ 'action' => 'add', 'new' => 'a1', 'newline' => 1 ],
  73. [ 'action' => 'add', 'new' => 'a2', 'newline' => 2 ],
  74. ],
  75. ];
  76. $otherTestCases[] = [
  77. $this->getMockDiff( [ $this->getMockDiffOp( 'delete', [ 'd1' ] ) ] ),
  78. [ [ 'action' => 'delete', 'old' => 'd1', 'oldline' => 1 ] ],
  79. ];
  80. $otherTestCases[] = [
  81. $this->getMockDiff( [ $this->getMockDiffOp( 'delete', [ 'd1', 'd2' ] ) ] ),
  82. [
  83. [ 'action' => 'delete', 'old' => 'd1', 'oldline' => 1 ],
  84. [ 'action' => 'delete', 'old' => 'd2', 'oldline' => 2 ],
  85. ],
  86. ];
  87. $otherTestCases[] = [
  88. $this->getMockDiff( [ $this->getMockDiffOp( 'change', [ 'd1' ], [ 'a1' ] ) ] ),
  89. [ [
  90. 'action' => 'change',
  91. 'old' => 'd1',
  92. 'new' => 'mockLine',
  93. 'newline' => 1, 'oldline' => 1
  94. ] ],
  95. ];
  96. $otherTestCases[] = [
  97. $this->getMockDiff( [ $this->getMockDiffOp(
  98. 'change',
  99. [ 'd1', 'd2' ],
  100. [ 'a1', 'a2' ]
  101. ) ] ),
  102. [
  103. [
  104. 'action' => 'change',
  105. 'old' => 'd1',
  106. 'new' => 'mockLine',
  107. 'newline' => 1, 'oldline' => 1
  108. ],
  109. [
  110. 'action' => 'change',
  111. 'old' => 'd2',
  112. 'new' => 'mockLine',
  113. 'newline' => 2, 'oldline' => 2
  114. ],
  115. ],
  116. ];
  117. $testCases = [];
  118. foreach ( $emptyArrayTestCases as $testCase ) {
  119. $testCases[] = [ $testCase, [] ];
  120. }
  121. foreach ( $otherTestCases as $testCase ) {
  122. $testCases[] = [ $testCase[0], $testCase[1] ];
  123. }
  124. return $testCases;
  125. }
  126. }