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