diff options
author | addshore <addshorewiki@gmail.com> | 2014-02-26 22:47:40 +0100 |
---|---|---|
committer | addshore <addshorewiki@gmail.com> | 2014-02-26 22:53:05 +0100 |
commit | 3d1fba6e726fb9e977a23e1c4e080ccbbd9fd51d (patch) | |
tree | ce4bb24450154d2c25a6df5d9c234b6d6f40244d /tests/phpunit/includes/diff/ArrayDiffFormatterTest.php | |
parent | 96b04cef361c26bbc6b76be6afbf298e4d22fed3 (diff) | |
download | mediawikicore-3d1fba6e726fb9e977a23e1c4e080ccbbd9fd51d.tar.gz mediawikicore-3d1fba6e726fb9e977a23e1c4e080ccbbd9fd51d.zip |
Add tests for ArrayDiffFormatter and DiffOp
This change also adds some tested getters to
the DiffOp class to improve testability of the class
Change-Id: I26eafd791b9892f565a18d0502474ef9c24f9a29
Diffstat (limited to 'tests/phpunit/includes/diff/ArrayDiffFormatterTest.php')
-rw-r--r-- | tests/phpunit/includes/diff/ArrayDiffFormatterTest.php | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/tests/phpunit/includes/diff/ArrayDiffFormatterTest.php b/tests/phpunit/includes/diff/ArrayDiffFormatterTest.php new file mode 100644 index 000000000000..50c5c572e454 --- /dev/null +++ b/tests/phpunit/includes/diff/ArrayDiffFormatterTest.php @@ -0,0 +1,116 @@ +<?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; + } + +} |