5 * @author Adam Shorland
9 class ArrayDiffFormatterTest
extends MediaWikiTestCase
{
13 * @param array $expectedOutput
14 * @dataProvider provideTestFormat
15 * @covers ArrayDiffFormatter::format
17 public function testFormat( $input, $expectedOutput ) {
18 $instance = new ArrayDiffFormatter();
19 $output = $instance->format( $input );
20 $this->assertEquals( $expectedOutput, $output );
23 private function getMockDiff( $edits ) {
24 $diff = $this->getMockBuilder( 'Diff' )
25 ->disableOriginalConstructor()
27 $diff->expects( $this->any() )
28 ->method( 'getEdits' )
29 ->will( $this->returnValue( $edits ) );
33 private function getMockDiffOp( $type = null, $orig = array(), $closing = array() ) {
34 $diffOp = $this->getMockBuilder( 'DiffOp' )
35 ->disableOriginalConstructor()
37 $diffOp->expects( $this->any() )
39 ->will( $this->returnValue( $type ) );
40 $diffOp->expects( $this->any() )
42 ->will( $this->returnValue( $orig ) );
43 if ( $type === 'change' ) {
44 $diffOp->expects( $this->any() )
45 ->method( 'getClosing' )
46 ->with( $this->isType( 'integer' ) )
47 ->will( $this->returnCallback( function () {
51 $diffOp->expects( $this->any() )
52 ->method( 'getClosing' )
53 ->will( $this->returnValue( $closing ) );
58 public function provideTestFormat() {
59 $emptyArrayTestCases = array(
60 $this->getMockDiff( array() ),
61 $this->getMockDiff( array( $this->getMockDiffOp( 'add' ) ) ),
62 $this->getMockDiff( array( $this->getMockDiffOp( 'delete' ) ) ),
63 $this->getMockDiff( array( $this->getMockDiffOp( 'change' ) ) ),
64 $this->getMockDiff( array( $this->getMockDiffOp( 'copy' ) ) ),
65 $this->getMockDiff( array( $this->getMockDiffOp( 'FOOBARBAZ' ) ) ),
66 $this->getMockDiff( array( $this->getMockDiffOp( 'add', 'line' ) ) ),
67 $this->getMockDiff( array( $this->getMockDiffOp( 'delete', array(), array( 'line' ) ) ) ),
68 $this->getMockDiff( array( $this->getMockDiffOp( 'copy', array(), array( 'line' ) ) ) ),
71 $otherTestCases = array();
72 $otherTestCases[] = array(
73 $this->getMockDiff( array( $this->getMockDiffOp( 'add', array( ), array( 'a1' ) ) ) ),
74 array( array( 'action' => 'add', 'new' => 'a1', 'newline' => 1 ) ),
76 $otherTestCases[] = array(
77 $this->getMockDiff( array( $this->getMockDiffOp( 'add', array( ), array( 'a1', 'a2' ) ) ) ),
79 array( 'action' => 'add', 'new' => 'a1', 'newline' => 1 ),
80 array( 'action' => 'add', 'new' => 'a2', 'newline' => 2 ),
83 $otherTestCases[] = array(
84 $this->getMockDiff( array( $this->getMockDiffOp( 'delete', array( 'd1' ) ) ) ),
85 array( array( 'action' => 'delete', 'old' => 'd1', 'oldline' => 1 ) ),
87 $otherTestCases[] = array(
88 $this->getMockDiff( array( $this->getMockDiffOp( 'delete', array( 'd1', 'd2' ) ) ) ),
90 array( 'action' => 'delete', 'old' => 'd1', 'oldline' => 1 ),
91 array( 'action' => 'delete', 'old' => 'd2', 'oldline' => 2 ),
94 $otherTestCases[] = array(
95 $this->getMockDiff( array( $this->getMockDiffOp( 'change', array( 'd1' ), array( 'a1' ) ) ) ),
100 'newline' => 1, 'oldline' => 1
103 $otherTestCases[] = array(
104 $this->getMockDiff( array( $this->getMockDiffOp(
111 'action' => 'change',
114 'newline' => 1, 'oldline' => 1
117 'action' => 'change',
120 'newline' => 2, 'oldline' => 2
125 $testCases = array();
126 foreach ( $emptyArrayTestCases as $testCase ) {
127 $testCases[] = array( $testCase, array() );
129 foreach ( $otherTestCases as $testCase ) {
130 $testCases[] = array( $testCase[0], $testCase[1] );