2014-08-06 00:16:10 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-02 20:47:04 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Mapper\TargetPageMapper;
|
2022-11-02 21:34:17 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Model\TargetPage;
|
2019-10-05 03:54:45 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
|
2018-01-24 00:31:53 +00:00
|
|
|
/**
|
2022-11-02 20:47:04 +00:00
|
|
|
* @covers \MediaWiki\Extension\Notifications\Mapper\TargetPageMapper
|
2018-01-24 00:31:53 +00:00
|
|
|
*/
|
2022-11-02 20:47:04 +00:00
|
|
|
class TargetPageMapperTest extends MediaWikiUnitTestCase {
|
2014-08-06 00:16:10 +00:00
|
|
|
|
2023-05-20 18:35:53 +00:00
|
|
|
public static 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 ) {
|
2022-11-02 21:34:17 +00:00
|
|
|
$target = $this->mockTargetPage();
|
2022-11-02 20:47:04 +00:00
|
|
|
$targetMapper = new TargetPageMapper( $this->mockMWEchoDbFactory( $dbResult ) );
|
2014-08-06 00:16:10 +00:00
|
|
|
$this->assertEquals( $result, $targetMapper->insert( $target ), $message );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-02 21:34:17 +00:00
|
|
|
* Mock object of TargetPage
|
|
|
|
* @return TargetPage
|
2014-08-06 00:16:10 +00:00
|
|
|
*/
|
2022-11-02 21:34:17 +00:00
|
|
|
protected function mockTargetPage() {
|
|
|
|
$target = $this->createMock( TargetPage::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$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
|
2022-11-02 20:47:04 +00:00
|
|
|
* @return 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|