MediaWiki
REL1_24
|
00001 <?php 00002 00009 class ArrayDiffFormatterTest extends MediaWikiTestCase { 00010 00017 public function testFormat( $input, $expectedOutput ) { 00018 $instance = new ArrayDiffFormatter(); 00019 $output = $instance->format( $input ); 00020 $this->assertEquals( $expectedOutput, $output ); 00021 } 00022 00023 private function getMockDiff( $edits ) { 00024 $diff = $this->getMockBuilder( 'Diff' ) 00025 ->disableOriginalConstructor() 00026 ->getMock(); 00027 $diff->expects( $this->any() ) 00028 ->method( 'getEdits' ) 00029 ->will( $this->returnValue( $edits ) ); 00030 return $diff; 00031 } 00032 00033 private function getMockDiffOp( $type = null, $orig = array(), $closing = array() ) { 00034 $diffOp = $this->getMockBuilder( 'DiffOp' ) 00035 ->disableOriginalConstructor() 00036 ->getMock(); 00037 $diffOp->expects( $this->any() ) 00038 ->method( 'getType' ) 00039 ->will( $this->returnValue( $type ) ); 00040 $diffOp->expects( $this->any() ) 00041 ->method( 'getOrig' ) 00042 ->will( $this->returnValue( $orig ) ); 00043 if ( $type === 'change' ) { 00044 $diffOp->expects( $this->any() ) 00045 ->method( 'getClosing' ) 00046 ->with( $this->isType( 'integer' ) ) 00047 ->will( $this->returnCallback( function () { 00048 return 'mockLine'; 00049 } ) ); 00050 } else { 00051 $diffOp->expects( $this->any() ) 00052 ->method( 'getClosing' ) 00053 ->will( $this->returnValue( $closing ) ); 00054 } 00055 return $diffOp; 00056 } 00057 00058 public function provideTestFormat() { 00059 $emptyArrayTestCases = array( 00060 $this->getMockDiff( array() ), 00061 $this->getMockDiff( array( $this->getMockDiffOp( 'add' ) ) ), 00062 $this->getMockDiff( array( $this->getMockDiffOp( 'delete' ) ) ), 00063 $this->getMockDiff( array( $this->getMockDiffOp( 'change' ) ) ), 00064 $this->getMockDiff( array( $this->getMockDiffOp( 'copy' ) ) ), 00065 $this->getMockDiff( array( $this->getMockDiffOp( 'FOOBARBAZ' ) ) ), 00066 $this->getMockDiff( array( $this->getMockDiffOp( 'add', 'line' ) ) ), 00067 $this->getMockDiff( array( $this->getMockDiffOp( 'delete', array(), array( 'line' ) ) ) ), 00068 $this->getMockDiff( array( $this->getMockDiffOp( 'copy', array(), array( 'line' ) ) ) ), 00069 ); 00070 00071 $otherTestCases = array(); 00072 $otherTestCases[] = array( 00073 $this->getMockDiff( array( $this->getMockDiffOp( 'add', array( ), array( 'a1' ) ) ) ), 00074 array( array( 'action' => 'add', 'new' => 'a1', 'newline' => 1 ) ), 00075 ); 00076 $otherTestCases[] = array( 00077 $this->getMockDiff( array( $this->getMockDiffOp( 'add', array( ), array( 'a1', 'a2' ) ) ) ), 00078 array( 00079 array( 'action' => 'add', 'new' => 'a1', 'newline' => 1 ), 00080 array( 'action' => 'add', 'new' => 'a2', 'newline' => 2 ), 00081 ), 00082 ); 00083 $otherTestCases[] = array( 00084 $this->getMockDiff( array( $this->getMockDiffOp( 'delete', array( 'd1' ) ) ) ), 00085 array( array( 'action' => 'delete', 'old' => 'd1', 'oldline' => 1 ) ), 00086 ); 00087 $otherTestCases[] = array( 00088 $this->getMockDiff( array( $this->getMockDiffOp( 'delete', array( 'd1', 'd2' ) ) ) ), 00089 array( 00090 array( 'action' => 'delete', 'old' => 'd1', 'oldline' => 1 ), 00091 array( 'action' => 'delete', 'old' => 'd2', 'oldline' => 2 ), 00092 ), 00093 ); 00094 $otherTestCases[] = array( 00095 $this->getMockDiff( array( $this->getMockDiffOp( 'change', array( 'd1' ), array( 'a1' ) ) ) ), 00096 array( array( 00097 'action' => 'change', 00098 'old' => 'd1', 00099 'new' => 'mockLine', 00100 'newline' => 1, 'oldline' => 1 00101 ) ), 00102 ); 00103 $otherTestCases[] = array( 00104 $this->getMockDiff( array( $this->getMockDiffOp( 00105 'change', 00106 array( 'd1', 'd2' ), 00107 array( 'a1', 'a2' ) 00108 ) ) ), 00109 array( 00110 array( 00111 'action' => 'change', 00112 'old' => 'd1', 00113 'new' => 'mockLine', 00114 'newline' => 1, 'oldline' => 1 00115 ), 00116 array( 00117 'action' => 'change', 00118 'old' => 'd2', 00119 'new' => 'mockLine', 00120 'newline' => 2, 'oldline' => 2 00121 ), 00122 ), 00123 ); 00124 00125 $testCases = array(); 00126 foreach ( $emptyArrayTestCases as $testCase ) { 00127 $testCases[] = array( $testCase, array() ); 00128 } 00129 foreach ( $otherTestCases as $testCase ) { 00130 $testCases[] = array( $testCase[0], $testCase[1] ); 00131 } 00132 return $testCases; 00133 } 00134 00135 }