2014-08-06 00:16:10 +00:00
|
|
|
<?php
|
|
|
|
|
2019-10-05 03:54:45 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
|
2018-01-24 00:31:53 +00:00
|
|
|
/**
|
2019-10-23 10:23:09 +00:00
|
|
|
* @covers \EchoTargetPageMapper
|
2018-01-24 00:31:53 +00:00
|
|
|
*/
|
2019-07-10 20:47:28 +00:00
|
|
|
class EchoTargetPageMapperTest extends MediaWikiUnitTestCase {
|
2014-08-06 00:16:10 +00:00
|
|
|
|
|
|
|
public function provideDataTestInsert() {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
|
|
|
[
|
2014-08-06 00:16:10 +00:00
|
|
|
'successful insert with next sequence = 1',
|
2017-09-06 16:47:22 +00:00
|
|
|
[ 'insert' => true, 'insertId' => 2 ],
|
2014-08-06 00:16:10 +00:00
|
|
|
1
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
[
|
2014-08-06 00:16:10 +00:00
|
|
|
'successful insert with insert id = 2',
|
2017-09-06 16:47:22 +00:00
|
|
|
[ 'insert' => true, 'insertId' => 2 ],
|
2014-08-06 00:16:10 +00:00
|
|
|
2
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
];
|
2014-08-06 00:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideDataTestInsert
|
|
|
|
*/
|
|
|
|
public function testInsert( $message, $dbResult, $result ) {
|
|
|
|
$target = $this->mockEchoTargetPage();
|
|
|
|
$targetMapper = new EchoTargetPageMapper( $this->mockMWEchoDbFactory( $dbResult ) );
|
|
|
|
$this->assertEquals( $result, $targetMapper->insert( $target ), $message );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock object of EchoTargetPage
|
2021-01-23 11:54:27 +00:00
|
|
|
* @return EchoTargetPage
|
2014-08-06 00:16:10 +00:00
|
|
|
*/
|
|
|
|
protected function mockEchoTargetPage() {
|
2022-09-29 13:41:35 +00:00
|
|
|
$target = $this->createMock( EchoTargetPage::class );
|
|
|
|
$target->method( 'toDbArray' )
|
|
|
|
->willReturn( [] );
|
|
|
|
$target->method( 'getPageId' )
|
|
|
|
->willReturn( 2 );
|
|
|
|
$target->method( 'getEventId' )
|
|
|
|
->willReturn( 3 );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-06 00:16:10 +00:00
|
|
|
return $target;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock object of MWEchoDbFactory
|
2021-01-23 11:54:27 +00:00
|
|
|
* @param array $dbResult
|
|
|
|
* @return MWEchoDbFactory
|
2014-08-06 00:16:10 +00:00
|
|
|
*/
|
|
|
|
protected function mockMWEchoDbFactory( $dbResult ) {
|
2022-09-29 13:41:35 +00:00
|
|
|
$dbFactory = $this->createMock( MWEchoDbFactory::class );
|
|
|
|
$dbFactory->method( 'getEchoDb' )
|
|
|
|
->willReturn( $this->mockDb( $dbResult ) );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-06 00:16:10 +00:00
|
|
|
return $dbFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-16 18:58:52 +00:00
|
|
|
* Returns a mock database object
|
2021-01-23 11:54:27 +00:00
|
|
|
* @param array $dbResult
|
2018-01-16 18:58:52 +00:00
|
|
|
* @return \Wikimedia\Rdbms\IDatabase
|
2014-08-06 00:16:10 +00:00
|
|
|
*/
|
|
|
|
protected function mockDb( array $dbResult ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$dbResult += [
|
2014-08-06 00:16:10 +00:00
|
|
|
'insert' => '',
|
|
|
|
'insertId' => '',
|
|
|
|
'select' => '',
|
|
|
|
'delete' => ''
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2019-10-05 03:54:45 +00:00
|
|
|
$db = $this->createMock( IDatabase::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$db->method( 'insert' )
|
|
|
|
->willReturn( $dbResult['insert'] );
|
|
|
|
$db->method( 'insertId' )
|
|
|
|
->willReturn( $dbResult['insertId'] );
|
|
|
|
$db->method( 'select' )
|
|
|
|
->willReturn( $dbResult['select'] );
|
|
|
|
$db->method( 'delete' )
|
|
|
|
->willReturn( $dbResult['delete'] );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-06 00:16:10 +00:00
|
|
|
return $db;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|