MediaWiki  REL1_23
ArrayDiffFormatterTest.php
Go to the documentation of this file.
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( 'action' => 'change', 'old' => 'd1', 'new' => 'mockLine', 'newline' => 1, 'oldline' => 1 ) ),
00097         );
00098         $otherTestCases[] = array(
00099             $this->getMockDiff( array( $this->getMockDiffOp( 'change', array( 'd1', 'd2' ), array( 'a1', 'a2' ) ) ) ),
00100             array(
00101                 array( 'action' => 'change', 'old' => 'd1', 'new' => 'mockLine', 'newline' => 1, 'oldline' => 1 ),
00102                 array( 'action' => 'change', 'old' => 'd2', 'new' => 'mockLine', 'newline' => 2, 'oldline' => 2 ),
00103             ),
00104         );
00105 
00106         $testCases = array();
00107         foreach ( $emptyArrayTestCases as $testCase ) {
00108             $testCases[] = array( $testCase, array() );
00109         }
00110         foreach ( $otherTestCases as $testCase ) {
00111             $testCases[] = array( $testCase[0], $testCase[1] );
00112         }
00113         return $testCases;
00114     }
00115 
00116 }