mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Echo
synced 2024-11-28 01:30:15 +00:00
db79d76d83
Notably: any() is the default anyway. It doesn't really make the tests more specific or better readable when we repeat it all the time. Change-Id: I56d201bfce454587b00015b7208f313dd8ed9624
161 lines
3.5 KiB
PHP
161 lines
3.5 KiB
PHP
<?php
|
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
/**
|
|
* @group Database
|
|
* @covers \EchoEventMapper
|
|
*/
|
|
class EchoEventMapperTest extends MediaWikiIntegrationTestCase {
|
|
|
|
protected function setUp(): void {
|
|
parent::setUp();
|
|
$this->tablesUsed[] = 'echo_event';
|
|
$this->tablesUsed[] = 'echo_notification';
|
|
$this->tablesUsed[] = 'echo_target_page';
|
|
}
|
|
|
|
public function provideDataTestInsert() {
|
|
return [
|
|
[
|
|
'successful insert with next sequence = 1',
|
|
[ 'insert' => true, 'insertId' => 1 ],
|
|
1
|
|
],
|
|
[
|
|
'successful insert with insert id = 2',
|
|
[ 'insert' => true, 'insertId' => 2 ],
|
|
2
|
|
]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideDataTestInsert
|
|
*/
|
|
public function testInsert( $message, $dbResult, $result ) {
|
|
$event = $this->mockEchoEvent();
|
|
$eventMapper = new EchoEventMapper( $this->mockMWEchoDbFactory( $dbResult ) );
|
|
$this->assertEquals( $result, $eventMapper->insert( $event ), $message );
|
|
}
|
|
|
|
/**
|
|
* Successful fetchById()
|
|
*/
|
|
public function testSuccessfulFetchById() {
|
|
$eventMapper = new EchoEventMapper(
|
|
$this->mockMWEchoDbFactory(
|
|
[
|
|
'selectRow' => (object)[
|
|
'event_id' => 1,
|
|
'event_type' => 'test',
|
|
'event_variant' => '',
|
|
'event_extra' => '',
|
|
'event_page_id' => '',
|
|
'event_agent_id' => '',
|
|
'event_agent_ip' => '',
|
|
'event_deleted' => 0,
|
|
]
|
|
]
|
|
)
|
|
);
|
|
$res = $eventMapper->fetchById( 1 );
|
|
$this->assertInstanceOf( EchoEvent::class, $res );
|
|
}
|
|
|
|
public function testUnsuccessfulFetchById() {
|
|
$eventMapper = new EchoEventMapper(
|
|
$this->mockMWEchoDbFactory(
|
|
[
|
|
'selectRow' => false
|
|
]
|
|
)
|
|
);
|
|
$this->expectException( MWException::class );
|
|
$eventMapper->fetchById( 1 );
|
|
}
|
|
|
|
/**
|
|
* @return EchoEvent
|
|
*/
|
|
protected function mockEchoEvent() {
|
|
$event = $this->createMock( EchoEvent::class );
|
|
$event->method( 'toDbArray' )
|
|
->willReturn( [] );
|
|
|
|
return $event;
|
|
}
|
|
|
|
/**
|
|
* @param array $dbResult
|
|
* @return MWEchoDbFactory
|
|
*/
|
|
protected function mockMWEchoDbFactory( $dbResult ) {
|
|
$dbFactory = $this->createMock( MWEchoDbFactory::class );
|
|
$dbFactory->method( 'getEchoDb' )
|
|
->willReturn( $this->mockDb( $dbResult ) );
|
|
|
|
return $dbFactory;
|
|
}
|
|
|
|
/**
|
|
* @param array $dbResult
|
|
* @return IDatabase
|
|
*/
|
|
protected function mockDb( array $dbResult ) {
|
|
$dbResult += [
|
|
'insert' => '',
|
|
'insertId' => '',
|
|
'select' => '',
|
|
'selectRow' => ''
|
|
];
|
|
$db = $this->createMock( IDatabase::class );
|
|
$db->method( 'insert' )
|
|
->willReturn( $dbResult['insert'] );
|
|
$db->method( 'insertId' )
|
|
->willReturn( $dbResult['insertId'] );
|
|
$db->method( 'select' )
|
|
->willReturn( $dbResult['select'] );
|
|
$db->method( 'selectRow' )
|
|
->willReturn( $dbResult['selectRow'] );
|
|
|
|
return $db;
|
|
}
|
|
|
|
/**
|
|
* @covers \EchoEventMapper::fetchIdsByPage
|
|
*/
|
|
public function testFetchByPage() {
|
|
$user = $this->getTestUser()->getUser();
|
|
$page = $this->getExistingTestPage();
|
|
|
|
// Create a notification that is not associated with any page
|
|
EchoEvent::create( [
|
|
'type' => 'welcome',
|
|
'agent' => $user,
|
|
] );
|
|
|
|
// Create a notification with a title
|
|
$eventWithTitle = EchoEvent::create( [
|
|
'type' => 'welcome',
|
|
'agent' => $user,
|
|
'title' => $page->getTitle()
|
|
] );
|
|
|
|
// Create a notification with a target-page
|
|
$eventWithTargetPage = EchoEvent::create( [
|
|
'type' => 'welcome',
|
|
'agent' => $user,
|
|
'extra' => [ 'target-page' => $page->getId() ]
|
|
] );
|
|
|
|
$eventMapper = new EchoEventMapper();
|
|
|
|
$this->assertArrayEquals(
|
|
[ $eventWithTitle->getId(), $eventWithTargetPage->getId() ],
|
|
$eventMapper->fetchIdsByPage( $page->getId() )
|
|
);
|
|
}
|
|
|
|
}
|