| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Tests\Diff; |
| 4 |
|
| 5 |
use MediaWikiUnitTestCase; |
| 6 |
use Wikimedia\Diff\ArrayDiffFormatter; |
| 7 |
use Wikimedia\Diff\Diff; |
| 8 |
use Wikimedia\Diff\DiffOp; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class ArrayDiffFormatterTest extends MediaWikiUnitTestCase { |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public function testFormat( Diff $input, array $expectedOutput ) { |
| 24 |
$instance = new ArrayDiffFormatter(); |
| 25 |
$output = $instance->format( $input ); |
| 26 |
$this->assertSame( $expectedOutput, $output ); |
| 27 |
} |
| 28 |
|
| 29 |
private function getMockDiff( array $edits ) { |
| 30 |
$diff = $this->createMock( Diff::class ); |
| 31 |
$diff->method( 'getEdits' ) |
| 32 |
->willReturn( $edits ); |
| 33 |
return $diff; |
| 34 |
} |
| 35 |
|
| 36 |
private function getMockDiffOp( string $type, $orig = [], array $closing = [] ) { |
| 37 |
$diffOp = $this->createMock( DiffOp::class ); |
| 38 |
$diffOp->method( 'getType' ) |
| 39 |
->willReturn( $type ); |
| 40 |
$diffOp->method( 'getOrig' ) |
| 41 |
->willReturn( $orig ); |
| 42 |
if ( $type === 'change' ) { |
| 43 |
$diffOp->method( 'getClosing' ) |
| 44 |
->with( $this->isType( 'integer' ) ) |
| 45 |
->willReturn( 'mockLine' ); |
| 46 |
} else { |
| 47 |
$diffOp->method( 'getClosing' ) |
| 48 |
->willReturn( $closing ); |
| 49 |
} |
| 50 |
return $diffOp; |
| 51 |
} |
| 52 |
|
| 53 |
public function provideTestFormat() { |
| 54 |
$emptyArrayTestCases = [ |
| 55 |
$this->getMockDiff( [] ), |
| 56 |
$this->getMockDiff( [ $this->getMockDiffOp( 'add' ) ] ), |
| 57 |
$this->getMockDiff( [ $this->getMockDiffOp( 'delete' ) ] ), |
| 58 |
$this->getMockDiff( [ $this->getMockDiffOp( 'change' ) ] ), |
| 59 |
$this->getMockDiff( [ $this->getMockDiffOp( 'copy' ) ] ), |
| 60 |
$this->getMockDiff( [ $this->getMockDiffOp( 'FOOBARBAZ' ) ] ), |
| 61 |
$this->getMockDiff( [ $this->getMockDiffOp( 'add', 'line' ) ] ), |
| 62 |
$this->getMockDiff( [ $this->getMockDiffOp( 'delete', [], [ 'line' ] ) ] ), |
| 63 |
$this->getMockDiff( [ $this->getMockDiffOp( 'copy', [], [ 'line' ] ) ] ), |
| 64 |
]; |
| 65 |
foreach ( $emptyArrayTestCases as $testCase ) { |
| 66 |
yield [ $testCase, [] ]; |
| 67 |
} |
| 68 |
|
| 69 |
yield [ |
| 70 |
$this->getMockDiff( [ $this->getMockDiffOp( 'add', [], [ 'a1' ] ) ] ), |
| 71 |
[ [ 'action' => 'add', 'new' => 'a1', 'newline' => 1 ] ], |
| 72 |
]; |
| 73 |
yield [ |
| 74 |
$this->getMockDiff( [ $this->getMockDiffOp( 'add', [], [ 'a1', 'a2' ] ) ] ), |
| 75 |
[ |
| 76 |
[ 'action' => 'add', 'new' => 'a1', 'newline' => 1 ], |
| 77 |
[ 'action' => 'add', 'new' => 'a2', 'newline' => 2 ], |
| 78 |
], |
| 79 |
]; |
| 80 |
yield [ |
| 81 |
$this->getMockDiff( [ $this->getMockDiffOp( 'delete', [ 'd1' ] ) ] ), |
| 82 |
[ [ 'action' => 'delete', 'old' => 'd1', 'oldline' => 1 ] ], |
| 83 |
]; |
| 84 |
yield [ |
| 85 |
$this->getMockDiff( [ $this->getMockDiffOp( 'delete', [ 'd1', 'd2' ] ) ] ), |
| 86 |
[ |
| 87 |
[ 'action' => 'delete', 'old' => 'd1', 'oldline' => 1 ], |
| 88 |
[ 'action' => 'delete', 'old' => 'd2', 'oldline' => 2 ], |
| 89 |
], |
| 90 |
]; |
| 91 |
yield [ |
| 92 |
$this->getMockDiff( [ $this->getMockDiffOp( 'change', [ 'd1' ], [ 'a1' ] ) ] ), |
| 93 |
[ [ |
| 94 |
'action' => 'change', |
| 95 |
'old' => 'd1', |
| 96 |
'new' => 'mockLine', |
| 97 |
'oldline' => 1, |
| 98 |
'newline' => 1, |
| 99 |
] ], |
| 100 |
]; |
| 101 |
yield [ |
| 102 |
$this->getMockDiff( [ $this->getMockDiffOp( |
| 103 |
'change', |
| 104 |
[ 'd1', 'd2' ], |
| 105 |
[ 'a1', 'a2' ] |
| 106 |
) ] ), |
| 107 |
[ |
| 108 |
[ |
| 109 |
'action' => 'change', |
| 110 |
'old' => 'd1', |
| 111 |
'new' => 'mockLine', |
| 112 |
'oldline' => 1, |
| 113 |
'newline' => 1, |
| 114 |
], |
| 115 |
[ |
| 116 |
'action' => 'change', |
| 117 |
'old' => 'd2', |
| 118 |
'new' => 'mockLine', |
| 119 |
'oldline' => 2, |
| 120 |
'newline' => 2, |
| 121 |
], |
| 122 |
], |
| 123 |
]; |
| 124 |
} |
| 125 |
|
| 126 |
} |