188ad3fdda1221fc292f77fed412d39d50aebb4c
[lhc/web/wiklou.git] / tests / phpunit / includes / diff / ArrayDiffFormatterTest.php
1 <?php
2
3 /**
4 * @licence GNU GPL v2+
5 * @author Adam Shorland
6 *
7 * @group Diff
8 */
9 class ArrayDiffFormatterTest extends MediaWikiTestCase {
10
11 /**
12 * @param Diff $input
13 * @param array $expectedOutput
14 * @dataProvider provideTestFormat
15 * @covers ArrayDiffFormatter::format
16 */
17 public function testFormat( $input, $expectedOutput ) {
18 $instance = new ArrayDiffFormatter();
19 $output = $instance->format( $input );
20 $this->assertEquals( $expectedOutput, $output );
21 }
22
23 private function getMockDiff( $edits ) {
24 $diff = $this->getMockBuilder( 'Diff' )
25 ->disableOriginalConstructor()
26 ->getMock();
27 $diff->expects( $this->any() )
28 ->method( 'getEdits' )
29 ->will( $this->returnValue( $edits ) );
30 return $diff;
31 }
32
33 private function getMockDiffOp( $type = null, $orig = array(), $closing = array() ) {
34 $diffOp = $this->getMockBuilder( 'DiffOp' )
35 ->disableOriginalConstructor()
36 ->getMock();
37 $diffOp->expects( $this->any() )
38 ->method( 'getType' )
39 ->will( $this->returnValue( $type ) );
40 $diffOp->expects( $this->any() )
41 ->method( 'getOrig' )
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 () {
48 return 'mockLine';
49 } ) );
50 } else {
51 $diffOp->expects( $this->any() )
52 ->method( 'getClosing' )
53 ->will( $this->returnValue( $closing ) );
54 }
55 return $diffOp;
56 }
57
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' ) ) ) ),
69 );
70
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 ) ),
75 );
76 $otherTestCases[] = array(
77 $this->getMockDiff( array( $this->getMockDiffOp( 'add', array( ), array( 'a1', 'a2' ) ) ) ),
78 array(
79 array( 'action' => 'add', 'new' => 'a1', 'newline' => 1 ),
80 array( 'action' => 'add', 'new' => 'a2', 'newline' => 2 ),
81 ),
82 );
83 $otherTestCases[] = array(
84 $this->getMockDiff( array( $this->getMockDiffOp( 'delete', array( 'd1' ) ) ) ),
85 array( array( 'action' => 'delete', 'old' => 'd1', 'oldline' => 1 ) ),
86 );
87 $otherTestCases[] = array(
88 $this->getMockDiff( array( $this->getMockDiffOp( 'delete', array( 'd1', 'd2' ) ) ) ),
89 array(
90 array( 'action' => 'delete', 'old' => 'd1', 'oldline' => 1 ),
91 array( 'action' => 'delete', 'old' => 'd2', 'oldline' => 2 ),
92 ),
93 );
94 $otherTestCases[] = array(
95 $this->getMockDiff( array( $this->getMockDiffOp( 'change', array( 'd1' ), array( 'a1' ) ) ) ),
96 array( array(
97 'action' => 'change',
98 'old' => 'd1',
99 'new' => 'mockLine',
100 'newline' => 1, 'oldline' => 1
101 ) ),
102 );
103 $otherTestCases[] = array(
104 $this->getMockDiff( array( $this->getMockDiffOp(
105 'change',
106 array( 'd1', 'd2' ),
107 array( 'a1', 'a2' )
108 ) ) ),
109 array(
110 array(
111 'action' => 'change',
112 'old' => 'd1',
113 'new' => 'mockLine',
114 'newline' => 1, 'oldline' => 1
115 ),
116 array(
117 'action' => 'change',
118 'old' => 'd2',
119 'new' => 'mockLine',
120 'newline' => 2, 'oldline' => 2
121 ),
122 ),
123 );
124
125 $testCases = array();
126 foreach ( $emptyArrayTestCases as $testCase ) {
127 $testCases[] = array( $testCase, array() );
128 }
129 foreach ( $otherTestCases as $testCase ) {
130 $testCases[] = array( $testCase[0], $testCase[1] );
131 }
132 return $testCases;
133 }
134
135 }