createMock() does the same, but is much easier to read. A small difference is that some of the replacements made in this patch didn't use disableOriginalConstructor() before. In case this was relevant we should see the respective test fail. If not we can save some CPU cycles and skip these constructors. Change-Id: Ib98fb06e0fe753b7a53cb087a47e1159515a8ad5
85 lines
2.6 KiB
PHP
85 lines
2.6 KiB
PHP
<?php
|
|
|
|
use Wikimedia\Rdbms\IResultWrapper;
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
/**
|
|
* @covers RevisionListBase
|
|
*
|
|
* @author DannyS712
|
|
*/
|
|
class RevisionListBaseTest extends MediaWikiUnitTestCase {
|
|
|
|
public function testGetType() {
|
|
$revisionListBase = $this->getMockBuilder( RevisionListBase::class )
|
|
->disableOriginalConstructor()
|
|
->getMockForAbstractClass();
|
|
|
|
$this->assertNull( $revisionListBase->getType() );
|
|
}
|
|
|
|
public function testReset() {
|
|
$revisionListBase = $this->getMockBuilder( RevisionListBase::class )
|
|
->disableOriginalConstructor()
|
|
->getMockForAbstractClass();
|
|
|
|
$revisionItemBase = $this->getMockBuilder( RevisionItemBase::class )
|
|
->disableOriginalConstructor()
|
|
->getMockForAbstractClass();
|
|
|
|
// Actual contents aren't used
|
|
$fakeRow = (object)[ 'key' => 'val' ];
|
|
|
|
$resultWrapper = $this->createMock( IResultWrapper::class );
|
|
$resultWrapper->expects( $this->once() )
|
|
->method( 'rewind' );
|
|
$resultWrapper->expects( $this->once() )
|
|
->method( 'current' )
|
|
->willReturn( $fakeRow );
|
|
|
|
$revisionListBase->expects( $this->once() )
|
|
->method( 'newItem' )
|
|
->with( $fakeRow )
|
|
->willReturn( $revisionItemBase );
|
|
|
|
// res is normally the result of a db query that uses wfGetDB and cannot
|
|
// be tested in a unit test
|
|
$mockAccess = TestingAccessWrapper::newFromObject( $revisionListBase );
|
|
$mockAccess->res = $resultWrapper;
|
|
|
|
$this->assertSame( $revisionItemBase, $revisionListBase->reset() );
|
|
$this->assertSame( $revisionItemBase, $revisionListBase->current() );
|
|
}
|
|
|
|
public function testResultWrapper() {
|
|
// Test methods that only depend on the result wrapper
|
|
$revisionListBase = $this->getMockBuilder( RevisionListBase::class )
|
|
->disableOriginalConstructor()
|
|
->getMockForAbstractClass();
|
|
|
|
$this->assertSame( 0, $revisionListBase->key() );
|
|
$this->assertFalse( $revisionListBase->valid() );
|
|
$this->assertSame( 0, $revisionListBase->length() );
|
|
|
|
$resultWrapper = $this->createMock( IResultWrapper::class );
|
|
$resultWrapper->expects( $this->once() )
|
|
->method( 'key' )
|
|
->willReturn( 991 );
|
|
$resultWrapper->expects( $this->once() )
|
|
->method( 'valid' )
|
|
->willReturn( true );
|
|
$resultWrapper->expects( $this->once() )
|
|
->method( 'numRows' )
|
|
->willReturn( 457 );
|
|
|
|
// res is normally the result of a db query that uses wfGetDB and cannot
|
|
// be tested in a unit test
|
|
$mockAccess = TestingAccessWrapper::newFromObject( $revisionListBase );
|
|
$mockAccess->res = $resultWrapper;
|
|
|
|
$this->assertSame( 991, $revisionListBase->key() );
|
|
$this->assertTrue( $revisionListBase->valid() );
|
|
$this->assertSame( 457, $revisionListBase->length() );
|
|
}
|
|
|
|
}
|