summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/diff/ArrayDiffFormatterTest.php
blob: 188ad3fdda1221fc292f77fed412d39d50aebb4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php

/**
 * @licence GNU GPL v2+
 * @author Adam Shorland
 *
 * @group Diff
 */
class ArrayDiffFormatterTest extends MediaWikiTestCase {

	/**
	 * @param Diff $input
	 * @param array $expectedOutput
	 * @dataProvider provideTestFormat
	 * @covers ArrayDiffFormatter::format
	 */
	public function testFormat( $input, $expectedOutput ) {
		$instance = new ArrayDiffFormatter();
		$output = $instance->format( $input );
		$this->assertEquals( $expectedOutput, $output );
	}

	private function getMockDiff( $edits ) {
		$diff = $this->getMockBuilder( 'Diff' )
			->disableOriginalConstructor()
			->getMock();
		$diff->expects( $this->any() )
			->method( 'getEdits' )
			->will( $this->returnValue( $edits ) );
		return $diff;
	}

	private function getMockDiffOp( $type = null, $orig = array(), $closing = array() ) {
		$diffOp = $this->getMockBuilder( 'DiffOp' )
			->disableOriginalConstructor()
			->getMock();
		$diffOp->expects( $this->any() )
			->method( 'getType' )
			->will( $this->returnValue( $type ) );
		$diffOp->expects( $this->any() )
			->method( 'getOrig' )
			->will( $this->returnValue( $orig ) );
		if ( $type === 'change' ) {
			$diffOp->expects( $this->any() )
				->method( 'getClosing' )
				->with( $this->isType( 'integer' ) )
				->will( $this->returnCallback( function () {
					return 'mockLine';
				} ) );
		} else {
			$diffOp->expects( $this->any() )
				->method( 'getClosing' )
				->will( $this->returnValue( $closing ) );
		}
		return $diffOp;
	}

	public function provideTestFormat() {
		$emptyArrayTestCases = array(
			$this->getMockDiff( array() ),
			$this->getMockDiff( array( $this->getMockDiffOp( 'add' ) ) ),
			$this->getMockDiff( array( $this->getMockDiffOp( 'delete' ) ) ),
			$this->getMockDiff( array( $this->getMockDiffOp( 'change' ) ) ),
			$this->getMockDiff( array( $this->getMockDiffOp( 'copy' ) ) ),
			$this->getMockDiff( array( $this->getMockDiffOp( 'FOOBARBAZ' ) ) ),
			$this->getMockDiff( array( $this->getMockDiffOp( 'add', 'line' ) ) ),
			$this->getMockDiff( array( $this->getMockDiffOp( 'delete', array(), array( 'line' ) ) ) ),
			$this->getMockDiff( array( $this->getMockDiffOp( 'copy', array(), array( 'line' ) ) ) ),
		);

		$otherTestCases = array();
		$otherTestCases[] = array(
			$this->getMockDiff( array( $this->getMockDiffOp( 'add', array( ), array( 'a1' ) ) ) ),
			array( array( 'action' => 'add', 'new' => 'a1', 'newline' => 1 ) ),
		);
		$otherTestCases[] = array(
			$this->getMockDiff( array( $this->getMockDiffOp( 'add', array( ), array( 'a1', 'a2' ) ) ) ),
			array(
				array( 'action' => 'add', 'new' => 'a1', 'newline' => 1 ),
				array( 'action' => 'add', 'new' => 'a2', 'newline' => 2 ),
			),
		);
		$otherTestCases[] = array(
			$this->getMockDiff( array( $this->getMockDiffOp( 'delete', array( 'd1' ) ) ) ),
			array( array( 'action' => 'delete', 'old' => 'd1', 'oldline' => 1 ) ),
		);
		$otherTestCases[] = array(
			$this->getMockDiff( array( $this->getMockDiffOp( 'delete', array( 'd1', 'd2' ) ) ) ),
			array(
				array( 'action' => 'delete', 'old' => 'd1', 'oldline' => 1 ),
				array( 'action' => 'delete', 'old' => 'd2', 'oldline' => 2 ),
			),
		);
		$otherTestCases[] = array(
			$this->getMockDiff( array( $this->getMockDiffOp( 'change', array( 'd1' ), array( 'a1' ) ) ) ),
			array( array(
				'action' => 'change',
				'old' => 'd1',
				'new' => 'mockLine',
				'newline' => 1, 'oldline' => 1
			) ),
		);
		$otherTestCases[] = array(
			$this->getMockDiff( array( $this->getMockDiffOp(
				'change',
				array( 'd1', 'd2' ),
				array( 'a1', 'a2' )
			) ) ),
			array(
				array(
					'action' => 'change',
					'old' => 'd1',
					'new' => 'mockLine',
					'newline' => 1, 'oldline' => 1
				),
				array(
					'action' => 'change',
					'old' => 'd2',
					'new' => 'mockLine',
					'newline' => 2, 'oldline' => 2
				),
			),
		);

		$testCases = array();
		foreach ( $emptyArrayTestCases as $testCase ) {
			$testCases[] = array( $testCase, array() );
		}
		foreach ( $otherTestCases as $testCase ) {
			$testCases[] = array( $testCase[0], $testCase[1] );
		}
		return $testCases;
	}

}